簡體   English   中英

Socket.IO:TypeError:對象沒有方法包

[英]Socket.IO: TypeError: Object has no method packet

我遇到了Socket.IO的全局發射事件的問題。 這是服務器端錯誤,而不是#489

發生錯誤的情況

nsp = io.of('/namespace');
nsp.emit('hello', 'there');

io.emit('hello', 'there');

io.sockets.emit('hello', 'there');

var needed_namespace = '/my_namespace';
io.nsps[needed_namespace].emit('hello', 'there');

在每種情況下,我都有以下錯誤:

TypeError: Object function (key) {
    if (!this[key]) {
      return this[key] = 1;
    } else {
        return this[key] += 1;
    }
} has no method 'packet'
at /Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/index.js:125:28
at Encoder.encode (/Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/node_modules/socket.io-parser
at Adapter.broadcast (/Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/index.js:107:16)
at Namespace.emit (/Users/nr/Git/lt-connection/node_modules/socket.io/lib/namespace.js:218:18)
at SocketTransport.global.SocketTransport.SocketTransport.deliver (/Users/nr/Git/lt-connection/lib/prototype/socket.coffee:40:26)
at SocketTransport.global.SocketTransport.SocketTransport.emit (/Users/nr/Git/lt-connection/lib/prototype/socket.coffee:11:6)
at SocketTransport.global.SocketTransport.SocketTransport.send_from_http (/Users/nr/Git/lt-connection/lib/prototype/socket.
at Object.exports.routes.handlers.post (/Users/nr/Git/lt-connection/lib/http_server/routes/socket_router.coffee:9:11)
at apply (/Users/nr/Git/lt-connection/node_modules/director/lib/director/router.js:444:19)
at _every (/Users/nr/Git/lt-connection/node_modules/director/lib/director/router.js:28:9)
at [object Object].Router.invoke (/Users/nr/Git/lt-connection/node_modules/director/lib/director/router.js:450:5)
at parseAndInvoke (/Users/nr/Git/lt-connection/node_modules/director/lib/director/http/index.js:175:10)
at [object Object].Router.dispatch (/Users/nr/Git/lt-connection/node_modules/director/lib/director/http/index.js:198:7)
at /Users/nr/Git/lt-connection/lib/http_server/index.coffee:31:13
at IncomingForm.<anonymous> (/Users/nr/Git/lt-connection/node_modules/formidable/lib/incoming_form.js:104:9)
at IncomingForm.emit (events.js:92:17)
at IncomingForm._maybeEnd (/Users/nr/Git/lt-connection/node_modules/formidable/lib/incoming_form.js:551:8)
at QuerystringParser.parser.onEnd (/Users/nr/Git/lt-connection/node_modules/formidable/lib/incoming_form.js:446:10)
at QuerystringParser.end (/Users/nr/Git/lt-connection/node_modules/formidable/lib/querystring_parser.js:25:8)
at IncomingMessage.<anonymous> (/Users/nr/Git/lt-connection/node_modules/formidable/lib/incoming_form.js:129:30)
at IncomingMessage.emit (events.js:92:17)
at _stream_readable.js:938:16
at process._tickCallback (node.js:419:13)

發生錯誤的實際代碼(它是從CoffeeScript編譯的,為便於閱讀,抱歉):

deliver: function(message, channel, namespace, socket) {
    var err;
    socket = this.get_namespace(namespace, socket);
    message = this.prepage_message(message);
    try {
        if (channel != null) {
        //error occur here (line 125:28 from error stack)
        return socket.emit(channel, message);
    }
        //also error will be here when 'channel' not passed to '.deliver()'
        return socket.emit(message);
    } catch (_error) {
        err = _error;
        return console.warn('Something may be wrong: ' + err.stack);
    }
},

get_namespace: function(ns, socket) {
    var namespace, _ref;
    if (socket != null) {
        return socket;
    }
    namespace = null;
    if ((namespace = this.namespaces[ns]) != null) {
        return namespace;
    } else if ((namespace = (_ref = this.io.nsps) != null ? _ref[namespace] : void 0) != null) {
        return this.namespaces[ns] = namespace;
    } else {
        return this.create_namespace(ns);
    }
},

create_namespace: function(ns) {
    var socket;
    if (ns == null) {
        ns = '/';
    }
    socket = this.io;
    if (ns !== '/') {
        socket = socket.of(ns);
    }
    this.namespaces[ns] = socket;
    return socket;
}

有什么辦法嗎? 還是我做錯了嗎? 順便說一下,其中兩種情況在官方文檔中列出

GitHub上也有一個問題: #1747

UPD:

另一個堆棧跟蹤:

  socket.io-parser encoding packet {"type":2,"data":["hello","world"],"nsp":"/"} +0ms
  socket.io-parser encoded {"type":2,"data":["hello","world"],"nsp":"/"} as 2["hello","world"] +0ms

/Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/index.js:125
        if (socket) socket.packet(encodedPackets, true, flags.volatile);
                           ^
TypeError: Object function (key) {
    if (!this[key]) {
      return this[key] = 1;
    } else {
      return this[key] += 1;
    }
  } has no method 'packet'
  at /Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/index.js:125:28
  at Encoder.encode (/Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/node_modules/socket.io-parser/index.js:110:5)
  at Adapter.broadcast (/Users/nr/Git/lt-connection/node_modules/socket.io/node_modules/socket.io-adapter/index.js:107:16)
  at Namespace.emit (/Users/nr/Git/lt-connection/node_modules/socket.io/lib/namespace.js:218:18)
  at Server.(anonymous function) [as emit] (/Users/nr/Git/lt-connection/node_modules/socket.io/lib/index.js:338:16)
  at SocketTransport.global.SocketTransport.SocketTransport.deliver (/Users/nr/Git/lt-connection/lib/prototype/socket.coffee:40:15)
  at SocketTransport.global.SocketTransport.SocketTransport.emit (/Users/nr/Git/lt-connection/lib/prototype/socket.coffee:11:6)

您可以嘗試使用setInterval查看此問題嗎? 可能是因為上下文不同。

https://github.com/Automattic/socket.io/issues/489

您正在使用一個對象(類?)來存儲您的方法,因此它可能處於錯誤的上下文中。

謝謝大家 問題解決了,被我的應用程序和modyfied原型造成的Object

global.Object.prototype.increment = function(key) {
    if(!this[key]){
        this[key] = 1;
    }else{
        this[key] += 1;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM