简体   繁体   中英

Socket.io and express setup

I' m trying socket.io Cut and pasting the exact code they provide I have an error ! Any ideas I'm running on windows8 and node 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. You need to pass the Server to socket.io, and not the express application function.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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