Javascript Can';t在react客户端和express server之间建立websocket连接

Javascript Can';t在react客户端和express server之间建立websocket连接,javascript,websocket,ws,Javascript,Websocket,Ws,我正在尝试使用WebSocket在react客户端和express服务器之间建立连接。每次我尝试这个,我都会出错。我想我错过了什么 服务器代码: var http = require('http'); var ws = require('ws'); var theHttpServer = http.createServer(); var theWebSocketServer = new ws.Server({ server: theHttpServer, verifyClien

我正在尝试使用WebSocket在react客户端和express服务器之间建立连接。每次我尝试这个,我都会出错。我想我错过了什么

服务器代码:

var http = require('http');
var ws = require('ws');

var theHttpServer = http.createServer();
var theWebSocketServer = new ws.Server({
    server: theHttpServer,
    verifyClient: true
});

theHttpServer.on('request', app);
theHttpServer.listen(9000,
    function () {
        console.log("The Server is lisening on port 9000.")
    });

theWebSocketServer.on('connection', function connection(msg) {
    console.log("CONNECTION CREATED");

    websocket.on('message', function incoming(message) {
    });
});
客户端代码:

let wsConnection = new WebSocket("ws://localhost:9000");

wsConnection.onopen = function(eventInfo) {
    console.log("Socket connection is open!");
}
错误:

如果(!this.options.verifyClient(info))返回abortHandshake(套接字,401); ^


TypeError:this.options.verifyClient不是一个函数

您要传递的
verifyClient
不是一个函数,而是一个布尔值。您可能希望将此更改为:

function verifyClient(info) { 
  // ...Insert your validation code here 
};
var theWebSocketServer = new ws.Server({
  server: theHttpServer,
  verifyClient: verifyClient
});
我完全删除了“verifyClient:true”部分。现在它工作得很好!