我在 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"});
});
});
});
你怎么能看到我可以向服务器发送数据但没有响应服务器似乎运行良好