socket.io无效....从控制台收到错误消息http://localhost:8001/socket.io/1/?t=1441787711937 404(未找到)。
这是服务器代码(serverPackage.js)。使用node-static
var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function(req, res) {
file.serve(req, res);
}).listen(8001);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
socket.on('message', function(message) {
log('S --> got message: ', message);
socket.broadcast.to(message.channel).emit('message', message);
});
socket.on('create or join', function(room) {
var numClients = io.sockets.clients(room).length;
log('S --> Room ' + room + ' has ' + numClients + ' client(s)');
log('S --> Request to create or join room', room);
if (numClients == 0) {
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else {
socket.emit('full', room);
}
});
function log() {
var array = [">>> "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
});
这就是socket.io链接到浏览器的方式:(index.html)
<script src='http://localhost:8001/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js'></script>
package.json是
{
"name": "video_call",
"version": "1.0.0",
"description": "A simple video call app",
"main": "serverPackage",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "AOS",
"license": "ISC",
"dependencies":{
"express":"3.x",
"socket.io":"0.9.x"
}
}
我在同一文件夹目录中有index.html,serverPackage.js,package.json和(node_modules文件夹)。
我不知道有什么不对,为什么它找不到。任何人都可以帮助我。