Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "socket hang up" using nodejs / socket.io and wscat

I have the following nodejs web socket server

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(8000);

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });


});

console.log("Listening on 8000");

When trying to connect using wscat

wscat -c ws://localhost:8000/

I get the following error

error: Error: socket hang up

It works just fine in browser with the following javascript

var socket = io.connect('http://localhost:8000');
socket.on('news', function (data) {
  console.log(data);
  socket.emit('my other event', { my: 'data' });
});

Help is appreciated!

like image 703
Khanh Nguyen Avatar asked Jul 26 '16 07:07

Khanh Nguyen


Video Answer


1 Answers

I suggest that you try connecting to endpoint while specifying transport protocol. Like this:

wscat -c ws://localhost:3000/socket.io/\?transport=websocket

This works for my case.

like image 81
Oto Brglez Avatar answered Nov 01 '22 22:11

Oto Brglez