Differences between websockets and long polling for turn based game server

后端 未结 1 521
滥情空心
滥情空心 2020-12-07 07:37

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.

相关标签:
1条回答
  • 2020-12-07 07:55

    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.

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

    What about Websockets ?

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

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

    Conclusion :

    If there is a need of Real time communication you can very well opt for 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.

    References and other considerations :

    PubNub long polling vs sockets - mobile battery life

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

    long polling in objective-C

    Websocket Introduction

    Websocket Vs Long Polling

    Using Websockets in Apps

    Websocket Application

    PushTechnology-Long Polling

    0 讨论(0)
提交回复
热议问题