Я пытаюсь установить соединение между реагирующим клиентом и экспресс-сервером с помощью веб-сокетов. Каждый раз, когда я пытаюсь это сделать, я получаю сообщение об ошибке. Я думаю, что я что-то упускаю.
Код сервера:
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.info("The Server is lisening on port 9000.")
});
theWebSocketServer.on('connection', function connection(msg) {
console.info("CONNECTION CREATED");
websocket.on('message', function incoming(message) {
});
});
Код клиента:
let wsConnection = new WebSocket("ws://localhost:9000");
wsConnection.onopen = function(eventInfo) {
console.info("Socket connection is open!");
}
Ошибка:
if (!this.options.verifyClient(info)) вернуть 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
});
Я полностью удалил часть «verifyClient: true». Теперь он работает просто отлично!