简体   繁体   English

将信息从服务器发送到客户端

[英]Send information from a server to a client

I'm looking for a way to send information from a server to a client, for example a song or an image. 我正在寻找一种从服务器向客户端发送信息的方法,例如歌曲或图像。

Explanation : I want to send a data from my server to the clients who downloaded the HTML5 application. 说明:我想从服务器向下载HTML5应用程序的客户端发送数据。

But I dont know how. 但是我不知道如何。 I know I can send a php request from the client to the server and answer afterwards, but how could I send something from the server without the client ask it. 我知道我可以将php请求从客户端发送到服务器,然后再回答,但是我如何在没有客户端请求的情况下从服务器发送一些请求。

Thanks. 谢谢。

You may want to try either Server-Sent Events or WebSockets: 您可能想尝试服务器发送的事件或WebSockets:

These technologies allow a client web application to remain open to communication from the server at any time. 这些技术允许客户端Web应用程序随时保持开放状态,以与服务器进行通信。 Server-Sent Events are exclusively server-to-client, whilst WebSockets can be used for bi-directional communication. 服务器发送的事件专门用于服务器到客户端,而WebSockets可用于双向通信。

Adding to jokeyrhyme's answer... 添加到jokeyrhyme的答案...

You want to asynchronously send data from the server to the client. 您要异步将数据从服务器发送到客户端。 This means the client doesn't know when to expect the data. 这意味着客户端不知道何时需要数据。 In practical terms, on today's Web, you have the following options: 实际上,在当今的Web上,您可以选择以下选项:

  • Some form of polling, long polling, Comet, etc. 某种形式的轮询,长时间轮询,彗星等。
  • WebSocket WebSocket

The first option is better understood since those techniques have been around for a long time. 由于这些技术已经存在了很长时间,因此更好地理解了第一种选择。

WebSocket is newer but is the better solution as it alleviates the problems that plague HTTP-based techniques with polling, long polling, etc. For a small application, or one that polls infrequently, you can get away with polling. WebSocket较新,但它是更好的解决方案,因为它缓解了轮询,长时间轮询等困扰基于HTTP的技术的问题。对于小型应用程序或不频繁轮询的应用程序,您可以摆脱轮询。 But when you want to scale, those solutions run into problems. 但是,当您想扩展规模时,这些解决方案就会遇到问题。

I would not bother with SSE (Server-Sent Events) as that is pretty much a subset of WebSocket. 我不会理会SSE(服务器发送的事件),因为那几乎是WebSocket的一个子集。 Anyone considering SSE usually ends up just using WebSocket since it's about the same amount of work and it gives you more (eg two-way interaction). 考虑使用SSE的任何人通常最终只会使用WebSocket,因为它的工作量几乎相同,并且可以为您提供更多的服务(例如双向交互)。

However WebSocket doesn't replace HTTP; 但是,WebSocket不会取代HTTP; an application can use both at the same time. 一个应用程序可以同时使用两者。 Use the right tool for the right job. 使用正确的工具完成正确的工作。

In your case, have a client with a WebSocket connection. 对于您的情况,有一个具有WebSocket连接的客户端。 Then your backend application can notify the client at any time (asynchronously) that there is something to do (eg a new song or image is available, as you said in your original post). 然后,您的后端应用程序可以随时(异步)通知客户端有事情要做(例如,正如您在原始帖子中所说的,可以使用新的歌曲或图像)。

I would not bother sending the song or image down the WebSocket connection, although you could. 尽管可以,但我不会打扰WebSocket连接发送歌曲或图像。 Instead, the client can fetch the song or image using traditional HTTP techniques, which are well understood and good at handling static content. 取而代之的是,客户端可以使用传统的HTTP技术来获取歌曲或图像,该技术已被很好地理解并且擅长处理静态内容。 For example, you can take advantage of caching if multiple people are downloading the same (song or image) file. 例如,如果多个人正在下载同一文件(歌曲或图像),则可以利用缓存。

So, send the id or URL of the song/image to be downloaded via the WebSocket to the client. 因此,将要通过WebSocket下载的歌曲/图像的ID或URL发送到客户端。 Then fetch the song/image via HTTP. 然后通过HTTP获取歌曲/图像。

That's an example of using both HTTP and WebSocket for their strengths. 这是同时使用HTTP和WebSocket的示例。 WebSocket for the efficient asynchronous interaction with virtually no bandwidth consumption, and HTTP for efficient fetching of static resources. WebSocket用于几乎没有带宽消耗的高效异步交互,而HTTP用于高效地获取静态资源。

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

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