1

I'm trying to make a connection between a react client and an express server with websockets. Every time I try this i get an error. I think I'm missing something.

Server code:

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) {
    });
});

Client code:

let wsConnection = new WebSocket("ws://localhost:9000");

wsConnection.onopen = function(eventInfo) {
    console.log("Socket connection is open!");
}

The error:

if (!this.options.verifyClient(info)) return abortHandshake(socket, 401); ^

TypeError: this.options.verifyClient is not a function

1 Answer 1

1

You're passing verifyClient as a boolean, not a function. What you would maybe want to do is change this to:

function verifyClient(info) { 
  // ...Insert your validation code here 
};
var theWebSocketServer = new ws.Server({
  server: theHttpServer,
  verifyClient: verifyClient
});
Sign up to request clarification or add additional context in comments.

1 Comment

I totally removed the "verifyClient: true" part. Now it works just fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.