Home > database >  how to connect via WebSocket running on Ubuntu server
how to connect via WebSocket running on Ubuntu server

Time:02-01

Im have hetzner server on which im trying to run WebSocket. Unfortunately I got stack so here is my code from test.js)

const https = require('https');
const fs = require('fs');
const ws = require('ws');

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem'),
};

let server = http.createServer(options, (req, res) => {
    console.log(req);
    res.writeHead(200);
    res.end();
});

server.addListener('upgrade', (req, res, head) => console.log('UPGRADE:', req.url));
server.on('error', (err) => console.error(err));
server.listen(8080, () => console.log('started on 8080'));

const wss = new ws.Server({ server, path: '/echo' });
wss.on('connection', (ws) => {
    console.log('client connected');
    ws.send('Hello');
    ws.on('message', (data) => ws.send('Receive: '   data));
});
wss.on('error', (e) => console.log(e));
* I got this code from some sources

After running the server I got message in console started on 8080. Nothing else... I tried to test it but I always got errors with no code. It looks like service where I tested it cannot find WebSocket. It is possible that problem actually in connecting to the serer.

Im not shure which path string should I use. I have hostname ubuntu-s**** and server IP 49.****. I did lots of attempts with 'wss://ubuntu-s***:8080/echo' and 'wss://49.***:8080/echo' but none of them gave any result

I still have no messages in console that cliend tried to connect :( Moreover I tried to run it on the local server (of course I removed SSL sertificates connecthion and changed server protocol to HTTP) and it works perfectly!!!

Thanks a lot for urs replies

UPD: message I got when trying to connect ws WebSocket connection to 'ws://49.***:8080/echo' failed:

CodePudding user response:

First double check if the IP address is correct then change protocol to HTTP because you are using it on top of HTTP:

http://49.***:8080/echo

CodePudding user response:

I didnt open the port 8080. That was actual problem)

  •  Tags:  
  • Related