问题描述
我有一个在 nginx 反向代理下运行的 nodejs socket.io 应用.
I have a nodejs socket.io app running under nginx reverse proxy.
我的nginx配置如下:
My nginx configuration is as follows:
location ~ ^/(online|socket\.io) {
proxy_pass http://localhost:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
这似乎有效,我的 nodejs 应用程序连接正常.在服务器端 socket.io 我有以下内容:
This seems to work, and my nodejs app picks up the connection fine. On the server side socket.io I have the following:
io.of('/online').on('connection', function(socket){
这似乎工作正常,并且 'connection' 事件正在触发
This seems to work fine, and the 'connection' event is firing
当我尝试从服务器向客户端发出事件时出现问题:
The problem arises when I try to emit an event from the server to the client:
socket.join("users");
io.sockets.in("users").emit("got_users",users);
'got_users' 事件没有被客户端接收.在客户端,我有以下内容:
The 'got_users' event is not being picked up by the client. On the client side I have the following:
a4e.socket=io.connect('http://www.example.com/online');
这似乎工作正常,但随后:
This seems to work fine, but then:
a4e.socket.on("got_users",function(online_users){
无论我尝试什么,这都不会触发got_users"事件.
This doesn't pick up the "got_users" event, no matter what I try.
非常感谢任何帮助.
推荐答案
我终于想通了,下面这行:
I finally figured this out, the following line:
io.sockets.in("users").emit("got_users",users);
应替换为:
io.of("/online").in("users").emit("got_users",users);
然后就可以了.
这篇关于未收到 Socket.io 事件(nginx 反向代理,服务器到客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!