生产中的 WSS 连接问题

生产中的 WSS 连接问题

我使用 Angular8 作为前端,使用 Nodejs 作为后端

我已经在生产中配置了 WSS,但与客户端的连接无法正常工作,在一个页面上连接正常,但在另一个页面上连接不工作。

websocket 和服务器在同一端口上运行

使用 Ws(localhost) 一切正常

我们在后端使用的包

https://www.npmjs.com/package/ws

后端代码:

express = require('express');
app = express();
const http = require('http');
const port = 8080;
const fs = require('fs');
const certificate = {
 cert: fs.readFileSync(''),
 key: fs.readFileSync('')
}
const httpServer = http.createServer(app,certificate);

var server = require('ws');

var s =  new server.Server({ server:httpServer },{
  rejectUnauthorized: false
});
s.on('connection', function (ws) {
    ws.on('message', function (message) {
         var obj = JSON.parse(message);
          if (obj.messagetype == "test") {
              //send data
            }     
    }
}

httpServer.listen(port);

前端代码

export class WebsocketService {
constructor() { }
private subject: Rx.Subject<MessageEvent>;

public connect(url): Rx.Subject<MessageEvent> {
  if (!this.subject) {
    this.subject = this.create(url);
  }
 return this.subject;
}

private create(url): Rx.Subject<MessageEvent> {
  let ws = new WebSocket(url);

  let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
   ws.onmessage = obs.next.bind(obs);
   ws.onerror = obs.error.bind(obs);
   ws.onclose = obs.complete.bind(obs);
 return ws.close.bind(ws);
});
  let observer = {
  next: (data: Object) => {
   if (ws.readyState === WebSocket.OPEN) {
      ws.send(JSON.stringify(data));
    }
  }
 };
  return Rx.Subject.create(observer, observable);
  }
 }

const CHAT_URL = "wss://cen.abcuae.com/";

export class SockoneService {

public messages: Subject<Message>;
public messages2: Subject<Message>;

constructor(wsService: WebsocketService) {
 this.messages = <Subject<Message>>wsService.connect(CHAT_URL).pipe(map(
   (response: MessageEvent): Message => {
     let data = JSON.parse(response.data);
     return data;
   }));
   this.messages2 =  this.messages;
 }

这是 /etc/nginx/sites-available/default 下的配置

server {

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

server_name cen.abcuae.com;

location / {
    
            proxy_pass http://0.0.0.0:8080; #whatever port your app runs on
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_read_timeout 3600; 

} 
}
server {
listen 443;
listen [::]:443 ssl;

 server_name cen.abcuae.com;
    ssl_certificate /etc/letsencrypt/live/cen.abcuae.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cen.abcuae.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by C


location /websocket {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
  proxy_set_header Host $host;
  proxy_pass https://cen.abcuae.com:5000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}
}

答案1

express = require('express');
app = express();

//Use Here https
const http = require('https');

const port = 8080;
const fs = require('fs');
const certificate = {
 cert: fs.readFileSync(''),
 key: fs.readFileSync('')
}
const httpServer = http.createServer(app,certificate);

var server = require('ws');

var s =  new server.Server({ server:httpServer },{
  rejectUnauthorized: false
});
s.on('connection', function (ws) {
    ws.on('message', function (message) {
         var obj = JSON.parse(message);
          if (obj.messagetype == "test") {
              //send data
            }     
    }
}

相关内容