Node.js Nodejs-socket.io TypeError:无法调用未定义的方法“on”
我对nodejs是新手。我在socketio方法中出错Node.js Nodejs-socket.io TypeError:无法调用未定义的方法“on”,node.js,socket.io,Node.js,Socket.io,我对nodejs是新手。我在socketio方法中出错 var http = require('http'); var io = require('socket.io'); var server = http.createServer(function(request, response){ console.log('Connection'); response.writeHead(200, {'Content-Type': 'text/html'}); response
var http = require('http');
var io = require('socket.io');
var server = http.createServer(function(request, response){
console.log('Connection');
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world');
response.end();
});
io.listen(server);
io.sockets.on('connection', function(socket){
socket.emit('message', {'message': 'hello world'});
});
这是回溯
io.sockets.on('connection',function(socket){
^
TypeError: Cannot call method 'on' of undefined
at Object.<anonymous> (/home/vivek/Documents/nodefiles/server.js:64:12)
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
我已安装套接字版本“0.9.16”。在哪里可以更正此问题??
提前谢谢
尝试卸下插槽,只需打开io即可
io.sockets.on('connection', function(socket){
socket.emit('message', {'message': 'hello world'});
});
io.sockets.on('connection', function(socket){
socket.emit('message', {'message': 'hello world'});
});