簡體   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