我正在尝试将对象从一个客户端传递到另一个客户端,例如多人棋盘游戏中的棋子.我有使用工作的解决方案JSON.parser和__proto__,但我很好奇,想知道是否有更好的方法.
客户发送:
var my_piece = new BoardPiece();
// ... my_piece is assigned data, like x-y coordinates
socket.send(JSON.stringify(my_piece));
Run Code Online (Sandbox Code Playgroud)
服务器将该片段转发给其他人:
client.broadcast(piece);
Run Code Online (Sandbox Code Playgroud)
另一位客户收到:
var your_piece = JSON.parse(json);
your_piece.__proto__ = BoardPiece.prototype; // provide access to BoardPiece's functions
Run Code Online (Sandbox Code Playgroud)
这是我使用的最后一步,我__proto__担心我可能会在脚下射击自己.有更好的建议吗?