基于回合的游戏服务器的websocket和长时间轮询之间的区别 [英] Differences between websockets and long polling for turn based game server

查看:125
本文介绍了基于回合的游戏服务器的websocket和长时间轮询之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

另外,如果我有兴趣也制作一个Web客户端,在websocket和长时间轮询之间还有什么需要考虑的东西吗?

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

推荐答案

什么是长轮询?

What is Long polling ?

传统轮询技术的一种变体,它可以模拟从服务器到客户端的信息推送.使用长轮询时,客户端以与普通轮询类似的方式从服务器请求信息.

A 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.

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

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

那Websockets呢?

What about Websockets ?

WebSocket在客户端和服务器之间提供了持久的连接,双方都可以使用它随时开始发送数据.

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

  • 客户端通过已知过程建立WebSocket连接 作为WebSocket握手.此过程从客户端开始 向服务器发送常规HTTP请求.
  • 此请求中包含一个升级标头,该标头通知服务器 客户端希望建立WebSocket连接.
  • 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.

结论:

如果需要实时通信,则可以选择websockets.

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

但是在长轮询中:

Web客户端和Web服务器之间的连接保持打开状态,以便服务器获取新信息时可以将其推送到客户端.然后该请求完成.然后在客户端和服务器之间发出新请求,然后等待服务器进行另一次更新.由于HTTP/1.1保持活动状态,通常会在多个请求中持续打开同一TCP连接.

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.

参考和其他注意事项:

PubNub长时间轮询与插座-移动电池寿命

什么是长轮询,Websockets ,服务器发送的事件(SSE)和彗星?

在目标C中进行长时间轮询

Websocket简介

Websocket与长轮询

在Apps中使用Websocket

Websocket应用程序

PushTechnology-长轮询

这篇关于基于回合的游戏服务器的websocket和长时间轮询之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆