在socket.io中传递字符串值

Pass string value in socket.io

本文关键字:字符串 socket io      更新时间:2023-09-26

在我传递任何字符串值后,服务器将断开连接。服务器只接受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);
    }
});

我试过这个代码。但它不起作用。请帮帮我。

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

这是客户端代码。。。