简体   繁体   English

如何在骨干网中侦听HTTP事件

[英]How to listen for HTTP events in backbone

A complete backbone noob question. 一个完整的主干菜鸟问题。

I was wondering, is there a way to listen to HTTP-POST events in backbone? 我想知道,有没有办法在骨干网中监听HTTP-POST事件?

Lets say, when an API user does a POST request using wget from the command line I would like to update my view. 可以说,当API用户使用wget从命令行执行POST请求时,我想更新我的视图。 I looked at the API docs and I tried the request option but had no luck. 我查看了API文档 ,尝试了request选项,但没有运气。 Basically in my view I am trying something like this: 基本上我认为我正在尝试这样的事情:

this.listenTo(this.collection, 'request', this.render);

PS: I have the function definition of this.render in my code PS:我的代码中有this.render的函数定义

除非服务器使用某种服务器推送技术明确通知客户端有关事件,否则一个客户端无法监听另一客户端的事件。

I'm just going to extend on @Yaroslav's answer. 我将继续扩展@Yaroslav的答案。 His answer is totally correct; 他的回答是完全正确的。 you cannot explicitly do what you want. 您不能明确地做自己想做的事。 Your workflow would have to go something like this: 您的工作流程必须像这样:

  1. Client does some action from outside of your webapp (such as a command line POST) 客户端从您的Web应用程序外部执行某些操作(例如,命令行POST)
  2. Your server does whatever it was asked to do, but then notifies your client. 您的服务器将执行要求执行的所有操作,然后通知您的客户端。
  3. Your Backbone view accepts the notification and re-renders (with this.render) 您的骨干网视图接受通知并重新渲染(使用this.render)

In terms of your server push technology, you have a few different options. 在服务器推送技术方面,您有几种不同的选择。

  1. Polling w/ AJAX 带AJAX的轮询
  2. Long-polling w/ AJAX 带AJAX的长轮询
  3. Websockets 网络套接字
  4. Server-Sent Events 服务器发送的事件

All of these are touched upon and explained in this blog post . 所有这些在本博客文章中都有涉及和解释。

A brief summary: 简要总结:

Polling 轮询

Polling is when your web app asks your server for new information at a given interval. 轮询是指Web应用程序在给定的时间间隔内向服务器询问新信息。 Ie, every 10 seconds your Backbone application asks if anything new has happened. 即,您的Backbone应用程序每10秒就会询问是否发生了新的情况。 This can give a max latency of your interval time, and can lead to alot of unnecessary requests 这可能会给您的间隔时间带来最大的延迟,并可能导致大量不必要的请求

Long polling 长时间轮询

Your client always maintains an open request to the server, which is not returned until something happens. 您的客户端始终维护对服务器的打开请求,直到发生某种情况才会返回。 At that time, your client can act on the new information, and then makes a new request, which again stays open until new information. 那时,您的客户可以对新信息采取行动,然后发出新请求,该请求再次保持打开状态,直到有新信息。

Websockets 网络套接字

Websockets are a bidirectional transport mechanism which enables server->client and client->server messages. Websocket是一种双向传输机制,可启用服务器->客户端和客户端->服务器消息。

Server Sent Events 服务器发送事件

Server sent events are a uni-directional transport and part of the HTML5 spec. 服务器发送的事件是单向传输,是HTML5规范的一部分。 They allow server->client messages. 它们允许服务器->客户端消息。

Your client should listen to push service (WebSocket, long-polling AJAX, etc.) so if some event happen on server, the server will notify clients about it. 您的客户端应侦听推送服务(WebSocket,长轮询AJAX等),因此,如果服务器上发生某些事件,服务器将通知客户端有关此服务。

Consider say SockJS or Socket.IO for own implementation or some push service in the cloud that provides an API for these purposes, eg Pusher . 考虑使用SockJSSocket.IO来实现自己的实现,或者考虑使用云中的某些推送服务,这些推送服务会为此提供API,例如Pusher

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

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