Javascript 如何在发射成功后侦听新消息(nodejs上的socket.io)?

Javascript 如何在发射成功后侦听新消息(nodejs上的socket.io)?,javascript,python,node.js,socket.io,Javascript,Python,Node.js,Socket.io,在上次发射(io.sockets.emit)成功后,我想再次侦听新事件(socket.on())。因为,当大量消息(socket.on())到达节点服务器时,我得到了堆栈溢出(我认为,所有函数调用都填满了堆栈) 以下是服务器代码(节点): 这是客户端代码(python发送者到服务器): 这是web客户端(服务器的接收器)的代码: 常量套接字=io.connect('http://localhost:5002'); socket.on('图像',(数据)=>{ const image=docum

在上次发射(
io.sockets.emit
)成功后,我想再次侦听新事件(
socket.on()
)。因为,当大量消息(
socket.on()
)到达节点服务器时,我得到了堆栈溢出(我认为,所有函数调用都填满了堆栈)

以下是服务器代码(节点):

这是客户端代码(python发送者到服务器):

这是web客户端(服务器的接收器)的代码:


常量套接字=io.connect('http://localhost:5002');
socket.on('图像',(数据)=>{
const image=document.getElementById('image');
image.src=`data:image/jpeg;base64,${data}`;
});
它用于接收图像并发送到连接的客户端(用于视频),我知道WebRTC,但我想了解如何管理这些功能以避免堆栈溢出。谢谢大家的阅读

以下是节点服务器完整错误

<--- Last few GCs --->

[2909:0x55b22e73f140]  1832725 ms: Mark-sweep 2687.3 (2800.0) -> 2687.2 (2800.0) MB, 144.5 / 0.0 ms  allocation failure GC in old space requested
[2909:0x55b22e73f140]  1832872 ms: Mark-sweep 2687.2 (2800.0) -> 2687.2 (2769.0) MB, 147.5 / 0.0 ms  last resort GC in old space requested
[2909:0x55b22e73f140]  1833020 ms: Mark-sweep 2687.2 (2769.0) -> 2687.2 (2769.0) MB, 147.6 / 0.0 ms  last resort GC in old space requested


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3a0332198fe1 <JSObject>
    1: /* anonymous */ [/home/estudiante/Downloads/WebSockets_webCam/node_modules/engine.io/lib/socket.js:~359] [pc=0x254b3a412b24](this=0x4a2f0bec79 <Socket map = 0x1c0e8bebeb61>,data=0x3d8ac82d0021 <Very long string[79045]>,options=0x3d8ac82d0139 <Object map = 0x1c0e8bebdb91>,callback=0x3a0332182241 <undefined>)
    2: arguments adaptor frame: 2->3
    3: writeToEngine [/home/estudiante/Downlo...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [node]
 2: 0x55b22cfe5011 [node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
 5: v8::internal::Factory::NewUninitializedFixedArray(int) [node]
 6: 0x55b22cbd1add [node]
 7: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 8: 0x254b3a4040bd
Aborted (core dumped)

[2909:0x55b22e73f140]1832725毫秒:标记扫描2687.3(2800.0)->2687.2(2800.0)MB,请求的旧空间中的GC分配失败144.5/0.0毫秒
[2909:0x55b22e73f140]1832872毫秒:标记扫描2687.2(2800.0)->2687.2(2769.0)MB,147.5/0.0毫秒请求旧空间中的最后一次GC
[2909:0x55b22e73f140]1833020毫秒:标记扫描2687.2(2769.0)->2687.2(2769.0)MB,147.6/0.0毫秒请求旧空间中的最后一次GC
==JS堆栈跟踪=========================================
安全上下文:0x3a0332198fe1
1://*匿名*/[/home/estudiante/Downloads/WebSockets\u webCam/node\u modules/engine.io/lib/socket.js:~359][pc=0x254b3a412b24](this=0x4a2f0bec79,data=0x3d8ac82d0021,options=0x3d8ac82d0139,callback=0x3a0332182241)
2:参数适配器帧:2->3
3:写引擎[/home/estudiante/Downlo。。。
致命错误:调用和重试上次分配失败-JavaScript堆内存不足
1:node::Abort()[node]
2:0x55b22cfe5011[节点]
3:v8::Utils::ReportOOMFailure(char const*,bool)[节点]
4:v8::内部::v8::FatalProcessOutOfMemory(char const*,bool)[节点]
5:v8::internal::Factory::NewUninitializedFileDarray(int)[节点]
6:0x55b22cbd1add[节点]
7:v8::internal::Runtime\u GrowArrayElements(int,v8::internal::Object**,v8::internal::Isolate*)[node]
8:0x254b3a4040bd
中止(堆芯转储)

当客户端收到
图像事件时,它会做什么?服务器上的内存不足,而且似乎没有向我们显示足够的代码,以便我们了解原因,因为您显示的服务器端代码似乎没有积累内存。因此,一定有比您显示的更多的事情导致了问题。嗯,如果你发帖然后消失,很难帮助你。Stackoverflow不同于在线论坛。你获得帮助的最佳机会是发帖后不久就参与进来的人。如果你不在身边回答澄清问题,那么你就错过了与试图帮助你的人接触的机会,其他人会看到你反应迟钝,只是在滑雪p on by.我建议您在接下来的30-60分钟内可以多次查看时发布。感谢您推荐@jfriend00关于我在这里的第一次互动。感谢您对这个问题的建议,我刚刚编辑了这个问题并添加了web客户端代码,当它收到每一个图像时,该代码会呈现这些图像。@jfriend00嗨,你能看看这个问题吗。
from socketIO_client import SocketIO
import time
import numpy as np
import cv2
import base64

print("Conectando...")
socketIO = SocketIO('localhost', 5002)
cap = cv2.VideoCapture(0)
cap_2 = cv2.VideoCapture(1)
FPS = 23;
print("Conectado.")

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    retval, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)
    socketIO.emit("image",jpg_as_text)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    time.sleep(1/FPS)

cap.release()
cv2.destroyAllWindows()
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <img align="middle" width="1100" height="800" id="image">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
    <script type="text/javascript">
        const socket = io.connect('http://localhost:5002');
        socket.on('image', (data) => {
            const image = document.getElementById('image');
            image.src = `data:image/jpeg;base64,${data}`;
        });
    </script>
</body>
</html>
<--- Last few GCs --->

[2909:0x55b22e73f140]  1832725 ms: Mark-sweep 2687.3 (2800.0) -> 2687.2 (2800.0) MB, 144.5 / 0.0 ms  allocation failure GC in old space requested
[2909:0x55b22e73f140]  1832872 ms: Mark-sweep 2687.2 (2800.0) -> 2687.2 (2769.0) MB, 147.5 / 0.0 ms  last resort GC in old space requested
[2909:0x55b22e73f140]  1833020 ms: Mark-sweep 2687.2 (2769.0) -> 2687.2 (2769.0) MB, 147.6 / 0.0 ms  last resort GC in old space requested


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x3a0332198fe1 <JSObject>
    1: /* anonymous */ [/home/estudiante/Downloads/WebSockets_webCam/node_modules/engine.io/lib/socket.js:~359] [pc=0x254b3a412b24](this=0x4a2f0bec79 <Socket map = 0x1c0e8bebeb61>,data=0x3d8ac82d0021 <Very long string[79045]>,options=0x3d8ac82d0139 <Object map = 0x1c0e8bebdb91>,callback=0x3a0332182241 <undefined>)
    2: arguments adaptor frame: 2->3
    3: writeToEngine [/home/estudiante/Downlo...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [node]
 2: 0x55b22cfe5011 [node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
 5: v8::internal::Factory::NewUninitializedFixedArray(int) [node]
 6: 0x55b22cbd1add [node]
 7: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 8: 0x254b3a4040bd
Aborted (core dumped)