端口80上的Nginx HTTP和2 TCP

时间:2017-08-18 09:59:59

标签: node.js nginx websocket socket.io

我正在做的事情我有两个websockets,一个用于客户端,一个用于服务器,与端口3000的HTTP节点服务器一起使用,我在这里做错了吗?或者这不可能吗?

nginx config:

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://localhost:3000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    location /ws/ {
        proxy_pass http://localhost:8000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}

api.js

import openSocket from 'socket.io-client';
const  socket = openSocket('ws://example.com/ws');

function roomSubscribe(roomId, cb) {
    socket.on('roomInfo', data => cb(null, data));
    socket.emit('getRoom', {'roomId': roomId});
}

function sendMessage(content) {
    socket.emit('sendMessage', content);
}

export { roomSubscribe, sendMessage };

1 个答案:

答案 0 :(得分:0)

我认为你应该做一些修改: 删除位置/

中的升级设置

请注意,您要求的路径为 / ws ,但nginx.conf中的位置正则表达式为 / ws / 。保持不变。

用它来获取套接字:

.get()

添加此代码以查看是否构建了ws连接。

socket = openSocket("ws://example.com/ws/", {transports: ['websocket']})
相关问题