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 {
...
@@ -47,52 +47,52 @@ class biboxApi {
this
.
allowRequest
=
true
;
this
.
allowRequest
=
true
;
}
}
subscribeSymbolsAndTicker
(
symbols
,
callback
)
{
//
subscribeSymbolsAndTicker(symbols, callback) {
this
.
_publicRequest
(
'/v1/bullet/usercenter/loginUser'
,{
protocol
:
'websocket'
,
encrypt
:
true
},(
error
,
result
)
=>
{
//
this._publicRequest('/v1/bullet/usercenter/loginUser',{protocol:'websocket',encrypt:true},(error,result)=>{
if
(
error
){
//
if(error){
console
.
log
(
"fetch ws token error"
);
//
console.log("fetch ws token error");
this
.
subscribeSymbolsAndTicker
(
symbols
,
callback
);
//
this.subscribeSymbolsAndTicker(symbols,callback);
}
else
{
//
}else{
const
token
=
result
.
data
.
bulletToken
;
//
const token = result.data.bulletToken;
this
.
_doSubscribe
(
symbols
,
token
,
callback
);
// this._openWs
(symbols,token,callback);
}
//
}
});
//
});
}
//
}
_doSubscribe
(
symbols
,
token
,
callback
)
{
// _openWs
(symbols,token, callback) {
const
reqURL
=
`
${
wsUrl
}
?bulletToken=
${
token
}
&format=json&resource=api`
;
//
const reqURL = `${wsUrl}?bulletToken=${token}&format=json&resource=api`;
const
wss
=
new
WebSocket
(
reqURL
,
{
agent
});
//
const wss = new WebSocket(reqURL, {agent});
wss
.
on
(
'open'
,
()
=>
{
//
wss.on('open', () => {
console
.
log
(
"websocket on open"
);
//
console.log("websocket on open");
});
//
});
wss
.
on
(
'message'
,
(
data
)
=>
{
//
wss.on('message', (data) => {
const
response
=
JSON
.
parse
(
data
);
//
const response = JSON.parse(data);
if
(
response
.
type
===
'ack'
){
//
if(response.type === 'ack'){
const
id
=
response
.
id
;
//
const id = response.id;
for
(
const
symbol
of
symbols
){
//
for(const symbol of symbols){
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'subscribe'
,
'topic'
:
`/trade/
${
symbol
}
_TRADE`
}))
//
wss.send(JSON.stringify({id,type:'subscribe','topic':`/trade/${symbol}_TRADE`}))
}
//
}
setInterval
(()
=>
{
//
setInterval(()=>{
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
//
wss.send(JSON.stringify({id,type:'ping'}))
},
40000
);
//
},40000);
}
else
if
(
response
.
type
===
'subscribe'
){
//
}else if(response.type === 'subscribe'){
callback
(
null
,
response
);
//
callback(null,response);
}
else
{
//
}else{
console
.
log
(
data
);
//
console.log(data);
}
//
}
});
//
});
wss
.
on
(
'error'
,
(
error
)
=>
{
//
wss.on('error', (error) => {
console
.
log
(
" websocket error:"
);
//
console.log(" websocket error:");
console
.
log
(
error
);
//
console.log(error);
})
//
})
wss
.
on
(
'close'
,
()
=>
{
//
wss.on('close', () => {
console
.
log
(
"websocket closed"
);
//
console.log("websocket closed");
setTimeout
(()
=>
{
//
setTimeout(() => {
this
.
subscribeSymbolsAndTicker
(
symbols
,
callback
);
//
this.subscribeSymbolsAndTicker(symbols, callback);
},
2000
);
//
}, 2000);
})
//
})
}
//
}
subscribeSymbols
(
symbols
,
depth
,
callback
)
{
subscribeSymbols
(
symbols
,
depth
,
callback
)
{
this
.
_publicRequest
(
'/v1/bullet/usercenter/loginUser'
,{
protocol
:
'websocket'
,
encrypt
:
true
},(
error
,
result
)
=>
{
this
.
_publicRequest
(
'/v1/bullet/usercenter/loginUser'
,{
protocol
:
'websocket'
,
encrypt
:
true
},(
error
,
result
)
=>
{
...
@@ -100,43 +100,55 @@ class biboxApi {
...
@@ -100,43 +100,55 @@ class biboxApi {
console
.
log
(
"fetch ws token error"
);
console
.
log
(
"fetch ws token error"
);
this
.
subscribeSymbols
(
symbols
,
depth
,
callback
);
this
.
subscribeSymbols
(
symbols
,
depth
,
callback
);
}
else
{
}
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
token
=
result
.
data
.
bulletToken
;
const
reqURL
=
`
${
wsUrl
}
?bulletToken=
${
token
}
&format=json&resource=api`
;
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'
,
()
=>
{
wss
.
on
(
'open'
,
()
=>
{
console
.
log
(
"websocket on open"
);
console
.
log
(
"websocket on open"
);
});
});
wss
.
on
(
'message'
,
(
data
)
=>
{
wss
.
on
(
'message'
,
(
data
)
=>
{
const
response
=
JSON
.
parse
(
data
);
const
response
=
JSON
.
parse
(
data
);
if
(
response
.
type
===
'ack'
)
{
if
(
response
.
type
===
'ack'
)
{
const
id
=
response
.
id
;
const
id
=
response
.
id
;
setInterval
(()
=>
{
setInterval
(()
=>
{
if
(
wss
.
readyState
===
constants
.
SocketReadyStateOpen
)
{
if
(
wss
.
readyState
===
constants
.
SocketReadyStateOpen
)
{
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
wss
.
send
(
JSON
.
stringify
({
id
,
type
:
'ping'
}))
}
}
},
40000
);
},
40000
);
for
(
const
symbol
of
symbols
)
{
for
(
const
symbol
of
symbols
)
{
this
.
_subscribeSymbol
(
wss
,
id
,
symbol
,
depth
)
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
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
asks
=
totalOrderbook
[
symbol
].
asks
||
[];
let
bids
=
totalOrderbook
[
symbol
].
bids
||
[];
let
bids
=
totalOrderbook
[
symbol
].
bids
||
[];
if
(
response
.
data
.
type
===
'SELL'
)
{
if
(
response
.
data
.
type
===
'SELL'
)
{
const
updateData
=
getUpdatedData
(
asks
,
response
);
const
updateData
=
getUpdatedData
(
asks
,
response
);
asks
=
mergeDepth
(
asks
,
updateData
,
true
);
asks
=
mergeDepth
(
asks
,
updateData
,
true
);
}
else
if
(
response
.
data
.
type
===
'BUY'
)
{
}
else
if
(
response
.
data
.
type
===
'BUY'
)
{
const
updateData
=
getUpdatedData
(
bids
,
response
);
const
updateData
=
getUpdatedData
(
bids
,
response
);
bids
=
mergeDepth
(
bids
,
updateData
,
false
);
bids
=
mergeDepth
(
bids
,
updateData
,
false
);
}
}
totalOrderbook
[
symbol
].
bids
=
bids
;
totalOrderbook
[
symbol
].
bids
=
bids
;
totalOrderbook
[
symbol
].
asks
=
asks
;
totalOrderbook
[
symbol
].
asks
=
asks
;
const
ret
=
{
data
:
{
SELL
:
asks
,
BUY
:
bids
},
timestamp
:
timeStamp
,
symbol
:
symbol
}
const
ret
=
{
data
:
{
SELL
:
asks
,
BUY
:
bids
},
timestamp
:
timeStamp
,
symbol
:
symbol
}
callback
(
null
,
ret
);
callback
(
null
,
ret
);
}
else
{
// console.log(data);
}
}
// else {
// // console.log(data);
// }
});
});
wss
.
on
(
'error'
,
(
error
)
=>
{
wss
.
on
(
'error'
,
(
error
)
=>
{
console
.
log
(
"websocket error:"
);
console
.
log
(
"websocket error:"
);
...
@@ -149,8 +161,6 @@ class biboxApi {
...
@@ -149,8 +161,6 @@ class biboxApi {
},
2000
);
},
2000
);
})
})
}
}
});
}
_subscribeSymbol
(
wss
,
id
,
symbol
,
depth
){
_subscribeSymbol
(
wss
,
id
,
symbol
,
depth
){
const
ipAddress
=
IPs
[
Math
.
round
(
Math
.
random
()
*
(
IPs
.
length
-
1
))];
const
ipAddress
=
IPs
[
Math
.
round
(
Math
.
random
()
*
(
IPs
.
length
-
1
))];
...
@@ -162,7 +172,10 @@ class biboxApi {
...
@@ -162,7 +172,10 @@ class biboxApi {
return
;
return
;
}
}
const
data
=
result
.
data
;
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`
}))
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