Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New WebSG Networking APIs #607

Merged
merged 13 commits into from
May 15, 2023
Merged
Binary file modified examples/network/c/build/networked-example.wasm
Binary file not shown.
10 changes: 6 additions & 4 deletions examples/network/c/src/networked-example.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdbool.h>
#include <string.h>
#include "../../../../src/engine/scripting/emscripten/src/websg.h"
#include "../../../../src/engine/scripting/emscripten/src/websg-network.h"
#include "../../../../src/engine/scripting/emscripten/src/websg-networking.h"

node_id_t material_button;
node_id_t room1_switch;
Expand All @@ -23,6 +23,8 @@ bool material_button_state = true;
bool room1_switch_state = true;
bool room2_switch_state = true;

network_listener_id_t network_listener;

int PACKET_BYTES = 3;
bool entered = false;

Expand Down Expand Up @@ -67,12 +69,12 @@ export void websg_load() {

export void websg_enter() {
entered = true;
websg_network_listen();
network_listener = websg_network_listen();
}

export void websg_update(float_t dt) {
// consume net packets
while (entered && websg_network_receive(inbound_network_packet, PACKET_BYTES) > 0) {
while (entered && websg_network_listener_receive(network_listener, inbound_network_packet, PACKET_BYTES) > 0) {
// material_button
material_button_state = inbound_network_packet[0];
websg_material_set_base_color_texture(left_cube_material, material_button_state ? planks_texture : bricks_texture);
Expand Down Expand Up @@ -113,6 +115,6 @@ export void websg_update(float_t dt) {
outbound_network_packet[1] = room1_switch_state ? 1 : 0;
outbound_network_packet[2] = room2_switch_state ? 1 : 0;

websg_network_broadcast(outbound_network_packet, PACKET_BYTES);
websg_network_broadcast(outbound_network_packet, PACKET_BYTES, 1, 1);
}
}
158 changes: 0 additions & 158 deletions examples/websg-v1/websg.d.ts

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"type": "module",
"scripts": {
"dev": "vite",
"dev:testnet": "VITE_USE_TESTNET=true yarn dev",
"build": "tsc && vite build",
"build:scripting-runtime": "./src/engine/scripting/emscripten/build.sh",
"build:examples": "./examples/build.sh",
Expand Down Expand Up @@ -125,7 +124,6 @@
"vite-node": "^0.29.2",
"vite-plugin-cross-origin-isolation": "^0.1.6",
"vite-plugin-static-copy": "^0.13.0",
"vitest": "^0.29.2",
"ws": "^8.5.0"
"vitest": "^0.29.2"
}
}
2 changes: 1 addition & 1 deletion packages/websg-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websg-types",
"version": "0.1.3",
"version": "0.1.4",
"description": "Typescript types for the Web Scene Graph API",
"author": {
"name": "The Matrix.org Foundation",
Expand Down
44 changes: 37 additions & 7 deletions packages/websg-types/types/websg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ declare namespace WebSG {

interface PhysicsBodyProps {
type: PhysicsBodyType;
mass?: number;
linearVelocity?: ArrayLike<number>;
angularVelocity?: ArrayLike<number>;
inertiaTensor?: ArrayLike<number>;
Expand Down Expand Up @@ -530,15 +531,44 @@ declare namespace WebSG {

declare const world: WebSG.World;

interface WebSGNetworking {
listen(): undefined;
close(): undefined;
broadcast(data: ArrayBuffer): undefined;
receive(): ArrayBuffer | undefined;
receiveInto(buffer: ArrayBuffer): number;
declare namespace WebSGNetworking {
class Peer {
get id(): string;
get isHost(): boolean;
get isLocal(): boolean;
get translation(): WebSG.Vector3;
get rotation(): WebSG.Quaternion;
send(message: string | ArrayBuffer, reliable: boolean): undefined;
}

class NetworkMessage {
peer: Peer;
data: ArrayBuffer | string;
bytesWritten: number;
isBinary: boolean;
}

class NetworkMessageIterator {
next(): { value: NetworkMessage; done: boolean };
[Symbol.iterator](): NetworkMessageIterator;
}

class NetworkListener {
receive(buffer?: ArrayBuffer): NetworkMessageIterator;
close(): undefined;
}

class Network {
get host(): Peer | undefined;
get local(): Peer | undefined;
listen(): NetworkListener;
broadcast(message: string | ArrayBuffer, reliable: boolean): undefined;
onpeerentered: ((peer: Peer) => any) | null;
onpeerexited: ((peer: Peer) => any) | null;
}
}

declare const network: WebSGNetworking;
declare const network: WebSGNetworking.Network;

interface ThirdRoom {
enableMatrixMaterial(enabled: boolean): undefined;
Expand Down
24 changes: 16 additions & 8 deletions src/engine/GameWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ async function onInit({
// noop entity
addEntity(world);

function gameWorkerSendMessage<M extends Message<any>>(thread: Thread, message: M, transferList: Transferable[]) {
if (thread === Thread.Main) {
workerScope.postMessage({ dest: thread, message }, transferList);
} else if (thread === Thread.Render) {
renderPort.postMessage({ dest: thread, message }, transferList);
}
}

const ctx: GameState = {
thread: Thread.Game,
renderToGameTripleBufferFlags,
Expand Down Expand Up @@ -91,6 +83,22 @@ async function onInit({
}
};

function gameWorkerSendMessage<M extends Message<any>>(thread: Thread, message: M, transferList: Transferable[]) {
if (thread === Thread.Main) {
workerScope.postMessage({ dest: thread, message }, transferList);
} else if (thread === Thread.Render) {
renderPort.postMessage({ dest: thread, message }, transferList);
} else if (thread === Thread.Game) {
const handlers = ctx.messageHandlers.get(message.type);

if (handlers) {
for (let i = 0; i < handlers.length; i++) {
handlers[i](ctx, message);
}
}
}
}

workerScope.addEventListener("message", onMessage);

if (renderWorkerMessagePort) {
Expand Down
Loading