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

馋奶兔 提交于 2021-01-29 04:20:51

问题


I have a nodejs socket.io app running under nginx reverse proxy.

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

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

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. 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.

Any help greatly appreciated.


回答1:


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.



来源:https://stackoverflow.com/questions/42026922/socket-io-events-not-received-nginx-reverse-proxy-server-to-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!