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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With