502 Server Error

Hi I’ve been setting up this simple server and client and it’s been going all well for me. I recently added some things not related to the server in a separate file and it’s still fine. Then out of nowhere

Error during WebSocket handshake: Unexpected response code: 502

How do I fix it?
The client is untouched; the only modifications I made was to the server.

const WebSocket = require(‘ws’);
const wss = new WebSocket.Server({ port: 8080 });
wss.on(‘connection’, function a(socket) {

var server = http.createServer(function(request, response) {

server.listen(process.env.PORT || 8080, function httpListening() {
console.log((new Date()) + ". \nServer turned on, listening on port "+server.address().port + “.”)

Any help would be appreciated. I’m very new to Websocket. Thanks!

How long does it stay up until it gives you that error?

Could you set the port to 3000?

Yeah, I tried. It gave me

Error: listen EADDRINUSE: address already in use :::3000

Could this help?

It’s kinda weird. when i open the server, sometimes it gives me the error, and sometimes it does. But if it didn’t, it’s fine until the server restarts.

Thanks! I changed new Websocket({port:3000}) to new Websocket({server:server}) and it worked.

1 Like

Yay! Glad it worked out for you.