Browser html5 real-time game. broadcast game state

2019-07-27 07:00发布

问题:

I'm making real time game for facebook. Now I broadcast state using websockets. But for real-time data udp is much better. I found out that there is WebRTC out there. But I can't use it because it's peer-to-peer and I want authoritative server(the one which runs physics and makes it harder to cheat).

I consider the following options:

  1. use flash sockets. But I have never used flash. Is it even possible to make a wrapper in javascript for flash udp? How hard is it? Does it require some special stuff on server side?
  2. maybe browser supports udp in some way? Can I stream video or audio to browser using udp? maybe it's possible to use video stream for my game state
  3. use websockets. I have some client-side prediction and I hope that latency would be below 100ms(even lower to be quite frank with you) - so maybe it's ok?
  4. use this WebRTC thing. maybe I can somehow simulate browser on server and use it as peer? For example run a bunch of chromes and send game data to them with websocket(no latency, they are on one machine) and then stream to clients over udp? But browsers can have limit on peer-to-peer connections and I need cpu on server to run physics

What is the best option? Have you implemented one of them and what was the result?

回答1:

You can definitely build or use a server-side component that will support WebRTC data channels; from the browser/client perspective, it will just be another peer.

Strictly speaking, WebRTC data channels aren't UDP based but SCTP based; but in practice, they provide the kind of flexibility you would need for real-time data exchange (with e.g. the ability to set up connections that favor low latency over reliability).

An example of a server-side component to use data channels would be https://www.npmjs.com/package/rtc-dcstream