简体   繁体   English

从客户端访问存储在服务器端对象中的功能

[英]Accessing a function stored in an object on server-side from client-side

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

Client-side: 客户端:

socket.on('entrance', function(data){
        console.log(data.message);

        data.store(data.id, new Car('hero').init());    
    });

This gives me the following error: 这给了我以下错误:

在此处输入图片说明

It console.log 's the message stored in the object without a problem, so It seems like the error somewhere inside the function. console.log的消息存储在对象中没有问题,因此似乎在函数内部出现错误。

Thanks! 谢谢!

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,您可以通过网络发送JSON对象,而JSON不支持函数的发送: http : //www.json.org/

See this post full a detailed approach to do what you're trying to do: Sending anonymous functions through socket.io? 请参阅完整的这篇文章,详细了解您要执行的操作: 通过socket.io发送匿名函数? (the post talks specifically about anonymous functions, but the same holds true for what you're attempting). (本文专门讨论了匿名函数,但您尝试使用的匿名函数也是如此)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM