如何 运行 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?
我的要求是,如果某些用户通过 WS 或 WSS 连接,他们可以与每个 other.Now 通信,如果我 运行 节点服务器 WSS 它不会 运行 over HTTP 并且如果 运行 for WS 那么它在 HTTPS 上无法连接。有什么解决办法吗?
经过长时间的研究,我终于找到了这个解决方案并且对我有用 requiring.This 是我的 sever.js 文件。
/**
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 links 将 运行 来自相同的 file.For WSS 端口将为 8443,对于 WS 8080,其他 link 将保持不变。
对于 WSS,这些是必需的
键:fs.readFileSync('key.pem'),
证书:fs.readFileSync('cert.pem')
这里是生成这些文件的帮助
//如何从密钥和 crt 文件中获取 pem 文件
How to get .pem file from .key and .crt files?
openssl rsa -inform DER -outform PEM -in server.key -out server.crt.pem
如果遇到任何问题,请告诉我。
我的要求是,如果某些用户通过 WS 或 WSS 连接,他们可以与每个 other.Now 通信,如果我 运行 节点服务器 WSS 它不会 运行 over HTTP 并且如果 运行 for WS 那么它在 HTTPS 上无法连接。有什么解决办法吗?
经过长时间的研究,我终于找到了这个解决方案并且对我有用 requiring.This 是我的 sever.js 文件。
/**
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 links 将 运行 来自相同的 file.For WSS 端口将为 8443,对于 WS 8080,其他 link 将保持不变。 对于 WSS,这些是必需的
键:fs.readFileSync('key.pem'),
证书:fs.readFileSync('cert.pem')
这里是生成这些文件的帮助
//如何从密钥和 crt 文件中获取 pem 文件
How to get .pem file from .key and .crt files?
openssl rsa -inform DER -outform PEM -in server.key -out server.crt.pem
如果遇到任何问题,请告诉我。