我的客户端app.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://mywebsite.com/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
我的服务器端 app.js:
var express = require('express')
, http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(process.env.PORT || 8888);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/app.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
如果我访问http://mywebsite.com:8888,控制台会显示:
Object {hello: "world"}
但如果我访问http://mywebsite.com/app.html,控制台会显示:
GET http://mywebsite.com/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined
重发到这里https://stackoverflow.com/a/10192084/990434我有 express v3.1.1,我也试过这个答案https://stackoverflow.com/a/14167488/990434无济于事。我从一个谷歌小组中尝试了一些其他随机的东西,我似乎在我漫长的混乱历史中找不到这些东西。知道如何解决这个问题吗?