简体   繁体   English

Socket.io和Express设置

[英]Socket.io and express setup

I' m trying socket.io Cut and pasting the exact code they provide I have an error ! 我正在尝试socket.io剪切并粘贴它们提供的确切代码,但我出错了! Any ideas I'm running on windows8 and node v0.10.26 ? 我在Windows8和节点v0.10.26上运行的任何想法吗?

var io = require('socket.io')(server);
                         ^
TypeError: object is not a function
at Object.<anonymous> (c:\Users\david wilson\TravelShopOffers\app_socketio: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:902:3

Here's the code from their site : 这是他们网站上的代码:

In conjunction with Express 结合快递

Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. 从3.0开始,快速应用程序已成为您传递给http或http Server实例的请求处理程序功能。 You need to pass the Server to socket.io, and not the express application function. 您需要将服务器传递给socket.io,而不是快速应用程序函数。

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

Will not be the best answer, as I was just discovering and experimenting on express/socket.io a few days ago. 不会是最好的答案,因为几天前我只是在express / socket.io上进行发现和试验。 Anyway I came up with the following ( interested in feedback ) : 无论如何,我想出了以下内容(对反馈感兴趣):

var app = require('express')();
var server = app.listen(3000);

var io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket)
   {
     socket.on('myEvent',function(){ /* … */ });
   }
);

Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM