gpt4 book ai didi

javascript - 如何通过代理或作为中间件来操作 socket.io 数据?

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:52 25 4
gpt4 key购买 nike

我已经在 Google 和 SO 上搜索了几周,以解决这个一直困扰我的问题。我将首先概述我的目标是什么,然后我将尝试解释我已经接近解决它了。

目标:

在大型应用程序及其数据 API 之间注入(inject)一个小型 express 服务器,其唯一目的是操纵来自服务器的数据响应(或策划自定义响应来代替服务器响应的响应)。

我们正在使用 socket.io 将客户端与服务器连接,实际上我真正想要修改的事件很少。我想在客户端和服务器之间来回代理大多数事件而不进行修改。

但是,我希望将一些宝贵的事件发送回客户端以进行测试

我尝试过的:

我尝试将基本的 Express 服务器设置为 socket.io-clientsocket.io 服务器。不过,我不知道如何将我的请求代理给彼此,并且特别担心跟踪多个客户端的可能性。这里的链接是关键,我被难住了

我也尝试过使用 http-proxy这已经让我非常接近了,但是到目前为止,我唯一能做的就是使用 socket.on('data', (data) => {/* do some */}); 在我的 proxy.on('open', socket => {}); 事件中检查有效负载的来去。

因此,虽然我可以看到大量数据来来去去,但我仍然需要有机会操纵它。

这是以前有人做过的事情吗?是否有关于如何通过将客户端连接连接到服务器来使 Express 服务器充当代理的良好资源?

预先感谢您的专业知识。我担心这可能有点太先进了。

最佳答案

好吧,正如我所 promise 的,我想出了一半的理想解决方案。理想情况下,我想来回代理所有事件,并且只监听几个关键事件,但我还没有弄清楚这一部分。如果有人能在这方面帮助我,我将永远感激不已。

相反,我只是映射出套接字每一端发出的所有事件并将它们隐藏在连接的调用中。

以下是我如何实现我想要的效果的具体细节 - 您照常创建一个服务器,然后在其连接上,从内部启动一个客户端连接.on('connection', () => {}); 调用。一旦进入此范围,您就可以相对确定两个连接都已打开,并开始来回传递事件。理想情况下,您会检查事件连接,但如果您的服务器和/或客户端能够处理断开的连接,则可能没问题。

您将在下面的简单示例中看到我如何来回传递事件,并完成工作。就像我说的,如果有人可以帮助我使用类似通配符的方法(由于某种原因我无法工作),请告诉我!

// GLOBAL CONFIGS
const serverConfig = {}; // Feel free to add your desired options here

// Some of these options pertain more to me than they will to you; don't feel like
// you have to copy these configs
const clientConfig = {
// We don't want to pre-emptivly try to connect to data-api,
// because we need our client to give us data first.
autoConnect: false,
multiplex: false,
// Disables/bypasses long polling which causes the connection to
// be severed then re-connected (for my api at least)
transports: ["websocket"]
};

const PORT = 3000;
const PROXY_HOST = '<api-endpoint-goes-here>'
const NAMESPACE = '<the name of your namespace, if applicable>';

// SERVER STUFF - this will connect to your client
const app = require('express')();
const server = require('http').Server(app);
const cors = require('cors');
const io = require('socket.io')(server, serverConfig);
const nsp = io.of(`/${NAMESPACE}`);

// CLIENT STUFF - this will connect to your api server as a client
const client = require('socket.io-client');
const ioClient = client(`${PROXY_HOST}/${NAMESPACE}`, clientConfig);

// Self explanatory I hope
app.use(cors());

// I am going to call this a root socket - meaning no namespace. Not entirely useful for now unless
// we want to inspect transitions in and out of our namespace, but just putting it here FYI
io.on('connection', socket => {
console.log(`A connection to the root socket made ${socket.handshake.headers.origin}`);

socket.on('disconnect', () => {
console.log(`A client disconnected from the root socket.`);
});
});

// Serve up a basic html file so that we can interact with our socket.io instance in the
// console. This index.html has a similar setup with a socket connection to both the data
// api and this test server, so I can ping events via console and test how this is working
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});

// Run the server
server.listen(PORT, () => console.log('Test data server is running'));

// Here's where the real solution reveals itself for me :)

// Listen for connections to the namespace we are looking for - this is the meat & potatoes of our app
nsp.on('connection', socket => {
console.log(`the ${NAMESPACE} namespace was connected`);

// Time to initiate a connection to the ioClient because we need it for the app now
ioClient.connect();

// Now check to make sure ioCLient is connected. Once we get inside here, we can be
// reasonably sure that we have a connection to both the client, and the data-api
ioClient.on('connect', () => {
// Acknowledge we have connected to the data-api
console.log('Connected to data-api via socket.io');

// Events sent from data-api
ioClient.on('<your event>', data => {
socket.emit('<your event>', data);
});

ioClient.on('<your event with two params>', (data1, data2) => {
socket.emit('<your event with two params>', data1, data2);
});

// ... etc

// Other event catchers for ioClient
ioClient.on('disconnect', () => {
console.log('Disconnected from the data-api');
});

ioClient.on('error', err => {
console.error('Error with the data-api connection: ', err);
});

// Events sent from the app client
socket.on('<your event>', data => {
ioClient.emit('<your event>', data);
});

socket.on('<your event with two params>', (data1, data2) => {
ioClient.emit('<your event with two params>', data1, data2);
});

// ... etc
});
});

关于javascript - 如何通过代理或作为中间件来操作 socket.io 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53937188/

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