从客户端访问存储在服务器端对象中的函数 [英] Accessing a function stored in an object on server-side from client-side
本文介绍了从客户端访问存储在服务器端对象中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
服务端:
//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
是存储在对象中的消息没有问题,所以看起来像是函数内部某处的错误.
It console.log
's the message stored in the object without a problem, so It seems like the error somewhere inside the function.
谢谢!
推荐答案
使用 socket.io,您通过网络发送 JSON 对象,而 JSON 不支持发送函数:http://www.json.org/
With socket.io, you're sending JSON objects over the wire, and JSON doesn't support the sending of functions: http://www.json.org/
查看这篇文章的详细方法来做你想做的事情:通过 socket.io 发送匿名函数?(该帖子专门讨论了匿名函数,但对于您正在尝试的内容也是如此).
See this post full a detailed approach to do what you're trying to do: Sending anonymous functions through socket.io? (the post talks specifically about anonymous functions, but the same holds true for what you're attempting).
这篇关于从客户端访问存储在服务器端对象中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文