【问题标题】:Socket.io and Express getting an error "has no method socket"Socket.io 和 Express 收到错误“没有方法套接字”
【发布时间】:2012-04-10 06:08:01
【问题描述】:

这是我在尝试测试基本 Socket.io 和 Express 设置时遇到的错误(根据 socket.io 网站上的示例):

/Users/scottcorgan/Projects/sevenly/campaigns/node_modules/socket.io/lib/manager.js:659

        var socket = this.namespaces[i].socket(data.id, true);



 ^

TypeError: Object function extend(another) {
  var properties = Object.keys(another);
  var object = this;
  properties.forEach(function (property) {
    object[property] = another[property];
  });
  return object;
} has no method 'socket'
    at Manager.handleClient (/Users/scottcorgan/Projects/sevenly/campaigns/node_modules/socket.io/lib/manager.js:659:41)
    at Manager.handleUpgrade (/Users/scottcorgan/Projects/sevenly/campaigns/node_modules/socket.io/lib/manager.js:588:8)
    at HTTPServer.<anonymous> (/Users/scottcorgan/Projects/sevenly/campaigns/node_modules/socket.io/lib/manager.js:119:10)
    at HTTPServer.emit (events.js:88:20)
    at Socket.<anonymous> (http.js:1390:14)
    at TCP.onread (net.js:334:27)

感谢我能得到的任何帮助:)

【问题讨论】:

  • 应该清楚你从 this.namespaces 得到的任何项目都不包含一个叫做 socket 的方法。如果您需要帮助追踪真正的问题,请粘贴更多代码。
  • 通常socket不是一个方法,它是一个对象,你需要在给定的socket上调用.emit().write()
  • 我正在使用来自 socket.io 网站的 Express 示例:link
  • @scottcorgan:在这些示例中我找不到对this.namespaces[i].socket 的任何引用?
  • 我也做不到。这就是我问的原因。当我只使用他们的示例代码时,我无法弄清楚为什么会发生这个错误......我很困惑。

标签: node.js express socket.io


【解决方案1】:

这个问题源于您或您使用的库正在向 Object.prototype 添加函数。

因此这段代码:

Object.prototype.foo = function() {};
Object.prototype.bar = function() {};

var myObj = { x: 1 };

for (var i in myObj) {
   console.log(i)
}

将打印:x, foo, bar(不一定按此顺序)而不仅仅是 x如您所愿。

在你的情况下,这发生在 manager.js 中:

// initialize the socket for all namespaces
for (var i in this.namespaces) {
    var socket = this.namespaces[i].socket(data.id, true);

    // echo back connect packet and fire connection event
    if (i === '') {
        this.namespaces[i].handlePacket(data.id, { type: 'connect' });
    }
}

这段代码不希望遇到声明的:extend键,从错误的堆栈跟踪中可以看出:

TypeError: Object function extend(another) {
  var properties = Object.keys(another);
  var object = this;
  properties.forEach(function (property) {
    object[property] = another[property];
  });
  return object;
} has no method 'socket'

程序实际上试图在 extend 函数上调用 socket()

请参阅 Bob's rant here 了解向 Object.prototype 添加函数。

至于解决方案,您可以像这样在 manager.js 中添加条件语句:

// initialize the socket for all namespaces
for (var i in this.namespaces) {
    if ('extend' == i) continue;  // ADDED
    var socket = this.namespaces[i].socket(data.id, true);

    // echo back connect packet and fire connection event
    if (i === '') {
        this.namespaces[i].handlePacket(data.id, { type: 'connect' });
    }
}

或者您可以删除 Object.prototype.extend = function(...) {} 声明,这是我个人的偏好。

【讨论】:

    【解决方案2】:

    您的this.namespaces[i].socket(data.id, true); 不存在。执行console.log(typeof this.namespaces[i].socket(data.id, true)); 之类的操作,您可能会得到undefined

    我敢打赌,您的命名空间数组的元素之一丢失了。

    【讨论】:

    • 命名空间数组是什么意思?
    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 2014-07-19
    • 2014-08-05
    • 2017-06-04
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多