I am trying to get socket.io running with node/express (I have done this countless times - so I am pretty baffled). When I try to configure socket.io with the io.configure
method I get an error:
io.configure(function() {
^
TypeError: Object #<Server> has no method 'configure'
at Object.<anonymous> (/home/yz/webdev/ground/app.js:377:4)
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
My code is exactly how it says to do it on socket.io - but I'll post it just in case:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(8000);
//other declarations
app.use(...
passport.use(...
//some routes
app.get(...
app.post(...
io.configure(function() {
io.set('transports', ['websocket','xhr-polling','flashsocket']);
io.set('flash policy port', 10843);
});
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
...
});
});
The error gets thrown at the io.configure
method - I have done this before with other sites, I don't understand whey it isn't working now. Thanks.
UPDATE
I was just reading here that io.configure
doesn't exist anymore - however, I can't seem to get it working the new way either. Here is my new code:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
//var io = require('socket.io').listen(server);
var socket = require('socket.io')({
'transports': ['websocket','xhr-polling','flashsocket'],
'flash policy port': 10843
});
var io = socket.listen(server);
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
...
});
});
I'm not getting any errors, socket.io just doesn't seem to be starting.
You're assigning io
to a server, which is causing the problems when you attempt to call functions on io
which are available from socket.io
. Try replacing this line:
var io = require('socket.io').listen(server);
With this line:
var io = require('socket.io')(server);
The socket.io docs list a couple of configuration patterns you can take advantage of, and it is possible to create a server using socket.io
. But since you're already creating a server, you shouldn't need to do it with socket.io
.
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.