What's the best way to coordinate javascript clients to a single backend game?

前端 未结 3 1720
忘掉有多难
忘掉有多难 2021-01-21 17:44

What\'s the best method to use to notify javascript client\'s of changes that occur in a game asynchronously (i.e. moves made by other clients). As an example, assume a turn bas

3条回答
  •  忘掉有多难
    2021-01-21 18:13

    Polling every second or so is one option, but you may want to consider long polling instead, to reduce the latency.

    Quoting Comet Daily: The Long-Polling Technique:

    The long-polling Comet technique is a technique that optimizes traditional polling to reduce latency.

    Traditional polling sends an XMLHttpRequest to the server in fixed intervals. For example, open a new XMLHttpRequest every 15 seconds, receive an immediate response, and close the connection.

    Long-polling sends a request to the server, but a response is not returned to the client until one is available. As soon as the connection is closed, either due to a response being received by the client or if a request times out, a new connection is initiated. The result is a significant reduction in latency because the server usually has a connection established when it is ready to return information to return to the client.

提交回复
热议问题