使用 Socket.IO 的 NGINX 配置 [英] NGINX configuration to work with Socket.IO

查看:95
本文介绍了使用 Socket.IO 的 NGINX 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直试图让它工作 2 天,但我被卡住了.这是我第一次为使用 NodeJS+Socket IO 的 rails 配置服务器.我是 NGINX 和 Unicorn 的菜鸟.基本上,我的应用程序的 NodeJS+SocketIO 部分会将消息推送给连接到我的应用程序的用户.这是我的 nginx.conf

So I have been trying to get this to work 2 days and I am stuck. This is my first time configuring a server for rails that uses NodeJS+Socket IO. I am a noob with NGINX and Unicorn. Basically the NodeJS+SocketIO part of my app will push messages to users who are connected to my app. This is my nginx.conf

server{
    listen 80 default;
    root /home/deployer/saigon/public;
    try_files $uri/index.html $uri @unicorn;

    location /sockets {
        proxy_pass http://localhost:3210;
    }

    location @unicorn {
       proxy_pass http://localhost:3000;
    }
}

在我的 production.rb 中,我配置了用户必须向其发送消息/接收消息的 url

And in my production.rb, I have configured the url which the user will have to send message to/receive message from

SOCKET_IO_URL ='http://localhost:8080/sockets

为什么是 8080?我使用 Vagrant 转发 8080 -> 80

Why 8080? I use Vagrant to forward 8080 -> 80

我尝试访问 http://localhost:8080/sockets 并且我能够收到套接字欢迎消息.我查看了我的 NodeJS 服务器日志,它正在接收消息.然而,当谈到广播时......它只是不这样做.有没有人让这种应用程序与我想要做的设置一起工作?我应该选择 Apache + Unicorn 吗?

I tried accessing http://localhost:8080/sockets and I was able to get the socket welcome message. I looked at my NodeJS server log and it was receiving messages alright. However, when it comes to broadcasting..... it just does not do it. Has anyone ever gotten this kind of app to work with the set up I am trying to do? Should I just go with Apache + Unicorn ?

'

推荐答案

更新答案

我终于找到了让这个工作的方法.这一点都不明显,但是当在子文件夹上托管 socket.io 时,不要在 connect 语句中使用子文件夹.这就是我之前所做的,客户端从未收到响应.

I have finally figured out a way to get this working. It's not at all obvious, but when hosting socket.io on a subfolder, you do NOT use the subfolder in the connect statement. This is what I was doing before and the client was never receiving a response.

不工作

<script src="/test/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080/test/', {resource:'test/socket.io'});
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

http://localhost:8080/test/ 这是抛出的部分事情了.这为服务器端不尊重的本地套接字创建了一个命名空间.因此,客户端在命名空间/test/"上发送消息,但服务器响应将转到空命名空间",因此客户端永远不会收到消息.解决方法是简单地删除/test/"并确保您在客户端和服务器上使用资源变量.

http://localhost:8080/test/ This is the part which is throwing things off. That creates a namespace for the local socket which the server side does not respect. So the client sends the message on the namespace '/test/' but the server responses are going to an empty namepace '' so the client never gets the messages. The workaround it to simply remove the '/test/' and make sure you are using the resource variable on the client and the server.

工作!

<script src="/test/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080', {resource:'test/socket.io'});
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

我希望这可以帮助您完成工作.

I hope this helps you get things working on your end.

原答案

这不是你设置的问题,是socket.io不想在子文件夹上工作的问题.我敢打赌,如果您删除了/sockets,您的示例会正常工作.我在使用 http-node-proxy 尝试在子文件夹上托管 socket.io 连接时遇到了完全相同的问题.前段时间创建了一个错误,但它已关闭且从未解决.

It is not a problem with your setup, it is a problem of socket.io not wanting to work on sub folder. I would bet willing to be that if you dropped the /sockets your example would work fine. I ran into the exact same problem when using http-node-proxy trying to host socket.io connections on subfolders. There was a bug created a while ago, but it was closed out and never resolved.

https://github.com/LearnBoost/socket.io-client/问题/185

https://github.com/LearnBoost/socket.io/issues/320

我也在寻找解决方案,但我有一种感觉,我将不得不卷起袖子,亲自深入研究代码.

I am still looking for a solution as well, but I have a feeling I'm going to have to roll up my sleeves and dig into the code myself.

这篇关于使用 Socket.IO 的 NGINX 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆