Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Websocket connection undefined #252

Merged
merged 17 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/httpServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var http = require("http");
const http = require("http");
const { rpcError } = require("./utils/to");

module.exports = function(provider, logger) {
var server = http.createServer(function(request, response) {
Expand Down Expand Up @@ -50,6 +51,14 @@ module.exports = function(provider, logger) {
logger.log(payload.method);
}

// http connections do not support subscriptions
if (payload.method === "eth_subscribe" || payload.method === "eth_unsubscribe") {
headers["Content-Type"] = "application/json";
response.writeHead(400, headers);
response.end(rpcError(payload.id, -32000, "notifications not supported"));
break;
}

provider.send(payload, function(_, result) {
headers["Content-Type"] = "application/json";
response.writeHead(200, headers);
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/to.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,16 @@ module.exports = {
}
var bufVal = utils.toBuffer(val);
return utils.bufferToInt(bufVal);
},

rpcError: function(id, code, msg) {
return JSON.stringify({
jsonrpc: "2.0",
id: id,
error: {
code: code,
message: msg
}
});
}
};
6 changes: 5 additions & 1 deletion lib/webSocketServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,9 @@ ConnectionManager.prototype._logPayload = function(payload) {
};

ConnectionManager.prototype._updateSubscriptions = function(notification) {
this.connectionsBySubscriptionId[notification.params.subscription].connection.send(JSON.stringify(notification));
const subscription = this.connectionsBySubscriptionId[notification.params.subscription];
// Safety check for subscription/connection.
if (subscription) {
subscription.connection.send(JSON.stringify(notification));
}
};
Loading