如何在API调用下生成套接字发出事件 [英] How to generate the socket emit events under API's calls

查看:43
本文介绍了如何在API调用下生成套接字发出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关Socket.io的帮助/支持/指导 在我的服务器页面即app.js上,我初始化了Socket,并且工作正常.现在,我想将emit函数调用到其他页面.但是我没有从另一个页面获取事件,因为该事件是在连接下生成的. 我将分享伪代码,通过这些伪代码您可以了解有关app.js页面的信息

I need the help/support/guidance regarding the Socket.io On My server page i.e app.js, I initialized the Socket and it works fine. Now I want to call the emit function to some other page. But I did not get the event from another page because the event generated under connection. I will share the pseudo code by which you can get some idea about app.js page

const io = require('socket.io');
  const server = io.listen(3003);
  const triggerEvent = (socket) => {
    return socket.emit('event-trigger', true);
  }
  server.on('connection', function (socket) {
    triggerEvent(socket);
  });

我还尝试使用Module.exports triggerEvent函数并将该函数获取其他页面 我正在打开和测试Socket Connection的HTML页面 test.html:

I also tried to use Module.exports triggerEvent Function and get that function other pages My HTML page where I am opening and testing the Socket Connection test.html:

<html>

<head>
  <title>Socket io client</title>
  <script src="http://localhost:3003/socket.io/socket.io.js"></script>
  <script>
    var socket = io("http://localhost:3003");
    // use your socket
    socket.on("event-trigger", (message) => {
      // do something with the message.
      alert(message)
      console.log(message)
    })
  </script>
</head>

<body>
</body>

</html>

所以目前它可以正常工作.如果我打开HTML页面,则套接字服务器生成警报.还可以在我的服务器控制台上打印用户连接的消息.

So currently it works fine. If I open the HTML Page then alert generated by socket server. Also user connected message print on my server console.

现在,我有多个路由,并且在该文件下编写了API功能的多个文件下.因此,假设某些路线文件夹中有handle.js文件.在该文件下,假设我有一个名为localhost:3000/likepost的API.因此,在此API上,当一个类似的用户被发布并且200个成功响应被发送回Frontend Server时,就在我想要 触发事件,即socket .emit('event-trigger',true);并且此事件将向Frontend发送事件触发请求,因此Frontend将显示一些通知,例如someone liked your post正在平台上呈现. 请为此提供一些指导.为此,我们将提供任何帮助或建议.

Now I have multiple routes and under that multiple files where API functionality is written. So let's say Some routes folder I have handle.js file. Under that file let's say I have an API named as localhost:3000/likepost. So on this API when a user like is posted and 200 success response is sent back to Frontend Server, then just after I want to trigger the event i.e socket .emit('event-trigger', true); and this event will send Frontend the event fired request, so Frontend will show some notification like someone liked your post is rendering on platform something like that. Please provide me some guidance for that. Any help or suggestion is really appreciated for that.

推荐答案

您的套接字连接引用未附加到emit函数,它应该在您的连接"(server.on('connection'))回调函数中 参考如下:

Your socket connection reference is not attached to the emit function, it should be inside your "connection" (server.on('connection')) callback function Reference below :

  const io = require('socket.io');
  const server = io.listen(3003);

  server.on('connection', function (socket) {      
    socket.emit('event-trigger', true);      
  });

这篇关于如何在API调用下生成套接字发出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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