1

我正在尝试使用 websockets 在反应客户端和快速服务器之间建立连接。每次我尝试这个我都会得到一个错误。我想我错过了一些东西。

服务器代码:

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!");
}

错误:

if (!this.options.verifyClient(info)) return abortHandshake(socket, 401); ^

TypeError:this.options.verifyClient 不是函数

4

1 回答 1

1

verifyClient作为布尔值而不是函数传递。您可能想要做的是将其更改为:

function verifyClient(info) { 
  // ...Insert your validation code here 
};
var theWebSocketServer = new ws.Server({
  server: theHttpServer,
  verifyClient: verifyClient
});
于 2019-04-09T13:47:25.637 回答