【发布时间】:2013-10-15 20:56:02
【问题描述】:
服务器端:
//When a player connects, all the players will get this message.
game.sockets.emit('entrance',
{
id: socket.id,
players: [],
message: 'Player ' + socket.id + ' is online',
store: function(id, object){
this.players.push({id: id, object: object});
}
});
客户端:
socket.on('entrance', function(data){
console.log(data.message);
data.store(data.id, new Car('hero').init());
});
这给了我以下错误:
它console.log的消息存储在对象中没有问题,所以它似乎是函数内部某处的错误。
谢谢!
【问题讨论】:
-
您不能通过网络传输函数,它们不能作为 JSON 序列化。您的
data对象将根本不包含store属性。每当您收到entrance事件(或从中构建整个Player实例)时,将其附加到客户端。
标签: javascript node.js object socket.io