简体   繁体   English

Web服务和Web应用程序之间双向通信的可能方式是什么

[英]what are the possible ways of two-way communication between web-service and web application

I have a web application and a web-service that needs to communicate with each other. 我有一个需要相互通信的Web应用程序和Web服务。 This communication has to be a two way communication because web-application can send events occured at interface to web-service. 这种通信必须是双向通信,因为Web应用程序可以将发生在接口的事件发送到Web服务。 And web-service can send the events recieved by third party to web-application part. Web服务可以将第三方收到的事件发送到Web应用程序部分。 So, what design should I follow for doing this? 那么,为此我应该遵循什么设计? How to make this communication a two-way communication? 如何使这种交流成为双向交流?

Regards, Anshul 问候,安舒尔

You can do this by employing the Comet model (called sometimes reversed AJAX). 您可以通过采用Comet模型(有时称为反向AJAX)来实现。 It is implemented by long-polling or opening a persistent connection to the webservice. 它是通过长期轮询或打开与Web服务的持久连接来实现的。

In long-polling mode, the client opens a connection to the webservice and waits for the server's response. 在长轮询模式下,客户端打开与Web服务的连接,并等待服务器的响应。 When there is an event coming from the server to the client, the server simply responds using that pre-existing connection and closes the connection, then the client reads the response, and immediately opens a new connection to listen for more events. 当服务器到客户端发生事件时,服务器仅使用该现有连接进行响应并关闭该连接,然后客户端读取响应,然后立即打开新连接以侦听更多事件。

In the streaming mode, the connection is persistent, not closed after each server event. 在流模式下,连接是持久的,不会在每次服务器事件后关闭。 HTTP/1.1 chunked responses with hidden iframe can be used to accomplish this. 可以使用带有隐藏iframe的HTTP / 1.1分块响应来完成此任务。

Regardless of the client-side implementation, beware using Comet may cause an excessive number of open connections on the server side. 无论采用哪种客户端实现,请注意使用Comet可能会导致服务器端的打开连接数量过多。 You should take that into account when deciding on the web-server stack / web framework - ideally it should use non-blocking I/O and continuations to decouple long connections from server's threads. 在决定Web服务器堆栈/ Web框架时,应考虑到这一点-理想情况下,它应使用非阻塞I / O和延续来断开长连接与服务器线程的耦合。

Some web frameworks have Comet support already built in: Play and Lift 某些Web框架已内置Comet支持: 播放提升

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

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