节点服务器不使用 express-ws
Posted
技术标签:
【中文标题】节点服务器不使用 express-ws【英文标题】:node server not working with express-ws 【发布时间】:2016-11-23 12:30:23 【问题描述】:我有一个节点 WebServer 能够通过 Websockets 与浏览器(比如 browserInstance)和 linux 终端(比如 ProxyInstance)进行通信。网络服务器的工作是将数据从终端移交给 WebBrowser,反之亦然。请在下面找到 server.js 代码:
var express = require('express');
var expressWs = require('express-ws');
var expressWs = expressWs(express());
var app = expressWs.app;
var appForpage = express();
var browserInstance;
var ProxyInstance;
var browserCounter = 0;
var ProxyCounter = 0;
app.ws('/fromBrowser', function(ws, req, next)
console.log("~~~~~~~~~~~~BROWSER");
if(browserCounter == 1)
ws.on('message', function(msg)
console.log("Messagae from Browser :", msg);
ProxyInstance.send(msg);
);
else
browserInstance = ws;
ws.on('message', function(msg)
console.log("Message from Browser :", msg);
ProxyInstance.send(msg);
);
browserCounter = 1;
ws.on('close', function()
console.log("Ws Connection closed");
);
//next();
);
app.ws('/fromProxy', function(ws, req, next)
console.log("~~~~~~~~~~~~PROXY");
if(ProxyCounter == 0)
ProxyInstance = ws;
ProxyCounter = 1;
else if(browserCounter == 1)
ws.on('message', function(msg)
console.log("Message from Proxy: ", msg);
browserInstance.send(msg);
);
ws.on('close', function()
console.log("Ws Connection closed");
);
//next();
);
appForpage.use(express.static(__dirname + '/public/')); // index.html resides in public directory
appForpage.listen(5000)
app.listen(3000)
首先,我正在创建从代理到网络服务器(/fromProxy)然后从浏览器(/fromBrowser)的 ws 连接。连接已成功创建。当我尝试通过网络服务器将数据从浏览器发送到代理时,它工作正常。当尝试通过 WebServer 与浏览器通信时,返回代理端的第一条消息,此消息失败。我没有收到来自 Proxy 的任何消息。我需要以相同的顺序运行各个端(首先是代理,然后是浏览器..)。
我只是节点的初学者。对于我的案例,我没有在互联网上找到任何示例。我在这里错过了什么?
【问题讨论】:
【参考方案1】:这真的很傻:(。我还没有从代理注册我的消息回调。请在下面找到工作代码。
var express = require('express');
var expressWs = require('express-ws');
var expressWs = expressWs(express());
var app = expressWs.app;
var appForpage = express();
var browserInstance;
var ProxyInstance;
var browserCounter = 0;
var ProxyCounter = 0;
app.ws('/fromBrowser', function(ws, req, next)
console.log("~~~~~~~~~~~~BROWSER");
if(browserCounter == 1)
ws.on('message', function(msg)
console.log("Messagae from Browser :", msg);
ProxyInstance.send(msg);
);
else
browserInstance = ws;
ws.on('message', function(msg)
console.log("Message from Browser :", msg);
ProxyInstance.send(msg);
);
browserCounter = 1;
ws.on('close', function()
console.log("Ws Connection closed");
);
//next();
);
app.ws('/fromProxy', function(ws, req, next)
console.log("~~~~~~~~~~~~PROXY");
if(ProxyCounter == 0)
ProxyInstance = ws;
ProxyCounter = 1;
/*This is the place where i went wrong. damnn..*/
ws.on('message', function(msg)
console.log("Message from Proxy: ", msg);
browserInstance.send(msg);
);
ws.on('close', function()
console.log("Ws Connection closed");
);
//next();
);
appForpage.use(express.static(__dirname + '/public/')); // index.html resides in public directory
appForpage.listen(5000)
app.listen(3000)
【讨论】:
以上是关于节点服务器不使用 express-ws的主要内容,如果未能解决你的问题,请参考以下文章
express-ws 中间件无法正确创建 websocket 服务器
在不使用表单的情况下,从 javascript 应用程序从节点服务器获取 JWT 令牌的最佳安全方法是啥?