Node.js w/ socket.io : TypeError: Obj has no method ‘on’ at new Manager

Following the example here
and running into trouble.

I installed socketIO with npm install socket.io
When I run node server.js
the following exception occurs:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object # has no method 'on'
    at new Manager (/Users/x/nodeprojects/cloudnode/node_modules/socket.io/lib/manager.js:97:10)
    at Object.listen (/Users/x/nodeprojects/cloudnode/node_modules/socket.io/lib/socket.io.js:71:10)
    at Object. (/Users/x/nodeprojects/cloudnode/sky/server.js:12:26)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array. (module.js:421:10)
    at EventEmitter._tickCallback (node.js:126:26)

server.js:

var static = require('./plugins/node-static');
var socketIO = require('socket.io');
var clientFiles = new static.Server('./client');

var httpServer = require('http');
httpServer.createServer(function (request, response) {
    request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) {
    client.send('Please enter a user name ...');

    var userName;
    client.on('message', function(message) {
        if(!userName) {
            userName = message;
            webSocket.broadcast(message + ' has entered the zone.');
            return;
        }

        var broadcastMessage = userName + ': ' + message;
        webSocket.broadcast(broadcastMessage);
    });

    client.on('disconnect', function() {
        var broadcastMessage = userName + ' has left the zone.';
        webSocket.broadcast(broadcastMessage);
    });
});

I’m running node v 0.4.10 Anyone have an idea for a solution?

Problem courtesy of: Soroush Hakami

Solution

You’re passing the http module to socket.io instead of the http server.

var http = require('http');
var httpServer = http.createServer(function (request, response) {
  request.addListener('end', function () {
            clientFiles.serve(request, response);
        });
}).listen(8253);

var webSocket = socketIO.listen(httpServer);
webSocket.on('connection', function(client) {

Solution courtesy of: fent

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Node.js w/ socket.io : TypeError: Obj has no method ‘on’ at new Manager

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录