gpt4 book ai didi

node.js - 在 express 中从 POST 请求处理程序发送 WebSocket 消息

转载 作者:行者123 更新时间:2023-12-04 12:05:32 25 4
gpt4 key购买 nike

我有一个 Express 服务器,用于监听来自外部 API 的 webhook 事件。当它收到这些事件时,我希望该 http 请求的处理程序向 WebSocket 客户端发送一条消息。这是一些基本代码来说明我的意思
外部 API 将向我的 Express 服务器上的端点发送 HTTP POST 请求,可以说它看起来像这样:

app.post('/external-api', (req, res) => {

// webhook payload will be in req.body
})
/external-api 的处理程序中我想向通过 WebSocket 连接到服务器的客户端发送消息。截至目前,我正在使用 npm ws库,所以我希望逻辑看起来像这样
app.post('/external-api', (req, res) => {

ws.broadcast(req.body);
})
有没有办法做到这一点?我愿意使用其他库,但我只需要一种方法来从 Express 中的 HTTP POST 请求处理程序向 WebSocket 客户端发送消息。

最佳答案

下面是一个例子:index.ts :

import express from 'express';
import WebSocket from 'ws';
import http from 'http';
import path from 'path';
import faker from 'faker';

const app = express();
const port = 3000;
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', (ws: WebSocket) => {
console.log('establish websocket connection');
ws.on('message', (message) => {
console.log('received: %s', message);
});
});

app.get('/client/:id', (req, res) => {
res.sendFile(path.resolve(__dirname, `./public/client-${req.params.id}.html`));
});

app.get('/external-api', (req, res) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(faker.internet.email());
}
});
res.sendStatus(200);
});

server.listen(port, () => console.log(`http server is listening on http://localhost:${port}`));
client-1.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>client 1</title>
</head>
<body>
<p>client 1</p>
<button id="test">Test</button>
<script>
(function() {
window.onload = function() {
const socket = new WebSocket('ws://localhost:3000');

// Connection opened
socket.addEventListener('open', function(event) {
socket.send('Hello Server! Client - 1');
});

// Listen for messages
socket.addEventListener('message', function(event) {
console.log('Message from server ', event.data);
});

const btn = document.getElementById('test');
btn.addEventListener('click', async () => {
try {
const res = await fetch('http://localhost:3000/external-api');
console.log(res);
} catch (error) {
console.log(error);
}
});
};
})();
</script>
</body>
</html>
client-2.html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>client 2</title>
</head>
<body>
<p>client 2</p>
<button id="test">Test</button>
<script>
(function() {
window.onload = function() {
const socket = new WebSocket('ws://localhost:3000');

// Connection opened
socket.addEventListener('open', function(event) {
socket.send('Hello Server! Client - 2');
});

// Listen for messages
socket.addEventListener('message', function(event) {
console.log('Message from server ', event.data);
});

const btn = document.getElementById('test');
btn.addEventListener('click', async () => {
try {
const res = await fetch('http://localhost:3000/external-api');
console.log(res);
} catch (error) {
console.log(error);
}
});
};
})();
</script>
</body>
</html>
现在,点击客户端 1 的按钮,发送 HTTP 请求到 /external-api .
客户端 1 的控制台日志:
enter image description here
客户端 2 的控制台日志:
enter image description here
服务器的日志:
http server is listening on http://localhost:3000
establish websocket connection
received: Hello Server! Client - 1
establish websocket connection
received: Hello Server! Client - 2
如您所见,服务器向客户端 1 和客户端 2 广播虚假电子邮件。

关于node.js - 在 express 中从 POST 请求处理程序发送 WebSocket 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63099518/

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