为什么我无法通过SocketIO发送Express的响应对象? [英] Why am I unable to send response object of Express through SocketIO?

查看:63
本文介绍了为什么我无法通过SocketIO发送Express的响应对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过将其发送到ws客户端来发送对明确请求的响应,并等待其响应.因此,我需要将 res 对象发送给客户端(我找不到其他方法).

I'm trying to send the response to an express request by sending it to a ws client and wait for its response. So I need to send the res object to the client (I couldn't find another way).

这就是我所做的:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
    socket.on('res', (e) => {
        e.res.send(e.data)
    })
})

app.get('/endpoint', (req, res) => {
    io.emit('req', { data: 'test', res: res });
});

http.listen(3000);

但是,在转到/endpoint后,出现此错误:

However, after going to /endpoint I'm getting this error:

RangeError:超出最大调用堆栈大小
在Function.isBuffer(buffer.js:428:36)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:42:87)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)
在hasBinary(/workspace/socketio/node_modules/has-binary2/index.js:56:59)

RangeError: Maximum call stack size exceeded
at Function.isBuffer (buffer.js:428:36)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:42:87)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)
at hasBinary (/workspace/socketio/node_modules/has-binary2/index.js:56:59)

推荐答案

为什么我无法通过SocketIO发送Express的响应对象?

Why am I unable to send response object of Express through SocketIO?

当对象通过socket.io发送时,它们会使用 JSON.stringify()(或类似方法)转换为JSON.但是 JSON.stringify()在正常工作之前有一些要求.特别是,它仅支持某些数据类型,不支持循环引用,并且我猜想您在 res 对象中的这两个问题都存在问题.

When objects are sent through socket.io, they are converted to JSON using JSON.stringify() (or something similar). But JSON.stringify() has some requirements before it will work properly. In particular, it only supports certain data types and it does not support circular references and I'm guessing that you have issues with both of those in the res object.

目前尚不清楚您要在这里完成什么.实际上,根本没有理由将 res 对象发送给您的客户端.即使可以将其字符串化,客户也无法处理该信息.只是服务器上的客房整理信息,以便从服务器发送响应,并且该信息只能由服务器本身使用,而不能由客户端使用.

It is not clear exactly what you're trying to accomplish here. There is literally no reason at all to send the res object to your client. Even if it could be stringified, there is nothing the client could do with that information anyway. It's just housekeeping information from your server in order to send a response from the server and that info can only be used by the server itself, not by a client.

如果要向客户端发送消息并等待客户端的响应,则socket.io具有执行此操作的功能.它通过将回调作为第三个参数传递给 .emit()来工作,然后客户端执行类似于其响应的操作.您可以查看此socket.io"ack"功能的文档这里.

If you want to send a message to a client and wait for the client's response, then socket.io has a feature to do that. It works by passing a callback as the 3rd argument to .emit() and then the client does something similar to craft its response. You can see the documentation for this socket.io "ack" feature here.

这篇关于为什么我无法通过SocketIO发送Express的响应对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆