简体   繁体   English

如何解决 React.js socket.io 确认超时?

[英]How to resolve React.js socket.io acknoweldgement timed out?

I have a Node.js and React.js applications, server and client, respectively.我有一个 Node.js 和 React.js 应用程序,分别是服务器和客户端。 I send data between them via socket.io and I am trying to implement acknoledgement but I get an error saying its been timed out:我通过socket.io在它们之间发送数据,我正在尝试实现确认,但我收到一条错误消息,说它已超时:

Error: operation has timed out
at Timeout._onTimeout (C:\Project\gateway\node_modules\socket.io\dist\broadcast-operator.js:137:30)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)

Here is my server code.这是我的服务器代码。 Here the message variable prints Error: operation has timed out这里消息变量打印Error: operation has timed out

 socket.emit((topic, message),function(message) {
        console.log('GOT MESSAGE');
        console.log(message);
    });  

Here is my client code:这是我的客户端代码:

 ioClient.on('triton/acs', (data, callback) => {
    callback("Message for server")
 }

Whhy is this not working and how to fix it?为什么这不起作用以及如何解决?

As @darvine mentioned, there is not a lot of code to see how you implement socket.io server side and client side.正如@darvine 提到的,没有太多代码可以查看如何实现 socket.io 服务器端和客户端。 However, from what I see above, I'm trying my best to answer your question in broad strokes since I am seeing a few syntax errors.但是,从我上面看到的情况来看,由于我看到了一些语法错误,所以我正在尽力粗略地回答您的问题。

Your backend code:您的后端代码:

socket.emit((topic, message),function(message) {
    console.log('GOT MESSAGE');
    console.log(message);
});  

It appears that you did not establish a connection to the client.您似乎没有与客户端建立连接。 You would achieve that by doing the following.您可以通过执行以下操作来实现。 Also, socket.emit is sending the data to the client.此外,socket.emit 正在将数据发送到客户端。 (There is no callback function in.emit). (.emit 中没有回调函数)。 You send the data as the second parameter.您将数据作为第二个参数发送。 If you want to receive data from the client then you use.on('listener', cb).如果你想从客户端接收数据,那么你使用.on('listener', cb)。 See below;见下文;

//establish connection to the client
io.on('connection', (socket)=> {
      //sending a message to the client
      socket.emit('yourSentMessage', 'you are sending this message to the client');
      
      //receiving a message from the client.
      socket.on('yourReceivedMessage', (msg) => {
                console.log(msg) //this message was received from the client
             });
     });

On the frontend, it is important that you provide your socket instance appropriately.在前端,适当地提供套接字实例很重要。 I would utilize context api.我会利用上下文 api。 See this article for further information on this.有关这方面的更多信息,请参阅本文 You can wrap the components in the provider and have access to the socket instance.您可以将组件包装在提供者中并可以访问套接字实例。 Again, it is also important to destinguish between.emit and.on on the frontend.同样,在前端区分 between.emit 和 .on 也很重要。 If you want to send data to the backend, you utilize.emit (no callback).如果你想向后端发送数据,你可以使用 .emit(无回调)。 If you receive data from the backend, you utilize.on('listener', cb).如果您从后端接收数据,则使用.on('listener', cb)。

Lastly, react requires you to set the proxy server while you are running on localhost.最后,React 要求您在本地主机上运行时设置代理服务器。 You do that in the package.json file as shown in the screenshot below:您可以在 package.json 文件中执行此操作,如下面的屏幕截图所示:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM