连接到 websockets NodeJS Express 服务器时出错

Posted

技术标签:

【中文标题】连接到 websockets NodeJS Express 服务器时出错【英文标题】:Error connecting to websockets NodeJS Express server 【发布时间】:2020-02-14 16:40:21 【问题描述】:

我有一个配置了 websockets 的快速服务器:

/tmp/ws-test $ cat httpServer.js
const express = require('express');
const WebSocket = require('ws');

const app = express();
const port = 8080;

app.get('/', function(req, res, next) 
  return res.send('Hello World!');
);

const wss = new WebSocket.Server( server: app );

wss.on('connection', function connection(ws) 
  ws.on('message', function incoming(message) 
    console.log('received: %s', message);
  );
  ws.send('something from server');
);

app.listen(port, function(err) 
  if (err) 
    throw err;
  
  console.log(`listening on port $port!`);
);

它按预期运行:

/tmp/ws-test $ node httpServer.js
listening on port 8080!

我有一个客户:

/tmp/ws-test $ cat client.js
const WebSocket = require('ws');

const ws = new WebSocket('ws://localhost:8080');

ws.on('open', function open() 
  ws.send('something from client');
);

ws.on('message', function incoming(data) 
  console.log(`received: $data`);
);

错误:

/tmp/ws-test $ node client.js
events.js:180
      throw er; // Unhandled 'error' event
      ^

Error: Unexpected server response: 200
    at ClientRequest.<anonymous> (/private/tmp/ws-test/node_modules/ws/lib/websocket.js:579:7)
    at ClientRequest.emit (events.js:203:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:575:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:17)
    at Socket.socketOnData (_http_client.js:449:22)
    at Socket.emit (events.js:203:13)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:276:11)
    at Socket.Readable.push (_stream_readable.js:210:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:166:17)
Emitted 'error' event at:
    at abortHandshake (/private/tmp/ws-test/node_modules/ws/lib/websocket.js:697:15)
    at ClientRequest.<anonymous> (/private/tmp/ws-test/node_modules/ws/lib/websocket.js:579:7)
    [... lines matching original stack trace ...]
    at Socket.Readable.push (_stream_readable.js:210:10)

在客户端中,我还尝试使用 http 进行 Web 套接字连接。

const ws = new WebSocket('http://localhost:8080');

但它会引发同样的错误。

为什么客户端在连接服务器时出错?

【问题讨论】:

【参考方案1】:

您应该将http 服务器实例传递给WebSocket.Server 构造函数,而不是express 应用程序的实例:

const express = require('express');
const WebSocket = require('ws');
const http = require('http');

const app = express();
const port = 8080;

const server = http.createServer(app);

app.get('/', function(req, res, next) 
    return res.send('Hello World!');
);

const wss = new WebSocket.Server( server );

wss.on('connection', function connection(ws) 
    ws.on('message', function incoming(message) 
        console.log('received: %s', message);
    );
    ws.send('something from server');
);

server.listen(port, function(err) 
    if (err) 
        throw err;
    
    console.log(`listening on port $port!`);
);

【讨论】:

就是这个原因,我添加了你的修改,连接成功。谢谢!

以上是关于连接到 websockets NodeJS Express 服务器时出错的主要内容,如果未能解决你的问题,请参考以下文章

连接到 websockets NodeJS Express 服务器时出错

如何在NodeJS中代理websocket连接到其他websockets

通过 PHP 连接到 socket.io(nodejs)

将 NestJS 连接到 websocket 服务器

Socket.IO-Client-Swift 未连接到 nodejs socket.io 服务器

nodejs Websocket服务器 - 检测到可能的EventEmitter内存泄漏