Socket.io上的404连接 [英] 404 on Socket.io connect
问题描述
我使用了socket.io网站示例中的代码,但遇到了一些问题
i use code from examples of socket.io site and has some problem
我的服务器代码(在debian 192.168.5.200上)
My server code (on debian 192.168.5.200)
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(1337);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
我的客户代码(index.html)
My client code (index.html)
<script src="http://{host ip}:1337/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://{host ip}:1337');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
我启动节点服务器 在浏览器中打开http://{host ip}:1337 然后...在Socket.io连接上得到404
i start node server open in browser http://{host ip}:1337 and... got 404 on socket.io connection
它尝试获取"/api/1/?t=..."URL并通过表示无法获取/api/1/?t = ..."且出现404错误得到答案
it try to get "/api/1/?t=..." url and got answer by express "Cannot get /api/1/?t=..." with 404 error
请帮助我(
推荐答案
尝试将您的客户端代码更改为:
Try to change your client code to:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on("connect", function() {
socket.on('news', function (data) {
socket.emit('my other event', { my: 'data' });
});
});
</script>
此外,为了能够转到/api/1...
,您需要注册相应的app.get
,例如如app.get("/api/*", ...
,它将处理与/api/...
的所有连接.否则,预计会收到404错误.
Also, in order to be able to go to /api/1...
you need to register the corresponding app.get
, e.g. as app.get("/api/*", ...
, which would handle all the connections to /api/...
. Otherwise it is expected that you will be getting the 404 error.
这篇关于Socket.io上的404连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!