简体   繁体   中英

Pass string value in socket.io

After I am passing any string value the server is getting disconnected. The server only accept number in node.js .

io = sio.listen(server);
io.sockets.on('connection', function(socket){
    socket.on('chat message', function(msg){
    if ((new String(msg)).toString('utf-8') === 'utf-8') {
        socket.send(msg);
        console.log("Bye bye!\n");
        socket.disconnect();
    } else  {
        console.log('Received: ', msg);
        messages.push(msg);
        io.sockets.emit('chat message', msg);
    }
});

I tried this code. But it is not working. Please help me.

var socket = io.connect('http://localhost:8000');
 $('form').submit(function(){
 socket.emit('chat message', $('#m').val());
 $('#m').val();
 return false;
 });
 var datavar=1;
 socket.on('chat message', function(msg){
 $('#messages').append(msg); });

this is client side code...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM