sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-04 12:05:32 25 4
mua khóa gpt4 Nike

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

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

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

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

1 Câu trả lời

Sau đây là một ví dụ: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 :


<đầu>


client 1


client 1




(chức năng() {
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 () => {
thử {
const res = await fetch('http://localhost:3000/external-api');
console.log(res);
} catch (error) {
console.log(error);
}
});
};
})();



client-2.html :


<đầu>


client 2


client 2




(chức năng() {
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 () => {
thử {
const res = await fetch('http://localhost:3000/external-api');
console.log(res);
} catch (error) {
console.log(error);
}
});
};
})();



现在,点击客户端 1 的按钮,发送 HTTP 请求到 /external-api .
客户端 1 的控制台日志:
nhập mô tả hình ảnh ở đây
客户端 2 的控制台日志:
nhập mô tả hình ảnh ở đây
服务器的日志:
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
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com