cex.ionetworking套接字身份validation时间戳错误

我目前正试图连接到CEX.IO比特币交易所的websocket。 WebSocket连接是可以的,但在身份validation时,我有错误:时间戳不在20秒范围内。 我不知道这个错误是什么。

testing用例1和2的createSignature是可以的( https://cex.io/websocket-api#authentication )。

计算签名和请求参数的代码

const WebSocket = require('ws'); const cexioWs = new WebSocket( 'wss://ws.cex.io/ws/', { perMessageDeflate: false } ); function createAuthRequest(apiKey, apiSecret) { let curTime = Math.floor(Date.now() / 1000); let hmac = crypto.createHmac('sha256', apiSecret); hmac.update(curTime.toString()); hmac.update(apiKey); let args = { e: "auth", auth: { key: apiKey, signature: hmac.digest('hex'), //createSignature(curTime, apiKey, apiSecret), timestamp: curTime } }; let authMessage = JSON.stringify(args); console.log(args); return authMessage; } cexioWs.on('message', (mess, error) => { //console.log("connected"); console.log("cexio message"); console.log(mess); let JSONMess = JSON.parse(mess); if (JSONMess.e === "connected") { cexioWs.send(createAuthRequest(key, secret)); cexioWs.send(JSON.stringify({ e: "subscribe", roomss: [ "tickers" ] })); } if (JSONMess.e === "ping") { console.log("pong message"); cexioWs.send(JSON.stringify({e: "pong"})); } }); 

这是工作代码:

 const crypto = require('crypto') const WebSocket = require('ws') var apiKey = '' var apiSecret = '' const cexioWs = new WebSocket('wss://ws.cex.io/ws/', {perMessageDeflate: false }); function createSignature(timestamp, apiKey, apiSecret){ var hmac = crypto.createHmac('sha256', apiSecret ); hmac.update( timestamp + apiKey ); return hmac.digest('hex'); } function createAuthRequest(apiKey, apiSecret ){ var timestamp = Math.floor(Date.now() / 1000); var args = { e: 'auth', auth: { key: apiKey, signature: createSignature(timestamp, apiKey, apiSecret), timestamp: timestamp } }; var authMessage = JSON.stringify( args ); return authMessage; } cexioWs.on('message', (mess, error) => { console.log("cexio message"); console.log(mess); let JSONMess = JSON.parse(mess); if (JSONMess.e === "connected") { cexioWs.send(createAuthRequest(apiKey, apiSecret)); cexioWs.send(JSON.stringify({ e: "subscribe", rooms: [ "tickers" ] })); } if (JSONMess.e === "ping") { console.log("pong message"); cexioWs.send(JSON.stringify({e: "pong"})); } }); 

不知道这是否有帮助,但我有两天相同的问题,检查了一切,然后我检查,代码看起来非常好。 后来我查了一下实际收到的时间,并与互联网时间进行了比较。 我的电脑时间比互联网时间提前了4分钟,我的设置closures了“从互联网更新时间”。

在同步我的电脑与互联网的时间后,我跑了脚本,它的工作完美。

道德的故事,确保你的电脑的时间和互联网的时间是一样的。 祝你好运!