Simple HTTP(S) Response on WebSocket (WSS) Server

huangapple go评论66阅读模式
英文:

Simple HTTP(S) Response on WebSocket (WSS) Server

问题

我正在端口443上运行一个带证书的WebSocket(wss)服务器。为此,我在Node.js中使用了“https”和“ws”模块(简化版):

require(__dirname + './config.js');
const https = require('https');
const WebSocketServer = require('ws');
const server = new https.createServer({
    key: fs.readFileSync('./privkey.pem'),
    cert: fs.readFileSync('./fullchain.pem')
});
const wss = new WebSocketServer.Server({ server: server });
wss.on("connection", function (socket) {
    const c_inst = new require('./client.js');
    const thisClient = new c_inst();
    thisClient.socket = socket;
    thisClient.initiate();
    socket.on('error', thisClient.error);
    socket.on('close', thisClient.end);
    socket.on('message', thisClient.data);
});
server.listen(config.port, config.ip);

wss通信运行完美!但如果我在浏览器中通过https打开wss URL,我会收到一个超时错误(错误代码524),因为对于“正常”的(https/get)请求没有响应。

如何在我的wss服务器中为这种情况实现响应呢?
我只想发送一个简单的文本或HTML响应,如“这是一个WebSocket服务器”,或者如果有人通过浏览器/https连接到wss套接字,可以执行重定向到另一个URL。

谢谢!

英文:

I am running a websocket (wss) server (with certificate) on port 443. For this i am using the "https" and "ws" module in nodejs (simplified):

require(__dirname + './config.js');
const https = require('https');
const WebSocketServer = require('ws');
const server = new https.createServer({
    key: fs.readFileSync('./privkey.pem'),
    cert: fs.readFileSync('./fullchain.pem')
});
const wss = new WebSocketServer.Server({server: server});
wss.on("connection", function(socket){
    const c_inst = new require('./client.js');
    const thisClient = new c_inst();
    thisClient.socket = socket;
    thisClient.initiate();
    socket.on('error', thisClient.error);
    socket.on('close', thisClient.end);
    socket.on('message', thisClient.data);
});
server.listen(config.port, config.ip);

The wss communication works perfect! But if i open the wss url via https:// in the browser i get a timeout error (Error code 524) because there is no response for a "normal" (https/get) request.

How can i implement a response for this case in my wss server?
I just want to send a response with a simple text or html like "This is a websocket server" or do a redirect to another url if someone connects to the wss socket via browser/https.

Thanks!

答案1

得分: 1

查看Node.js上的HTTPS服务器文档。
https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener

在创建服务器时,需要添加请求监听器。

const server = new https.createServer(
    {
        key: fs.readFileSync('./privkey.pem'),
        cert: fs.readFileSync('./fullchain.pem')
    },
    (req, res) => {
        res.writeHead(200);
        res.end('This is a websocket server\n');
        // 进行重定向或其他操作
    }
);
英文:

See the documentation for HTTPS server on node.
https://nodejs.org/api/https.html#httpscreateserveroptions-requestlistener

You need to add request listener when you create a server.

const server = new https.createServer(
    {
        key: fs.readFileSync('./privkey.pem'),
        cert: fs.readFileSync('./fullchain.pem')
    },
    (req, res) => {
        res.writeHead(200);
        res.end('This is a websocket server\n');
        // do redirects or whanever you want
    }
);

huangapple
  • 本文由 发表于 2023年1月5日 09:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75013089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定