已有的网站集成Socket.io
发布于 5天前 作者 yakczh 217 次浏览 来自 问答

根据上面的指南 http://socket.io/docs/using-multiple-nodes/#

nginx 配置

upstream socket_nodes {
                server 192.168.1.100:5000;
}

     location / {
            root   html;
            index  index.html index.htm;

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://socket_nodes;

        }

服务端

var io = require('socket.io').listen(5000);
io.sockets.on('connection', function (socket) { console.log("connected!')});

客户端 test.html

<script type="text/javascript" src="http://node-chat-dd5dc.codingapp.com/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>

但是这样提示 NetworkError: 404 Not Found - http://localhost:8080/test.html"

如果把nginx的location 改成

     location / {
            root   html;
            index  index.html index.htm;
     }
    location /xx {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://socket_nodes;

        }

客户端

<script type="text/javascript" src="http://node-chat-dd5dc.codingapp.com/socket.io/socket.io.js"></script>
<script>
var socket = io('/xx');
</script>

这样firebug提示 http://localhost:8080/socket.io/?EIO=3&transport=polling&t=1423467802142-0 404 Not Found 1ms

我想实现的结果是 原来的html 页面 比如test.html xx.html还是由nginx 80端口 处理 ,只有socket.io的消息 才由nodejs 5000端口 来处理

回到顶部