繁体   English   中英

无法在反应客户端和快速服务器之间建立 websocket 连接

[英]Can't make a websocket connection between react client and express server

我正在尝试使用 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)) 返回 abortHandshake(socket, 401); ^

类型错误:this.options.verifyClient 不是函数

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

function verifyClient(info) { 
  // ...Insert your validation code here 
};
var theWebSocketServer = new ws.Server({
  server: theHttpServer,
  verifyClient: verifyClient
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM