当前位置:网站首页>Explore using golang and webassembly to build a multiplayer game server

Explore using golang and webassembly to build a multiplayer game server

2021-05-04 14:17:54 For less

What is? WebAssembly? from GoogleMicrosoftMozillaApple And so on WebAssembly Is a new bytecode format , Mainstream browsers already support WebAssembly. and JS Need to explain the difference in execution ,WebAssembly Bytecode is very similar to the underlying machine code, which can be loaded and run quickly , So the performance is relative to JS Explain that execution has greatly improved .WebAssembly It's not a programming language , It's a bytecode standard , We need to compile bytecode in advanced programming language and put it in WebAssembly Virtual machine to run .

Panzr.io brief introduction

  • Games based on open source technology
  • Use Web As a distribution platform
  • Light and fast
  • Explore basic multiplayer Technology
  • Expand Go technical knowledge

Panzr.io framework

Panzr.io Deployment architecture

Triebwerk brief introduction

Project source code :

project Status:

  • Triebwerk Is an open source multiplayer game server
  • Use Go Language writing
  • Now it's just the basic prototype

How the game works ?

Server authority Architecture

  • Communicate only through the server
  • The client sends all input to the server
  • The server has the right to simulate
  • Prevent cheating and introduce delays

Client prediction and server coordination

  • The earliest by QuakeWorld Extension
  • Local simulation movement
  • Constantly synchronize with server state
  • Correct local state based on server state

Client side interpolation

  • net update (Updates) < Frames per second (Frames)
  • Interpolation between past states
  • Conservative algorithm
  • No inference

Define boundaries

Limit :

  • All the game logic is just 2D In the space
  • Uniform surface
  • Input control via keyboard only
  • Limit map size
  • Slow moving vehicles
  • There's no physical engine

Server implementation

Players move

collision detection

Binary data transfer

  • Minimize resource use
  • Prevent packet fragmentation
  • Minimize the impact of packet loss

WebAssembly modular

The game logic (Game logic)Server -> Client

  • file size > 2MB
  • The server and the client compute state based on the same logic
  • Data transfer through binary type

compile :

GOOS=js GOARCH=wasm go build -o tanks.wasm cmd/wasm/tanks.go

Client:

<script src="/game/wass_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(featch("/game/tanks.wass"), go.importObject).then(result => {
    go.run(result.instance);
});
</script>

Server:

js.Global().Set("updateNetworkPlayer", js.FuncOf(updateNetworkPlayer))

stay Go Code in state

posX := float32(30.457777)
posY := float32(10.336666)
buf := make([]byte, 8)

binary.LittleEndian.PutUint32(buf[0:], math.Float32bits(posX))
binary.LittleEndian.PutUint32(buf[4:], math.Float32bits(posY))

var uint8Array = js.Global().Get("Uint8Array")
dst := uint8Array.New(len(buf))
js.CopyBytesToJS(dst, buf)

stay Javascript Medium decoding state

let dv = new DataView(state.buffer)
let posX = dv.getFloat32(0, true)
let posY = dv.getFloat32(4, true)

Try online

http://panzr.io

Refs

 I'm for less 
 WeChat :uuhells123
 official account : Hacker afternoon tea 
 Add me WeChat ( Learn from each other ), Official account ( Get more learning materials ~)

版权声明
本文为[For less]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504141524047Y.html

随机推荐