0

我在 nodejs + socket.io 中有一个简单的 tcp 连接服务器可以向服务器发送数据,但我没有卷土重来。

对不起我的英语我来自巴西

看代码。

客户端代码:

if(newY !== ymouse  || newX !== xmouse)
{
    socket.emit('chat', {playerX: playerX, playerY: playerY});
}

socket.on('chat',
            function (data)
            {
                console.log(data)

            //  $("p#data_recieved").append("<br />\r\n [position] {" + data.msgr + ": playerX = " + data.playerX + ", playerY = " + data.playerY + "}");
                document.getElementById("vy").innerHTML = data.X;
                document.getElementById("vx").innerHTML = data.y;



            }

服务器代码:

var io = require('socket.io').listen(8000);

io.sockets.on('connection',
    function (socket)
    {
    socket.on('chat',
        function (data)
        {
            var sender = 'unregistered';
            socket.get('nickname', 
                function (err, name)
                {
                    console.log('Chat message by ', name);
                    console.log('error ', err);
                    console.log('X: ' + data.playerX);
                    console.log('Y: ' + data.playerY);
                    sender = name;
                });

            socket.broadcast.emit('chat', { playerX : data.playerX, playerY : data.playerY, msg : data, msgr : sender});
        });

    socket.on('position',
        function (data)
        {
            var sender = 'unregistered';
            socket.get('nickname', 
                function (err, name)
                {
                    sender = name;
                });

            if(sender == 'unregistered')
                return;

            var playerX = data.playerX;
            var playerY = data.playerY;

            socket.broadcast.emit('position', {msgr : sender, playerX : playerX, playerY : data.playerY});
        });

    socket.on('register',
        function (name)
        {
            socket.set('nickname', name,
                function ()
                {
                    io.sockets.emit('chat', {msg : "naay nag apil2! si " + name + '!', msgr : "mr. server"});
                });
        });

});

你怎么能看到我可以向服务器发送数据但没有响应服务器似乎运行良好

4

1 回答 1

0

我留下一个简单连接的例子,你可以在其中激发灵感

服务器:

var simple = io
  .sockets
  .on('connection', function(socket) {
    socket.on('message', function(data) {
      socket.broadcast.send(data);
    });
    socket.on('disconnect', function() {
      // handle disconnect
    });
  });

客户:

var socket = io.connect();
var content = $('#simple-content');
socket.on('connect', function() {
  $('#simple-form').css('display', 'block');
  content.append($('<p>').text('Connected'));  
});

socket.on('message', function(msg) {
  content.append($('<p>').text(msg)
         .append($('<em>').text(' from server')));
});

$('#simple-form').submit(function(e) {
  e.preventDefault();
  var textObj = $('#simple-text');   
  var msg = textObj.val();
  textObj.val('');
  content.append($('<p>').text(msg)
         .append($('<em>').text(' from me')));
  socket.send(msg);
});

我希望它会有所帮助!

于 2013-01-30T19:56:51.160 回答