Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kucoin
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
rongjun
kucoin
Commits
09eaf605
Commit
09eaf605
authored
Oct 05, 2018
by
zihan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用3个ws链接
parent
576f0d0c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
62 deletions
+75
-62
api_kucoin.js
api_kucoin.js
+75
-62
No files found.
api_kucoin.js
View file @
09eaf605
...
...
@@ -47,52 +47,52 @@ class biboxApi {
this
.
allowRequest
=
true
;
}
subscribeSymbolsAndTicker
(
symbols
,
callback
)
{
this
.
_publicRequest
(
'/v1/bullet/usercenter/loginUser'
,{
protocol
:
'websocket'
,
encrypt
:
true
},(
error
,
result
)
=>
{
if
(
error
){
console
.
log
(
"fetch ws token error"
);
this
.
subscribeSymbolsAndTicker
(
symbols
,
callback
);
}
else
{
const
token
=
result
.
data
.
bulletToken
;
this
.
_doSubscribe
(
symbols
,
token
,
callback
);
}
});
}
//
subscribeSymbolsAndTicker(symbols, callback) {
//
this._publicRequest('/v1/bullet/usercenter/loginUser',{protocol:'websocket',encrypt:true},(error,result)=>{
//
if(error){
//
console.log("fetch ws token error");
//
this.subscribeSymbolsAndTicker(symbols,callback);
//
}else{
//
const token = result.data.bulletToken;
// this._openWs
(symbols,token,callback);
//
}
//
});
//
}
_doSubscribe
(
symbols
,
token
,
callback
)
{
const
reqURL
=
`
${
wsUrl
}
?bulletToken=
${
token
}
&format=json&resource=api`
;
const
wss
=
new
WebSocket
(
reqURL
,
{
agent
});
wss
.
on
(
'open'
,
()
=>
{
console
.
log
(
"websocket on open"
);
});
wss
.
on
(
'message'
,
(
data
)
=>
{
const
response
=
JSON
.
parse
(
data
);
if
(
response
.
type
===
'ack'
){
const
id
=
response
.
id
;
for
(
const
symbol
of
symbols
){
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'subscribe'
,
'topic'
:
`/trade/
${
symbol
}
_TRADE`
}))
}
setInterval
(()
=>
{
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
},
40000
);
}
else
if
(
response
.
type
===
'subscribe'
){
callback
(
null
,
response
);
}
else
{
console
.
log
(
data
);
}
});
wss
.
on
(
'error'
,
(
error
)
=>
{
console
.
log
(
" websocket error:"
);
console
.
log
(
error
);
})
wss
.
on
(
'close'
,
()
=>
{
console
.
log
(
"websocket closed"
);
setTimeout
(()
=>
{
this
.
subscribeSymbolsAndTicker
(
symbols
,
callback
);
},
2000
);
})
}
// _openWs
(symbols,token, callback) {
//
const reqURL = `${wsUrl}?bulletToken=${token}&format=json&resource=api`;
//
const wss = new WebSocket(reqURL, {agent});
//
wss.on('open', () => {
//
console.log("websocket on open");
//
});
//
wss.on('message', (data) => {
//
const response = JSON.parse(data);
//
if(response.type === 'ack'){
//
const id = response.id;
//
for(const symbol of symbols){
//
wss.send(JSON.stringify({id,type:'subscribe','topic':`/trade/${symbol}_TRADE`}))
//
}
//
setInterval(()=>{
//
wss.send(JSON.stringify({id,type:'ping'}))
//
},40000);
//
}else if(response.type === 'subscribe'){
//
callback(null,response);
//
}else{
//
console.log(data);
//
}
//
});
//
wss.on('error', (error) => {
//
console.log(" websocket error:");
//
console.log(error);
//
})
//
wss.on('close', () => {
//
console.log("websocket closed");
//
setTimeout(() => {
//
this.subscribeSymbolsAndTicker(symbols, callback);
//
}, 2000);
//
})
//
}
subscribeSymbols
(
symbols
,
depth
,
callback
)
{
this
.
_publicRequest
(
'/v1/bullet/usercenter/loginUser'
,{
protocol
:
'websocket'
,
encrypt
:
true
},(
error
,
result
)
=>
{
...
...
@@ -100,43 +100,55 @@ class biboxApi {
console
.
log
(
"fetch ws token error"
);
this
.
subscribeSymbols
(
symbols
,
depth
,
callback
);
}
else
{
for
(
let
i
=
0
;
i
<
3
;
i
++
){
const
ip
=
IPs
.
length
>
i
?
IPs
[
i
]:
IPs
[
IPs
.
length
-
1
];
setTimeout
(()
=>
{
this
.
_openWs
(
result
,
symbols
,
depth
,
ip
,
callback
);
},
i
*
10000
);
}
}
});
}
_openWs
(
result
,
symbols
,
depth
,
ipAddress
,
callback
)
{
const
token
=
result
.
data
.
bulletToken
;
const
reqURL
=
`
${
wsUrl
}
?bulletToken=
${
token
}
&format=json&resource=api`
;
const
wss
=
new
WebSocket
(
reqURL
,
{
agent
});
const
wss
=
new
WebSocket
(
reqURL
,
{
agent
,
localAddress
:
ipAddress
});
wss
.
on
(
'open'
,
()
=>
{
console
.
log
(
"websocket on open"
);
});
wss
.
on
(
'message'
,
(
data
)
=>
{
const
response
=
JSON
.
parse
(
data
);
if
(
response
.
type
===
'ack'
)
{
if
(
response
.
type
===
'ack'
)
{
const
id
=
response
.
id
;
setInterval
(()
=>
{
if
(
wss
.
readyState
===
constants
.
SocketReadyStateOpen
)
{
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
setInterval
(()
=>
{
if
(
wss
.
readyState
===
constants
.
SocketReadyStateOpen
)
{
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
}
},
40000
);
for
(
const
symbol
of
symbols
)
{
},
40000
);
for
(
const
symbol
of
symbols
)
{
this
.
_subscribeSymbol
(
wss
,
id
,
symbol
,
depth
)
}
}
else
if
(
response
.
topic
&&
response
.
topic
.
startsWith
(
'/trade'
))
{
}
else
if
(
response
.
topic
&&
response
.
topic
.
startsWith
(
'/trade'
))
{
const
timeStamp
=
response
.
data
.
time
;
const
symbol
=
response
.
topic
.
replace
(
'/trade/'
,
''
).
replace
(
'_TRADE'
,
''
);
const
symbol
=
response
.
topic
.
replace
(
'/trade/'
,
''
).
replace
(
'_TRADE'
,
''
);
let
asks
=
totalOrderbook
[
symbol
].
asks
||
[];
let
bids
=
totalOrderbook
[
symbol
].
bids
||
[];
if
(
response
.
data
.
type
===
'SELL'
)
{
if
(
response
.
data
.
type
===
'SELL'
)
{
const
updateData
=
getUpdatedData
(
asks
,
response
);
asks
=
mergeDepth
(
asks
,
updateData
,
true
);
}
else
if
(
response
.
data
.
type
===
'BUY'
)
{
const
updateData
=
getUpdatedData
(
bids
,
response
);
}
else
if
(
response
.
data
.
type
===
'BUY'
)
{
const
updateData
=
getUpdatedData
(
bids
,
response
);
bids
=
mergeDepth
(
bids
,
updateData
,
false
);
}
totalOrderbook
[
symbol
].
bids
=
bids
;
totalOrderbook
[
symbol
].
asks
=
asks
;
const
ret
=
{
data
:
{
SELL
:
asks
,
BUY
:
bids
},
timestamp
:
timeStamp
,
symbol
:
symbol
}
callback
(
null
,
ret
);
}
else
{
// console.log(data);
callback
(
null
,
ret
);
}
// else {
// // console.log(data);
// }
});
wss
.
on
(
'error'
,
(
error
)
=>
{
console
.
log
(
"websocket error:"
);
...
...
@@ -149,8 +161,6 @@ class biboxApi {
},
2000
);
})
}
});
}
_subscribeSymbol
(
wss
,
id
,
symbol
,
depth
){
const
ipAddress
=
IPs
[
Math
.
round
(
Math
.
random
()
*
(
IPs
.
length
-
1
))];
...
...
@@ -162,7 +172,10 @@ class biboxApi {
return
;
}
const
data
=
result
.
data
;
totalOrderbook
[
symbol
]
=
{
asks
:
data
.
SELL
,
bids
:
data
.
BUY
};
const
oldData
=
totalOrderbook
[
symbol
];
if
(
oldData
&&
data
.
timestamp
>
oldData
.
timestamp
){
totalOrderbook
[
symbol
]
=
{
asks
:
data
.
SELL
,
bids
:
data
.
BUY
,
timestamp
:
data
.
timestamp
};
}
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'subscribe'
,
'topic'
:
`/trade/
${
symbol
}
_TRADE`
}))
});
}
...
...
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