I am doing something where I have two websockets, one for the client and one for the server are being used alongside an HTTP node server at port 3000, am I doing something wrong here? Or is this not possible?
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 };
I think you should do some modify: delete the upgrade settings in location /
And notice you request a path which is /ws , but the location regex in nginx.conf is /ws/ . keep it same.
use this to get a socket :
socket = openSocket("ws://example.com/ws/", {transports: ['websocket']})
Add this code to see whether the ws connection is build.
socket.on('connect', function () {
console.log('connected!');
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.