Skip to content

Latest commit

 

History

History
 
 

net

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

@javelin/net

Networking utilities for Javelin ECS.

Message Producer

Produce unreliable and reliable Javelin network protocol messages for a world.

Usage

import { createWorld } from "@javelin/ecs"
import { createMessageProducer } from "@javelin/net"

const world = createWorld({ ... })
const messageProducer = createMessageProducer({
  components: [
    // send components reliably when they change
    { type: Health },
    // send components unreliably by specifying a priority
    { type: Position, priority: 1 },
  ],
  // send unreliable updates 30 times a second
  updateInterval: (1 / 30) * 1000,
  // send a maximum of 1000 components per unreliable update
  updateSize: 1000,
})

const onClientConnect = client =>
  // send initial messages to new clients over reliable channel
  client.sendReliable(messageProducer.getInitialMessages(world))

const loop = () => {
  world.tick()

  const reliable = messageProducer.getReliableMessages(world)
  const unreliable = messageProducer.getUnreliableMessages(world)

  for (const client of clients) {
    client.sendReliable(reliable)
    client.sendUnreliable(unreliable)
  }
}

Message Handler

Apply Javelin network protocol messages to a world.

Usage

import { createWorld } from "@javelin/ecs"
import { createMessageHandler } from "@javelin/net"

const messageHandler = createMessageHandler()
const world = createWorld({
  systems: [messageHandler.system],
})

const client = await server.connect()

// apply remote messages to local world next tick
client.reliable.onMessage(messageHandler.push)
client.unreliable.onMessage(messageHandler.push)