在模型

时间:2016-02-15 15:19:12

标签: node.js socket.io

为什么我收到此消息?

我的app.js

io.sockets.on('connection', function(socket) {
    socket.on('login', function(data) {
        var player = new Player(socket, name)
    })      
}) 

我的player.js

var Player = function(socket, name) {
    this.socket = socket
    this.name_ = name
};

我无法解决这个问题,我不知道如何解决这个问题。这是一个问题吗?

错误讯息:

  

缺少错误处理程序onsocket。 RangeError:最大调用堆栈大小   超出了_hasBinary的Object.hasOwnProperty(native)   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:45)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)   在_hasBinary   (/Users/macbook/Desktop/batak/node_modules/socket.io/node_modules/has-binary/index.js:49:63)

1 个答案:

答案 0 :(得分:0)

如果将socket object存储在player object内部会给您带来问题,那么为什么不存储套接字的握手唯一ID?

这里我给出一个这种方法的例子,包括一个小例子,让你的应用程序通过使用socket.id作为搜索存储的索引来快速访问播放器对象。

var _players = {};
io.sockets.on('connection', function(socket) {
    socket.on('login', function(data) {
        var csid = socket.id;
        var player = new Player(csid, name);
        _players[csid] = player; //Store the player in memory
    });      
    //using the memory : example (prints name of player using socket.id)
    socket.on('ask_name', function() { 
         var csid = socket.id;
         console.log(_players[csid].name);});
});