Hi
I’ve been trying to setup a simple websocket server on Glitch. It worked without problems about 6 months ago, but the same code now gives me a strange error code 426 Upgrade error.
const WebSocket = require('ws');
const PORT = 5000;
const wsServer = new WebSocket.Server({
port: PORT
});
wsServer.on('connection', function (socket) {
// Some feedback on the console
console.log("A client just connected");
// Attach some behavior to the incoming socket
socket.on('message', function (msg) {
console.log("Received message from client: " + msg);
// socket.send("Take this back: " + msg);
// Broadcast that message to all connected clients except sender
wsServer.clients.forEach(function (client) {
if (client !== socket) {
client.send(msg);
}
});
});
socket.on('close', function () {
console.log('Client disconnected');
})
});
console.log( (new Date()) + " Server is listening on port " + PORT);
I tried it another way too, but the result is, more or less, the same.
const express = require("express");
const app = express();
const http = require('http');
const server = http.createServer(app);
server.listen(3000);
app.get("/", (req, res) => res.send('x'));
// WebSocket
const WebSocket = require('ws');
const wss = new WebSocket.Server({server});
wss.on('connection', (ws, req) => console.log('new connection'));
This code gives me error code 200, but again with the Upgrade error. While I know that this is due to wrong handling of https and wss connection I’m more than confused.
Added the header for user-agent, I’m trying to connect via Postman, C#, Node clients, but no luck so far using wss://unity-webrtc-signaling.glitch.me. Anyone knows what may be the problem here?
br,
Max