Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 5956

How to use websockets with node.js on ubuntu?

$
0
0

Hey guys I installed Linux for the first time yesterday for a desktop pc and just wanted to write a super simple websocket application in node.js with a TUI. Unfortunately the WebSocket does not work, because it rejects every connection both in the browser and when using wscat no connection can be established. The Webserver for itself works but the websocket not:

const express = require('express');const http = require('http');const socketIo = require('socket.io');const blessed = require('blessed');const app = express();const server = http.createServer(app);const io = socketIo(server, {  cors: {    origin: "*",    methods: ["GET", "POST"]  }});const screen = blessed.screen({  smartCSR: true,  title: 'Node.js Server'});const box = blessed.box({  top: 'top',  left: 'left',  width: '100%',  height: '90%',  content: 'Waiting for clients to connect...',  tags: true,  border: {    type: 'line'  },  style: {    border: {      fg: '#f0f0f0'    }  }});screen.append(box);const list = blessed.list({  top: '90%',  left: 'left',  width: '100%',  height: '10%',  keys: true,  vi: true,  items: [],  style: {    selected: {      bg: 'blue'    }  }});screen.append(list);screen.key(['escape', 'q', 'C-c'], () => process.exit(0));screen.render();let clients = [];app.get('/', (req, res) => {  res.send('Server is running');});io.on('connection', (socket) => {  console.log(`Client connected: ${socket.id}`);  box.setContent(`Client connected: ${socket.id}`);  clients.push(socket);  list.addItem(`Client ${socket.id}`);  screen.render();  socket.on('disconnect', () => {    console.log(`Client disconnected: ${socket.id}`);    clients = clients.filter(client => client !== socket);    list.removeItem(list.getItemIndex(`Client ${socket.id}`));    box.setContent(`Client disconnected: ${socket.id}`);    screen.render();  });  socket.on('error', (err) => {    console.error(`Client error: ${err}`);    box.setContent(`Client error: ${err}`);    screen.render();  });  list.on('select', (item) => {    const selectedClient = clients[list.getItemIndex(item)];    const prompt = blessed.prompt({      top: 'center',      left: 'center',      width: '50%',      height: '25%',      label: ' Command ',      border: {        type: 'line'      },      style: {        border: {          fg: '#f0f0f0'        }      }    });    screen.append(prompt);    prompt.readInput('Enter command:', '', (err, command) => {      if (!err && command) {        selectedClient.emit('executeCommand', command);      }      screen.remove(prompt);      screen.render();    });  });});server.listen(4000, () => {  console.log('Server listening on port 4000');  box.setContent('Server listening on port 4000');  screen.render();});

Is there something wrong with the code or are there certain settings in ubuntu that prevent the connection?

I have already tried using different ports and different websockets to test. When I call the server with curl, the expected result comes up. With wscat I get the message error: socket hang up.


Viewing all articles
Browse latest Browse all 5956

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>