I have a very simple example of a socket.io socket, and the server-side code looks like this:
https://github.com/js-demos/socketio-chat-demo/blob/master/index.js
var express = require('express'); var app = express(); var http = require('http').Server(app); var io = require('socket.io')(http); app.use(express.static('public')); io.on('connection', function(socket){ socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); http.listen(3000, function(){ console.log('listening on *:3000'); });
The client side uses the io socket code to connect it and works well:
https://github.com/js-demos/socketio-chat-demo/blob/master/public%2Findex.html
<script> var socket = io(); $('form').submit(function(){ socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script>
But I want to use another websocket client to connect the server, say wscat :
npm install -g wscat wscat ws://localhost:3000
But it cannot connect, with this error:
error: Error: socket hang up
Is my url ws://localhost:3000 wrong? How to make it work?
PS: You can see this project https://github.com/js-demos/socketio-chat-demo/ and try it
source share