From af5b772a201839e828fd18755ee90ef0bd781221 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 27 Nov 2018 13:36:09 -0500 Subject: [PATCH] add websocket support for remix-sim --- remix-simulator/package.json | 1 + remix-simulator/src/server.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/remix-simulator/package.json b/remix-simulator/package.json index 86f46b0e53..92c856dfee 100644 --- a/remix-simulator/package.json +++ b/remix-simulator/package.json @@ -22,6 +22,7 @@ "body-parser": "^1.18.2", "color-support": "^1.1.3", "express": "^4.16.3", + "express-ws": "^4.0.0", "fast-async": "^6.3.7", "merge": "^1.2.0", "remix-lib": "0.3.13", diff --git a/remix-simulator/src/server.js b/remix-simulator/src/server.js index 1171b8826d..f130f3bbb2 100644 --- a/remix-simulator/src/server.js +++ b/remix-simulator/src/server.js @@ -1,6 +1,7 @@ const express = require('express') const bodyParser = require('body-parser') const app = express() +const expressWs = require('express-ws')(app) const Provider = require('./provider') const log = require('./utils/logs.js') @@ -22,4 +23,15 @@ app.use(function (req, res) { }) }) +app.ws('/', function(ws, req) { + ws.on('message', function(msg) { + provider.sendAsync(JSON.parse(msg), (err, jsonResponse) => { + if (err) { + ws.send({error: err}); + } + ws.send(JSON.stringify(jsonResponse)); + }) + }); +}); + app.listen(8545, () => log('Remix Simulator listening on port 8545!'))