【问题标题】:NodeJS Socket.io client not workingNodeJS Socket.io 客户端不工作
【发布时间】:2014-01-13 22:57:07
【问题描述】:

我一直在尝试让 socket.io-client 与端口 9000 上的 socket.io localhost 服务器一起工作。客户端和服务器都基于 NodeJS。

这似乎连接到服务器:

io = require('socket.io-client')

socket = io.connect('http://localhost:9000', {resource : 'node_modules/socket.io'})

socket.on 'connect', ->
    socket.emit 'message', {hello: 'world'}
    return

但是它给出了以下错误:

      self.transport.onClose();
                 ^
TypeError: Cannot call method 'onClose' of null

服务器似乎正确地注册了请求:

GET /node_modules/socket.io/1/?t=1388048751499 200 11ms - 2.81kb

是否有基于 NodeJS 的 Socket IO 客户端的工作示例?找到的链接:

Uncaught TypeError: Cannot call method 'onClose' of null

【问题讨论】:

  • self.transport.onClose(); 在哪里调用?
  • 我相信这是来自 socket.io-client 模块。它似乎是一个相当稳定的模块。不太清楚为什么它不起作用。我检查了构造函数的文档,它似乎无法正常工作,不得不谷歌搜索,哈哈。。
  • 或者,有没有更好的方法/应该检查另一个模块?
  • 这不是socket.iosocket.io-client 的问题。你肯定做错了什么。我们需要更多的代码来找出什么是不正常的。

标签: node.js coffeescript socket.io


【解决方案1】:

什么。哈哈。 NPM 上的模块坏了。 -.-

伙计们直接从 Github 中提取 -.- 这行得通。

npm install git+https://github.com/LearnBoost/socket.io-client.git

在 v1.0 中,这对我有用(连同下面的 pong 示例)

服务器

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(){ // … });
server.listen(3000);

Express 运行在开发端口(对我来说是 9000),Socket.io 运行 @ 端口 3000

参考:http://liamkaufman.com/blog/2012/01/28/testing-socketio-with-mocha-should-and-socketio-client/#trouble-shooting

【讨论】:

  • 这是两年前的问题。 NPM 版本对我来说很好用(node@0.10.24,这里是gist)。
  • 谢谢!我建立在你的例如并纳入 v1.0。
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 2019-04-04
  • 2013-12-15
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 2012-05-24
相关资源
最近更新 更多