Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
zbNode
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ken
zbNode
Commits
3d617f8e
Commit
3d617f8e
authored
Apr 03, 2018
by
ken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
145 additions
and
0 deletions
+145
-0
.gitignore
.gitignore
+66
-0
package.json
package.json
+15
-0
zb.js
zb.js
+64
-0
No files found.
.gitignore
0 → 100644
View file @
3d617f8e
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
.idea/
package-lock.json
\ No newline at end of file
package.json
0 → 100644
View file @
3d617f8e
{
"name"
:
"
zbnode
"
,
"version"
:
"1.0.0"
,
"description"
:
""
,
"main"
:
"zb.js"
,
"scripts"
:
{
"zb"
:
"node $NODE_DEBUG_OPTION zb.js"
},
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
redis
"
:
"^2.8.0"
,
"
ws
"
:
"^5.1.0"
}
}
zb.js
0 → 100644
View file @
3d617f8e
const
stream
=
'wss://api.zb.com:9999/websocket'
;
const
WebSocket
=
require
(
'ws'
);
const
options
=
{};
const
observerSymbol
=
[
"btcusdt"
];
const
redis
=
require
(
'redis'
);
const
redisClient
=
redis
.
createClient
();
const
publishKey
=
"ZB@2"
;
const
_handleSocketError
=
function
(
error
)
{
// Errors ultimately result in a `close` event.
// see: https://github.com/websockets/ws/blob/828194044bf247af852b31c49e2800d557fedeff/lib/websocket.js#L126
console
.
log
(
'WebSocket error: '
+
this
.
endpoint
+
(
error
.
code
?
' ('
+
error
.
code
+
')'
:
''
)
+
(
error
.
message
?
' '
+
error
.
message
:
''
)
);
};
const
_handleSocketOpen
=
function
(
opened_callback
)
{
this
.
isAlive
=
true
;
console
.
log
(
"open"
);
for
(
const
symbol
of
observerSymbol
)
{
const
req
=
{
event
:
'addChannel'
,
channel
:
symbol
+
'_depth'
};
this
.
send
(
JSON
.
stringify
(
req
));
}
};
const
_handleSocketClose
=
function
(
code
,
reason
)
{
console
.
log
(
code
);
console
.
log
(
reason
);
console
.
log
(
'close'
);
subscribe
();
};
const
_subscribe
=
function
(
callback
,
opened_callback
=
false
)
{
const
ws
=
new
WebSocket
(
stream
);
ws
.
reconnect
=
options
.
reconnect
;
ws
.
isAlive
=
false
;
ws
.
on
(
'open'
,
_handleSocketOpen
.
bind
(
ws
,
opened_callback
));
// ws.on('pong', _handleSocketHeartbeat);
ws
.
on
(
'error'
,
_handleSocketError
);
ws
.
on
(
'close'
,
_handleSocketClose
.
bind
(
ws
));
ws
.
on
(
'message'
,
function
(
data
)
{
try
{
callback
(
JSON
.
parse
(
data
));
}
catch
(
error
)
{
console
.
log
(
'Parse error: '
+
error
.
message
);
}
});
return
ws
;
};
function
subscribe
()
{
_subscribe
((
depth
)
=>
{
console
.
log
(
depth
);
const
key
=
depth
.
channel
.
replace
(
'_'
,
''
).
toUpperCase
()
+
'@zb'
;
const
symbol
=
depth
.
channel
.
replace
(
'_depth'
,
''
).
toUpperCase
();
const
publishContent
=
JSON
.
stringify
({
symbol
,
time
:
depth
.
timestamp
*
1000
})
const
depthJson
=
JSON
.
stringify
({
asks
:
depth
.
asks
.
reverse
(),
bids
:
depth
.
bids
,
symbol
})
redisClient
.
set
(
depthJson
,
key
);
redisClient
.
publish
(
publishKey
,
publishContent
);
})
}
subscribe
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment