当服务器仅在端口80上发送消息时,WebSocket连接关闭

时间:2016-09-22 14:51:07

标签: javascript node.js websocket

我有一个运行Express的简单本地Nodejs服务器。我正在使用express-ws来设置WebSocket端点。客户端发送消息正常,服务器接收消息,但当服务器尝试发回消息时,连接关闭,客户端永远不会收到消息。

这只发生在端口80上。连接在端口3000,8080,443上保持打开状态,客户端收到服务器发回的消息。

app.js

const express = require('express');
const path = require('path');
const app = express();
const expressWs = require('express-ws')(app);

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res, next){
  res.send(`<script src="js/client.js"></script>`);
});

app.ws('/', function(ws, req) {
  ws.on('message', function(msg) {
    console.log(msg);
    ws.send(msg); //The connection doesn't close with this commented out
  });
});

app.listen(80);

client.js

const ws = new WebSocket(`ws://${window.location.host}`);

ws.onopen = function(e){
  console.log("WebSocket connected");
    ws.send("testing 1");
    ws.send("testing 2");
}

ws.onmessage = function(msg){
  console.log(msg);
}

我很茫然。任何帮助将不胜感激。

0 个答案:

没有答案