Error during WebSocket handshake: Unexpected response code: 502

I am making a small websocket-based database on Glitch. My testing was working perfectly at first, but when I ran my test and the client reloaded, I received the error message shown above. I tried turning new parts of the code into comments to fix this, but it seems to be a permanent issue. Here is my code:
On the server:`const WebSocket = require(‘ws’);

const wss = new WebSocket.Server({ port: 8080 });
var fs = require(‘fs’);
var data = ;
var i;

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function readWriteSync(val) {
var dat = fs.readFileSync(‘stuff.txt’, ‘utf-8’);
var newValue = dat+=val+’ ';
fs.writeFileSync(‘stuff.txt’, newValue, ‘utf-8’);
}

function deleter(){fs.writeFileSync(‘stuff.txt’, ‘’, ‘utf-8’);}
async function reloader(){
fs.writeFileSync(‘reload.txt’, ‘reload’, ‘utf-8’);
await sleep(100);
fs.writeFileSync(‘reload.txt’, ‘’, ‘utf-8’);
}

wss.on(‘connection’, (ws) => {
ws.on(‘message’, (message) => {
console.log(Received message => ${message})
if(message==‘delete’){deleter()}else{
data.push(message);deleter();readWriteSync(message);reloader();}
if(data.length>=10){
for(i=0;i<data.length;i++){
readWriteSync(data[i]);
}
reloader();
}
})
ws.send(fs.readFileSync(‘stuff.txt’, ‘utf-8’));
setInterval(function(){ if(fs.readFileSync(‘reload.txt’, ‘utf-8’)==‘reload’){ws.send(‘reload’)} }, 50);
})On the clienthmspokemondraft
Show
script.js
const url = ‘wss://hmsdraftserver.glitch.me’
const connection = new WebSocket(url)

connection.onopen = () => {
console.log(‘Hi’)
}

connection.onerror = (error) => {
console.log(WebSocket error: ${error})
}

connection.onmessage = (e) => {
console.log(e.data)
if(e.data==‘reload’){document.location.reload()}
}

function send(stuff){
connection.send(stuff)
}

var data;
var v = document.getElementsByTagName(‘input’)[0];
var b = document.getElementsByTagName(‘button’)[0];

function buttonpress(){
data = v.value;
send(data);
}
`

Hi @camelConvention can you provide your project name for someone to take a closer look? Off the top of my head I suspect the issue might be related to the fact that you’re using port 8080 for your websocket port - that port won’t be accessible to anything outside of the project’s container. Only the port related to process.env.PORT will be open outside of the container at any given time, and that will be proxied through ports 80 / 443 externally. That’s consistent with the 502 status code, I believe.

Sorry, I fixed the problem, but I was unable to solve my topic because it was hidden by the spam filter bot. Thanks for your support though!

1 Like

Hello please i’ve same problem could you hemlp: me on how did you solve it

Hi @33sKamal,

I’m sorry that you are having a similar issue! Can you share the name of your project where this is happening?

im getting
backoffice.js:34947 WebSocket connection to ‘wss://socket.33sweb.com/app/laravel_package_key_id_2?protocol=7&client=js&version=5.0.3&flash=false’ failed: Error during WebSocket handshake: Unexpected response code: 502

@33sKamal thanks for that. Can you also tell me the name of the project so I can take a closer look? If you prefer, you can tell me in a direct message.

Hi, I am also having the same error , when i open my project https://star-hollow-bulb.glitch.me/.

I also get the same " [Error during WebSocket handshake: Unexpected response code: 502]" .

It was working fine initially but it suddenly stopoed
Please help.

It seems like you are receiving this due to the current maintenance I suppose. Please have a look at https://status.glitch.com to see the current Glitch status :slight_smile:

The incident is resolved I think according to the timelines but I am still getting the issue

Project hosting is not currently working according to the status page, I’m sure Glitch will fix this as fast as possible.

3 Likes

Having the same issue with websocket connection to glitch also. While in the past it worked flawlessy, now connection just fails with status 502. What’s going on?