Avoid some exceptions
This commit is contained in:
parent
578e2debc6
commit
03ec966a02
28
index.js
28
index.js
|
@ -157,14 +157,24 @@ WebSocketConnection.prototype.boot = function () {
|
||||||
this.reconnect();
|
this.reconnect();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WebSocketConnection.prototype.trapexit = function () {
|
||||||
|
this.forceclose();
|
||||||
|
};
|
||||||
|
|
||||||
WebSocketConnection.prototype.isConnected = function () {
|
WebSocketConnection.prototype.isConnected = function () {
|
||||||
return this.sock && this.sock.readyState === this.sock.OPEN;
|
return this.sock && this.sock.readyState === this.sock.OPEN;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
WebSocketConnection.prototype.safeSend = function (m) {
|
||||||
|
try {
|
||||||
|
if (this.isConnected()) { this.sock.send(m); }
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Trapped exn while sending", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
WebSocketConnection.prototype.sendLocalRoutes = function () {
|
WebSocketConnection.prototype.sendLocalRoutes = function () {
|
||||||
if (this.isConnected()) {
|
this.safeSend(JSON.stringify(encodeEvent(updateRoutes(this.localRoutes))));
|
||||||
this.sock.send(JSON.stringify(encodeEvent(updateRoutes(this.localRoutes))));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocketConnection.prototype.handleEvent = function (e) {
|
WebSocketConnection.prototype.handleEvent = function (e) {
|
||||||
|
@ -192,11 +202,9 @@ WebSocketConnection.prototype.handleEvent = function (e) {
|
||||||
&& m[0] === this.label
|
&& m[0] === this.label
|
||||||
&& typeof(m[1]) === "number")
|
&& typeof(m[1]) === "number")
|
||||||
{
|
{
|
||||||
if (this.isConnected()) {
|
var encoded = JSON.stringify(encodeEvent(sendMessage(m[2], m[1], e.isFeedback)));
|
||||||
var encoded = JSON.stringify(encodeEvent(sendMessage(m[2], m[1], e.isFeedback)));
|
if (this.deduplicator.accept(encoded)) {
|
||||||
if (this.deduplicator.accept(encoded)) {
|
this.safeSend(encoded);
|
||||||
this.sock.send(encoded);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -234,7 +242,7 @@ WebSocketConnection.prototype.onmessage = function (wse) {
|
||||||
|
|
||||||
var j = JSON.parse(wse.data);
|
var j = JSON.parse(wse.data);
|
||||||
if (j === "ping") {
|
if (j === "ping") {
|
||||||
this.sock.send(JSON.stringify("pong"));
|
this.safeSend(JSON.stringify("pong"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue