ios 回合制游戏服务器的 websockets 和长轮询之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31715179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:02:47  来源:igfitidea点击:

Differences between websockets and long polling for turn based game server

iosweb-servicesrestwebsocketlong-polling

提问by acidic

I am writing a server for an iOS game. The game is turn based and the only time the server needs to push information to the client is to notify of the opponent's move.

我正在为 iOS 游戏编写服务器。游戏是回合制的,服务器需要向客户端推送信息的唯一时间是通知对手的移动。

I am curious if anyone could comment on the performance and ease of implementation differences between using websockets and long polling. Also, if I used websockets, should I only use it to receive information and send POST requests for everything else or should all communication be through the websocket?

我很好奇是否有人可以评论使用 websockets 和长轮询之间的性能和易于实现的差异。另外,如果我使用 websockets,我应该只使用它来接收信息并发送其他所有内容的 POST 请求,还是应该通过 websocket 进行所有通信?

Additionally, is there anything to extra to consider between websockets and long polling if I am interested in also making a web client?

此外,如果我也对制作 Web 客户端感兴趣,在 websockets 和长轮询之间还有什么需要考虑的吗?

回答by Tharif

What is Long polling ?

什么是长轮询?

enter image description hereA variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll.

在此处输入图片说明传统轮询技术的一种变体,允许模拟从服务器到客户端的信息推送。对于长轮询,客户端以与正常轮询类似的方式从服务器请求信息。

  • If the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available.
  • Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event.

    In a web/AJAX context, long polling is also known as Comet programming.

  • 如果服务器没有可供客户端使用的任何信息,则服务器不会发送空响应,而是持有请求并等待某些信息可用。
  • 一旦信息可用(或在适当的超时之后),就会向客户端发送完整的响应。客户端通常会立即从服务器重新请求信息,因此服务器几乎总是有一个可用的等待请求,它可以用来传递数据以响应事件。

    在 Web/AJAX 上下文中,长轮询也称为Comet 编程。

What about Websockets ?

Websockets 呢?

enter image description hereWebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time.

在此处输入图片说明WebSockets 提供客户端和服务器之间的持久连接,双方可以使用它随时开始发送数据。

  • The client establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the client sending a regular HTTP request to the server.
  • An Upgrade header is included in this request that informs the server that the client wishes to establish a WebSocket connection.
  • 客户端通过称为 WebSocket 握手的过程建立 WebSocket 连接。此过程从客户端向服务器发送常规 HTTP 请求开始。
  • 此请求中包含一个 Upgrade 标头,用于通知服务器客户端希望建立 WebSocket 连接。

Conclusion:

结论

If there is a need of Real time communication you can very well opt for websockets .

如果需要实时通信,您可以很好地选择 websockets 。

But in Long Polling :

但在长轮询中:

A connection is held open between the web client and the web server so that when the server has new information it can push it to the client. That request is then finished. A new request is then made between the client and the server and then wait for another update from the server. The same TCP connection is generally open persistently throughout multiple requests due to HTTP/1.1 keep-alives.

Web 客户端和 Web 服务器之间的连接保持打开状态,以便当服务器有新信息时可以将其推送到客户端。然后该请求就完成了。然后在客户端和服务器之间发出一个新请求,然后等待来自服务器的另一个更新。由于 HTTP/1.1 keep-alive,同一个 TCP 连接通常在多个请求中持续打开。

References and other considerations :

参考资料和其他注意事项:

PubNub long polling vs sockets - mobile battery life

PubNub 长轮询 vs 套接字 - 移动电池寿命

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?

long polling in objective-C

Objective-C 中的长轮询

Websocket Introduction

Websocket 介绍

Websocket Vs Long Polling

Websocket 与长轮询

Using Websockets in Apps

在应用程序中使用 Websocket

Websocket Application

Websocket 应用程序

PushTechnology-Long Polling

PushTechnology-长轮询