如何在ws和wss彼此通信或同步数据的同时运行websocket服务器?还是HTTP上的WSS和HTTPS上的WS? [英] How to run a websocket server on ws and wss at same time that they both communicate or sync data with each other? Or WSS on HTTP and WS on HTTPS?

查看:142
本文介绍了如何在ws和wss彼此通信或同步数据的同时运行websocket服务器?还是HTTP上的WSS和HTTPS上的WS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,如果某些用户通过 WS或WSS 连接,他们可以彼此通信.现在,如果我为 WSS 运行节点服务器,它将无法运行 HTTP ,并且如果在 WS 上运行,则它不会在 HTTPS 上连接.有什么解决方案?

My requirement is this that if some users connect through WS or WSS they can communicate with each other.Now if i run node server for WSS it does not run over HTTP and if run for WS then it does not Connect on HTTPS .Any solution?

推荐答案

经过长时间的研究,我终于找到了这个解决方案,并且正在按我的要求为我工作.这是我的sever.js文件.

After a long research at last i find this solution and is working for me as i was requiring.This is my sever.js file.

/**
Before running:
> npm install ws
Then:
> node server.js
> open http://localhost:8080 in the browser
*/


const http = require('http');
const fs = require('fs');
const ws = new require('ws');

//for wss
const https = require('https');
const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};


const wss = new ws.Server({noServer: true});

const clients = new Set();

function accept(req, res) {
  
  if (req.url == '/ws' && req.headers.upgrade &&
      req.headers.upgrade.toLowerCase() == 'websocket' &&
      // can be Connection: keep-alive, Upgrade
      req.headers.connection.match(/\bupgrade\b/i)) {
    wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);
  } else if (req.url == '/') { // index.html
    fs.createReadStream('./index.html').pipe(res);
  } else { // page not found
    res.writeHead(404);
    res.end();
  }
}

function onSocketConnect(ws) {
  clients.add(ws);
  log(`new connection`);

  ws.on('message', function(message) {
    log(`message received: ${message}`);

    message = message.slice(0, 500); // max message length will be 50

    for(let client of clients) {
      client.send(message);
    }
  });

  ws.on('close', function() {
    log(`connection closed`);
    clients.delete(ws);
  });
}

let log;
if (!module.parent) {
  log = console.log;

// for wss
  https.createServer(options,accept).listen(8443);

  http.createServer(accept).listen(8080);
} else {
  // to embed into javascript.info
  log = function() {};
  // log = console.log;
  exports.accept = accept;
}

现在WS和WSS链接将从同一文件运行.对于WSS端口将是8443,对于WS 8080,其他链接将保持不变.对于WSS,这是必需的

Now WS and WSS links will run from same file.For WSS port will be 8443 and for WS 8080,Other link will remain same. For WSS these are required

键:fs.readFileSync('key.pem'),

证书:fs.readFileSync('cert.pem')

这是生成这些文件的帮助

and here is help for generating these files

//如何从密钥和CRT文件中获取Pem文件

//how-to-get-pem-file-from-key-and-crt-files

如何从.key获取.pem文件和.crt文件?

openssl rsa -inform DER -outform PEM -in server.key -out server.crt.pem

openssl rsa -inform DER -outform PEM -in server.key -out server.crt.pem

让我知道是否遇到任何问题.

Let me know if facing any issue.

这篇关于如何在ws和wss彼此通信或同步数据的同时运行websocket服务器?还是HTTP上的WSS和HTTPS上的WS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆