node.js - Socket.io io.use fails - TypeError: io.use is not a function -
i'm working on project needs socket handshake session i'm trying code work, got https://www.npmjs.com/package/express-socket.io-session
var app = require('express')(), server = require("http").createserver(app), io = require("socket.io")(server), session = require("express-session")({ secret: "my-secret", resave: true, saveuninitialized: true }), sharedsession = require("express-socket.io-session"); // attach session app.use(session); // share session io sockets io.use(sharedsession(session)); io.on("connection", function(socket) { // i've add one... console.log(socket.handshake.session); // accept login event user's data socket.on("login", function(userdata) { socket.handshake.session.userdata = userdata; }); socket.on("logout", function(userdata) { if (socket.handshake.session.userdata) { delete socket.handshake.session.userdata; } }); }); server.listen(3000);
i run , first got
var io = require("socket.io")(server); ^ typeerror: require(...) not function @ object.<anonymous> (/users/jack/root/sites/scopone/provaric/server.js:8:30) @ module._compile (module.js:435:26) @ object.module._extensions..js (module.js:442:10) @ module.load (module.js:356:32) @ function.module._load (module.js:313:12) @ function.module.runmain (module.js:467:10) @ startup (node.js:136:18) @ node.js:963:3
solved adding
io = require("socket.io").listen(server);
and got one
io.use(function(socket, next) { ^ typeerror: io.use not function @ object.<anonymous> (/users/jack/root/sites/scopone/provaric/server.js:15:5) @ module._compile (module.js:435:26) @ object.module._extensions..js (module.js:442:10) @ module.load (module.js:356:32) @ function.module._load (module.js:313:12) @ function.module.runmain (module.js:467:10) @ startup (node.js:136:18) @ node.js:963:3
any ideas fix this?
socket.io@0.9.17
i had same problem in same context. though question here while,i post approach.
the whole problem used:
var io = require('socket.io');
but should be:
var io = require('socket.io')();
everything else working without braces @ end apparently io.use not.
instead of passing server did:
app.io = io;
in /bin/www
app.io.attach(server);
hope useful someone.
update: use socket 1.5.2.
Comments
Post a Comment