我正在尝试使用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(socket,401); ^
TypeError:this.options.verifyClient不是函数
答案 0 :(得分:1)
您将verifyClient
作为布尔值而不是函数进行传递。您可能想要做的就是将其更改为:
function verifyClient(info) {
// ...Insert your validation code here
};
var theWebSocketServer = new ws.Server({
server: theHttpServer,
verifyClient: verifyClient
});