简体   繁体   English

如何在没有 web-socket 的情况下从服务器监听事件

[英]How to listen the event from server without web-socket

I want to implement one validation feature where the client submits some data like CSV files.我想实现一个验证功能,客户端提交一些数据,如 CSV 文件。 And the server will do the validations.服务器将进行验证。 Validations are done in multiple stages each stage can take a variable amount of time to validate.验证分多个阶段完成,每个阶段可能需要不同的时间来验证。 Whenever the server completes one validation step, it should send it to the client, so that I can show it to the user in UI.每当服务器完成一个验证步骤时,它应该将其发送给客户端,以便我可以在 UI 中将其显示给用户。

What should be the better way to do it?更好的方法应该是什么?

I don't want to use WebSocket, as this is the only feature using that and it will be momentary.我不想使用 WebSocket,因为这是唯一使用它的功能,而且它是暂时的。

I also don't want to have long polling as it will keep the server busy unnecessary.我也不想进行长时间的轮询,因为它会使服务器不必要地忙碌。

I want something like firebase HTTPS polling it keeps open for a while and discards it every 3 sec, and in between keep listening to the server.我想要像 firebase HTTPS 之类的东西,轮询它保持打开一段时间并每 3 秒丢弃一次,并在两者之间继续监听服务器。 But I don't know how to implement it.但我不知道如何实现它。

Also if the answer is a keep-alive HTTPS connection.此外,如果答案是保持活动的 HTTPS 连接。 Not sure how to write code for that either.也不知道如何为此编写代码。 So the example in React/JS and the node can be very helpful.所以 React/JS 和节点中的示例非常有帮助。

You could also use rejax.io api service which you can make http post request to it with a message that will be broadcasted to client(s) for you.您还可以使用rejax.io api 服务,您可以向它发出 http 发布请求,并为您广播给客户端的消息。 I am author.我是作者。

  • register for a free api key注册一个免费的 api 密钥
  • include client script for clients包括客户端的客户端脚本
  • make (from server) http post request to service with message使(从服务器) http 发布请求以使用消息服务
  • message is broadcasted immediately to clients消息立即广播给客户端
  • your server continues with processing您的服务器继续处理

client code:客户端代码:

Rejax.listen('my-channel-name', function(text) {
    // received text from server
    console.log(text)
})

server code:服务器代码:

const axios = require('axios')
axios
    .post('https://rejax.io:3001/api/server', {
        'app_key' : 'MY_APP_KEY',
        'app_secret' : 'MY_APP_SECRET',
        'channel' : 'my-channel-name',
        'text' : 'hello from server'
    })
    .then(res => {
        console.log(res.data)
    })
    .catch(error => {
        console.error(error)
    })

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

相关问题 如何使用来自网络套接字的网络音频 API 流式传输音频块? - how to stream audio chunks using web audio API coming from web-socket? 如何将网络套接字对象从我的服务同步到组件-angular 2 - How do i sync a web-socket object from my service to the component -angular 2 如何为“ws”Web-Socket 模块创建自定义事件? - How to create custom events for 'ws' Web-Socket module? 是否可以识别收到的网络套接字请求? - Is it possible to identify web-socket request on receive? 为什么消费者.py中的事件处理程序function被多次调用,然后web-socket连接被强制终止? - Why an event handler function in consumers.py is called multiple times and then web-socket connection is killed forcefully? 客户端(JS-浏览器)和服务器(PHP)通过 Web-Socket 通过 IP 通信 - Client(JS-Browser) and Server(PHP) communication over IP through Web-Socket 如何立即收听来自客户端(vue)的服务器(快递)发送的事件 - How to listen to server(express) sent event from client(vue) instantly 通过网络套接字接收数据时 Blob 获取的问题 - Problems with Blob acquisition when receiving data through a web-socket 为什么尝试建立网络套接字时连接被拒绝? - Why is connection refused when trying to establish a web-socket? 如何在发出事件 Socket.io 后监听事件 - How to listen to an event after emitting an event Socket.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM