2016-02-26 16 views
6

ho una molto semplice socket.io chattare esempio, e il codice lato server è come questo:Come connettersi con socket.io da un client ws?

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'); 
}); 

Il lato client utilizzando la presa io codice per collegarlo e sta lavorando bene:

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> 

Ma voglio usare qualche altro client websocket per collegare t egli Server, dicono, wscat:

npm install -g wscat 
wscat ws://localhost:3000 

ma non può collegarsi, con questo errore:

error: Error: socket hang up 

È il mio url ws://localhost:3000 è sbagliato? Come farlo funzionare?

PS: Potete vedere questo progetto https://github.com/js-demos/socketio-chat-demo/ e provarlo

risposta

18

Dagli Strumenti di Chrome Dev, ho trovato il vero URL websocket, dovrebbe essere:

ws://localhost:3000/socket.io/?EIO=3&transport=websocket 

Usa questo URL con wscat funziona bene

Problemi correlati