gpt4 book ai didi

node.js - Node.js 上带有 websockets 的 https 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 23:01:49 24 4
gpt4 key购买 nike

我正在尝试创建具有 websockets 支持的简单 https 服务器。我有一些 https 服务器和 websockets 的功能代码,但都是独立的。不知道如何组合。我更喜欢使用免费使用的库(麻省理工学院,..)

我希望能够提供 http 请求和 websockets。

带有升级的 https 示例,但不知道如何处理升级后的 websocket 连接,就像我下面的第二个示例一样。

connection. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

var https = require('https');
var websocket = require('websocket');
var fs = require('fs');

var options = {
key:fs.readFileSync('./cert/server.key'),
cert:fs.readFileSync('./cert/server.crt')
};

var server = https.createServer(
options
, function(req,res) {

console.log('req');

res.writeHeader(200, { 'Content-Type': 'text/plain' });
res.write('test');
res.end();
}
);
server.on('upgrade', (req, socket, head) => {

console.log('upgrade ');

socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n');

socket.pipe(socket);
});

server.listen(8080, '127.0.0.1', () => {

// make a request
const options = {
port: 8080,
hostname: '127.0.0.1',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket'
}
};

const req = https.request(options);
req.end();

req.on('upgrade', (res, socket, upgradeHead) => {

console.log('got upgraded!');

*** so what to do here ***


/*
socket.end();
process.exit(0);
*/
});
});

这是功能性的 websocket,但无法处理简单的 http(s) 请求。

var https = require('https');
var ws = require('websocket').server;
var fs = require('fs');

var options = {
key:fs.readFileSync('./cert/server.key'),
cert:fs.readFileSync('./cert/server.crt')
};


var server = https.createServer(
options
, function(req,res) {
res.writeHeader(200);
res.end();
}
);

server.listen(8080);


var wss = new ws({httpServer:server});

var connectionNumber=0;
console.log('start ');
wss.on('request',function(req){

req.on('requestAccepted',function(conn){

conn.on('message',function(msg){

conn.send('test');
});

conn.on('close',function(msg){
});

});

req.accept(null,req.origin);
});

我一直在寻找组合解决方案的功能示例,但没有任何运气。

最佳答案

您可能会因自签名证书(代码1006)而遇到wss连接问题。在这种情况下,您必须将证书添加到根 CA( FirefoxChrome )。

// app.js
'use strict'
const https = require('https');
const fs = require('fs');
const ws = require('ws');

const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};

const index = fs.readFileSync('./index.html');

let server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end(index);
});
server.addListener('upgrade', (req, res, head) => console.log('UPGRADE:', req.url));
server.on('error', (err) => console.error(err));
server.listen(8000, () => console.log('Https running on port 8000'));

const wss = new ws.Server({server, path: '/echo'});
wss.on('connection', function connection(ws) {
ws.send('Hello');
ws.on('message', (data) => ws.send('Receive: ' + data));
});
<小时/>
// index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<script>
var socket = new WebSocket('wss://127.0.0.1:8000/echo');
socket.onopen = () => console.log('Connected') || setInterval(() => socket.send(new Date().toLocaleString()), 1000);
socket.onclose = (event) => console.log((event.wasClean) ? 'Disconnected' : 'Connection break: ' + (event.reason || event.code));
socket.onmessage = (event) => console.log('DATA', event.data);
socket.onerror = (err) => console.error(err.message);
</script>
Press F12 to open console...
</body>
</html>

关于node.js - Node.js 上带有 websockets 的 https 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46175397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com