gpt4 book ai didi

javascript - 带 socket.io 的 Node.js : TypeError: Obj has no method 'on' at new Manager

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:51 25 4
gpt4 key购买 nike

下面的例子here并遇到麻烦。

我用 npm install socket.io 安装了 socketIO当我运行 node server.js 时发生以下异常:

node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object #<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.<anonymous> (/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.<anonymous> (module.js:421:10)
at EventEmitter._tickCallback (node.js:126:26)

服务器.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);
});
});

我正在运行 Node v 0.4.10有人有解决方案的想法吗?

最佳答案

您将 http 模块传递给 socket.io 而不是 http 服务器。

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) {

关于javascript - 带 socket.io 的 Node.js : TypeError: Obj has no method 'on' at new Manager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8620861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com