2017-08-18 65 views
1

我正在做一些事情,我有兩個websockets,一個用於客戶端,一個用於服務器端口,與3000端口的HTTP節點服務器一起使用,我在這裏做錯了什麼?或者這是不可能的?Nginx HTTP和端口80上的2個TCP

nginx的配置:

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

他們可以 – tbking

回答

0

我認爲你應該做一些修改: 在位置/刪除升級設置

而且注意到您請求的路徑即/ws,但nginx.conf中的位置正則表達式爲/ws/。保持不變。

使用這種獲取套接字:

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

添加此代碼來查看WS連接是否建立。

socket.on('connect', function() { 
    console.log('connected!'); 
}); 
+0

3000端口進入我的應用程序/ HTTP和端口8000相同的端口上運行進入到我的服務器 – user3068644

+0

然後在位置/,不要使用升級。它用於ws連接構建。 – GuangshengZuo

+0

我仍然遇到以下兩個錯誤: 'websocket.js:6與'ws://example.com/sockjs-node/063/fabw334n/websocket'的WebSocket連接失敗:WebSocket握手期間出錯:意外的響應代碼: 400'和 'http://example.com/socket.io/?EIO=3&transport=polling&t=LttlOsD 404(Not Found)' – user3068644