【问题标题】:socket.io type error (http) object is not a functionsocket.io 类型错误 (http) 对象不是函数
【发布时间】:2017-01-23 19:39:01
【问题描述】:

我正在通过其网站Socket.IO 学习使用 socket.io。一切顺利,但在我添加第三行 var io=require('socket.io')(http); 后,尝试运行服务器时出现错误:

C:\Users\user\chat-example\index.js:3
var io = require('socket.io')(http);
                             ^
TypeError: object is not a function
    at Object.<anonymous> (C:\Users\user\chat-example\index.js:3:30)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

这是Socket.IO中的代码

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

怎么了?这是我第一次学习 nodejs 和 socket.io。因此,如果您能帮助我理解和解决问题,我将不胜感激。谢谢。

【问题讨论】:

  • 如果您安装了过时版本的socket.io,则会出现该错误。您需要至少升级到 1.0.0 以与文档兼容。最新版本是1.2.1
  • @JonathanLonowski 好的。我确实使用npm install socket.io@"~0.8.1" 安装了它。但是当我使用 npm npm info socket.io version 检查它时,它显示 1.2.1。
  • npm info 显示来自远程存储库的信息。 version 将是最新版本。 You can see what version you have installed with npm ls.
  • 好的。伟大的。非常感谢!

标签: node.js sockets socket.io


【解决方案1】:

如果您安装了过时的socket.io 版本,则会出现该错误。您需要至少升级到 1.0.0 以与文档兼容。最新版本是1.2.1。 ——乔纳森·洛诺夫斯基

【讨论】:

    猜你喜欢
    • 2018-08-27
    • 2015-01-25
    • 2013-06-15
    • 2015-11-05
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2021-01-24
    • 2012-01-26
    相关资源
    最近更新 更多