本文介绍了推送通知| websocket是强制性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器端使用PHP,在客户端使用HTML和javascript。



我制作了一个应用程序,利益相关者输入一个广播的消息多个组的实时接收者。



我对google进行了一些研究,我知道我需要使用WebSockets或Comet来实时推送通知。 WebSocket或Comet是否必须向用户发送大量通知?

我的理解是否正确?任何引用都以?开头?如果客户端是浏览器,那么标准浏览器只能通过Ajax()方式连接到服务器的两种方式例如http)请求或webSocket连接。因此,如果您希望客户从外部世界得到通知,它必须使用这两种机制之一。



HTTP请求是短暂的。 / strong>客户端发出服务器请求,服务器响应。 HTTP请求非常适合客户端从服务器请求信息。他们不擅长服务器向客户端发送信息,因为通常客户端没有连接。有一些黑客和解决方法,客户端在某个时间间隔内轮询服务器甚至服务器使用较长的运行请求来尝试模拟推送类型的系统,但它们最多只是次优的黑客。



webSockets是连续的连接。客户端连接并且连接保持原位,只要双方都需要。这使得任何一方都可以随时向对方发送消息。这意味着服务器可以随时推送数据到客户端。 webSockets对于推送连接是有效的,并且是推荐的(这是它们设计的主要内容之一)。



Comet是一个最初为使用HTTP而构建的库在webSocket发明之前尝试劈砍或模拟推送,然后再广泛支持。我想没有理由为什么人们会想要使用Comet而不是webSocket,除非你有一个不支持webSocket的旧浏览器。



所以,如果你是试图对浏览器进行实时服务器推送,那么你必须从客户端连续连接一个套接字,这意味着webSocket(或者像socket.io这样建立在webSocket之上的东西)。



对于可以访问手机SDK的手机应用程序,您可以使用操作系统内置的推送系统将一些消息从服务器推送到客户端。这与双向webSocket通道并不完全相同,但是因为您询问了推送通知,Android和IOS中可用的操作系统推送服务也可以作为将通知从服务器推送到客户端的选项。以下是有关和



截至2016年,您还可以使用但在边缘或IE)推送数据从服务器到客户端。这是。服务器发送的事件使用持久的HTTP连接,特殊的MIME类型和支持客户端,以便能够随时从服务器向客户端发送事件。与webSocket不同,服务器发送的事件只是一种方式(从服务器到客户端)。然后,客户端将使用传统的Ajax调用,以便能够将数据发送到服务器(而webSocket数据可以通过相同的webSocket连接以任一方式发送)。



下面是关于服务器发送事件如何工作的一个很好的描述:


I have PHP on the server side, and HTML and javascript on the client side.

I am making an app where a stakeholder types a message that is broadcasted to multiple recievers of a group in real time.

I did some research on google and I understand I need to use WebSockets or Comet for real time push notifications. Is WebSocket or Comet mandatory for sending mass notifications to users?

Is my understanding correct? Any references to start with?

解决方案

If the client is a browser, then the ONLY two ways a standard browser can connect to a server is via an Ajax (e.g. http) request or a webSocket connection. So, if you want a client to get notified of something from the outside world it has to use one of those two mechanisms.

HTTP requests are transitory. The client makes a request of a server, the server responds. HTTP requests are perfect for the client requesting information from the server. They are not very good at the server sending information to the client because normally the client is not connected. There are hacks and work-arounds where the client "polls" the server on some interval and maybe even the server uses longer running requests to try to simulate a "push" type system, but they are sub-optimal hacks at best.

webSockets are continuous connections. The client connects and the connection remains in place for as long as both sides want. This allows either side the ability to send a message to the other side whenever they want. That means the server can "push" data to the client whenever it wants. webSockets are efficient for push connections and are recommended (this is one of the main things they were designed for).

Comet is a library that was originally built for using HTTP to try to "hack" or "simulate" push before webSockets were invented and then before they were widely supported. I can think of no reason why one would want to use Comet instead of a webSocket unless you had such an old browser that webSocket was not supported.

So, if you are trying to do "realtime server push" to a browser, then you must have a continuously connected socket from the client which means webSocket (or something built on top of webSocket like socket.io).

For phone apps where you have access to the phone SDK, you can use the "push" system built into the OS to push some messages from server to client. This isn't quite the same as the two way webSocket channel, but since you asked about "push notifications", the OS push services available in both Android and IOS could also be an option for pushing notifications from server to client. Here's info on iOS notifications and Google Cloud Messaging

As of 2016, one can also use Server-sent events in all modern browsers except Microsoft browsers (not supported yet in Edge or IE) to push data from server to client. Here's a browser compatibility table. Server-sent events use a long lasting HTTP connection, a special MIME type and a supporting client in order to be able to send events from server to client at any time. Unlike webSockets, server-sent events are one way only (from server to client). A client would then use a traditional Ajax call in order to be able to send data to a server (whereas with a webSocket data can be sent either way over the same webSocket connection).

Here's a good description of how server-sent events work: How do server-sent events actually work?

这篇关于推送通知| websocket是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 17:57
查看更多