简体   繁体   English

未收到Socket.io事件(nginx反向代理,服务器到客户端)

[英]Socket.io events not received (nginx reverse proxy, server to client)

I have a nodejs socket.io app running under nginx reverse proxy. 我有一个在nginx反向代理下运行的nodejs socket.io应用程序。

My nginx configuration is as follows: 我的nginx配置如下:

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;
    }

This seems to work, and my nodejs app picks up the connection fine. 这似乎可行,并且我的nodejs应用可以正常运行。 On the server side socket.io I have the following: 在服务器端socket.io上,我有以下内容:

io.of('/online').on('connection', function(socket){

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);

The 'got_users' event is not being picked up by the client. 客户端未接收到“ got_users”事件。 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){

This doesn't pick up the "got_users" event, no matter what I try. 不管我尝试什么,这都不会发生“ got_users”事件。

Any help greatly appreciated. 任何帮助,不胜感激。

I finally figured this out, the following line: 我终于明白了,在下面的行:

io.sockets.in("users").emit("got_users",users);

Should be replaced with: 应替换为:

io.of("/online").in("users").emit("got_users",users);

Then it works. 然后就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM