无法在反应客户端和快速服务器之间建立 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)) return abortHandshake(socket, 401);
^
TypeError: this.options.verifyClient 不是函数
您将 verifyClient
作为布尔值而不是函数传递。您可能想要做的是将其更改为:
function verifyClient(info) {
// ...Insert your validation code here
};
var theWebSocketServer = new ws.Server({
server: theHttpServer,
verifyClient: verifyClient
});
我正在尝试使用 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 不是函数
您将 verifyClient
作为布尔值而不是函数传递。您可能想要做的是将其更改为:
function verifyClient(info) {
// ...Insert your validation code here
};
var theWebSocketServer = new ws.Server({
server: theHttpServer,
verifyClient: verifyClient
});