ping fixes

This commit is contained in:
ntr
2019-05-13 18:05:57 +10:00
parent 4f70354b94
commit 577ffd6421
3 changed files with 34 additions and 11 deletions
+14 -3
View File
@@ -28,14 +28,18 @@ function createSocket(events) {
// -------------
// Outgoing
// -------------
let ping = Date.now();
function send(msg) {
console.log('outgoing msg', msg);
ping = Date.now();
msg.token = account && account.token && account.token;
ws.send(cbor.encode(msg));
}
let ping;
function sendPing() {
ping = Date.now();
send({ method: 'ping', params: {} });
}
function sendAccountLogin(name, password) {
send({ method: 'account_login', params: { name, password } });
}
@@ -230,6 +234,11 @@ function createSocket(events) {
events.setVboxInfo(info);
}
function pong() {
events.setPing(Date.now() - ping);
setTimeout(sendPing, 1000);
}
// -------------
// Setup
// -------------
@@ -248,6 +257,7 @@ function createSocket(events) {
zone_close: res => console.log(res),
instance_state: instanceState,
vbox_info: vboxInfo,
pong,
};
function logout() {
@@ -278,7 +288,6 @@ function createSocket(events) {
const { method, params } = res;
console.log(res);
events.setPing(Date.now() - ping);
// check for error and split into response type and data
if (res.err) return errHandler(res.err);
@@ -303,6 +312,8 @@ function createSocket(events) {
sendAccountCryps();
}
sendPing();
return true;
});