我尝试从我的Node服务器解析'/socket.io/socket.io.js'时收到404。我无法确定原因。
我的服务器配置如下所示:
var express = require('express')
, engine = require('ejs-locals')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
// use ejs-locals for all ejs templates:
app.engine('ejs', engine);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs'); // so you can render('index')
app.use(express.static(__dirname + '/public'));
//define routes
...
app.listen(3000);
//socket io
io.sockets.on('connection', function (socket) {
socket.on('set nickname', function (name) {
socket.set('nickname', name, function () {
socket.emit('ready');
});
});
socket.on('msg', function () {
socket.get('nickname', function (err, name) {
console.log('Chat message by ', name);
});
});
});
在客户端,我有这个:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
我正在本地测试(端口:3000)。套接字代码基本上是从socket.io示例中删除,以使某些工作正常。我已经查看了其他帖子,似乎无法找到我出错的地方。有人可以帮忙吗?
由于
答案 0 :(得分:7)
知道了!
HTTP服务器需要在端口上列出(在这种情况下为3000)。然后我删除了app.listen(3000),因为地址已经在使用中并且不需要。
谢谢!
var express = require('express')
, engine = require('ejs-locals')
, app = express()
, http = require('http')
, server = http.createServer(app).listen(3000)
, io = require('socket.io').listen(server);