@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
428 lines (427 loc) • 18.6 kB
TypeScript
import * as flatbuffers from 'flatbuffers';
import { PeerNetworking } from './engine_networking_peer.js';
import { type IModel, type INetworkConnection, SendQueue } from './engine_networking_types.js';
import { Context } from './engine_setup.js';
export declare const debugNet: boolean;
export declare const debugOwner: boolean;
export interface INetworkingWebsocketUrlProvider {
getWebsocketUrl(): string | null;
}
export declare interface IConnectionData {
id: string;
}
/** Events regarding the websocket connection (e.g. when the connection opens) */
export declare enum ConnectionEvents {
ConnectionInfo = "connection-start-info"
}
/** Use to listen to room networking events like joining a networked room
* For example: `this.context.connection.beginListen(RoomEvents.JoinedRoom, () => { })`
* @link https://engine.needle.tools/docs/networking.html#manual-networking
* */
export declare enum RoomEvents {
/** Internal: Sent to the server when attempting to join a room */
Join = "join-room",
/** Internal: Sent to the server when attempting to leave a room */
Leave = "leave-room",
/** Incoming: When the local user has joined a room */
JoinedRoom = "joined-room",
/** Incoming: When the local user has left a room */
LeftRoom = "left-room",
/** Incoming: When a other user has joined the room */
UserJoinedRoom = "user-joined-room",
/** Incoming: When a other user has left the room */
UserLeftRoom = "user-left-room",
/** When a user joins a room, the server sends the entire room state. Afterwards, the server sends the room-state-sent event. */
RoomStateSent = "room-state-sent"
}
/**
* See {@link RoomEvents} for all event names and docs.
* - `joined-room`: When the local user has joined a room
* - `left-room`: When the local user has left a room
* - `user-joined-room`: When a other user has joined the room
* - `user-left-room`: When a other user has left the room
* - `room-state-sent`: When the server has sent the room state to the client
*/
type RoomEventsIncoming = Exclude<`${RoomEvents}`, "join-room" | "leave-room">;
/** Received when listening to `RoomEvents.JoinedRoom` event */
export declare class JoinedRoomResponse {
room: string;
viewId: string;
allowEditing: boolean;
inRoom: string[];
}
export declare class LeftRoomResponse {
room: string;
}
export declare class UserJoinedOrLeftRoomModel {
userId: string;
}
/** The Needle Engine networking server supports the concept of ownership that can be requested.
* This enum contains possible outgoing (Request*) and incoming (Response*) events for communicating ownership.
*
* Typically, using the {@link OwnershipModel} class instead of dealing with those events directly is preferred. */
export declare enum OwnershipEvent {
RequestHasOwner = "request-has-owner",
ResponseHasOwner = "response-has-owner",
RequestIsOwner = "request-is-owner",
ResponseIsOwner = "response-is-owner",
RequestOwnership = "request-ownership",
GainedOwnership = "gained-ownership",
RemoveOwnership = "remove-ownership",
LostOwnership = "lost-ownership",
GainedOwnershipBroadcast = "gained-ownership-broadcast",
LostOwnershipBroadcast = "lost-ownership-broadcast"
}
type OwnershipEventNamesIncoming = Exclude<`${OwnershipEvent}`, "request-has-owner" | "request-is-owner" | "request-ownership" | "remove-ownership">;
declare type WebsocketSendType = IModel | object | boolean | null | string | number;
/**
* Manages ownership of networked objects or components.
*
* In multiplayer scenarios, ownership determines which client has authority to modify an object.
* The networking server rejects changes from clients that don't own an object. This prevents conflicts
* when multiple users try to manipulate the same object simultaneously.
*
* **Ownership states:**
* - `hasOwnership`: This client owns the object and can modify it
* - `isOwned`: Some client (could be local or remote) owns the object
* - `undefined`: Ownership state is unknown (not yet queried)
*
* **Typical workflow:**
* 1. Request ownership before modifying an object
* 2. Make your changes while you have ownership
* 3. Free ownership when done (or keep it if still interacting)
*
* @example Basic usage
* ```ts
* export class MyComponent extends Behaviour {
* private ownership?: OwnershipModel;
*
* awake() {
* this.ownership = new OwnershipModel(this.context.connection, this.guid);
* }
*
* onClick() {
* // Request ownership before modifying the object
* this.ownership.requestOwnership();
* }
*
* update() {
* if (this.ownership.hasOwnership) {
* // Safe to modify and sync the object
* this.gameObject.position.y += 0.01;
* }
* }
*
* onDisable() {
* // Release ownership when done
* this.ownership.freeOwnership();
* this.ownership.destroy();
* }
* }
* ```
*
* @example Async ownership
* ```ts
* async modifyObject() {
* try {
* await this.ownership.requestOwnershipAsync();
* // Now guaranteed to have ownership
* this.transform.position.x = 5;
* } catch(e) {
* console.log("Failed to gain ownership");
* }
* }
* ```
*
* @see {@link SyncedTransform} for a complete example of ownership in action
* @link https://engine.needle.tools/docs/networking.html
*/
export declare class OwnershipModel {
/** The unique identifier (GUID) of the object this ownership model manages */
guid: string;
private connection;
/**
* Checks if the local client has ownership of this object.
* @returns `true` if this client owns the object and can modify it, `false` otherwise
*/
get hasOwnership(): boolean;
/**
* Checks if anyone (local or remote client) has ownership of this object.
* @returns `true` if someone owns the object, `false` if no one owns it, `undefined` if unknown
*/
get isOwned(): boolean | undefined;
/**
* Checks if Needle Engine networking is connected to a websocket. Note that this is **not equal** to being connected to a *room*. If you want to check if Needle Engine is connected to a networking room use the `isInRoom` property.
* @returns true if connected to the websocket.
*/
get isConnected(): boolean;
private _hasOwnership;
private _isOwned;
private _gainSubscription;
private _lostSubscription;
private _hasOwnerResponse;
constructor(connection: NetworkConnection, guid: string);
private _isWaitingForOwnershipResponseCallback;
/**
* Queries the server to update the `isOwned` state.
* Call this to check if anyone currently has ownership.
*/
updateIsOwned(): void;
private onHasOwnerResponse;
/**
* Requests ownership only if the object is not currently owned by anyone.
* Internally checks ownership state first, then requests ownership if free.
* @returns this OwnershipModel instance for method chaining
*/
requestOwnershipIfNotOwned(): OwnershipModel;
private waitForHasOwnershipRequestResponse;
/**
* Requests ownership and waits asynchronously until ownership is granted or timeout occurs.
* @returns Promise that resolves with this OwnershipModel when ownership is gained
* @throws Rejects with "Timeout" if ownership is not gained within ~1 second
* @example
* ```ts
* try {
* await ownership.requestOwnershipAsync();
* // Ownership granted, safe to modify object
* } catch(e) {
* console.warn("Could not gain ownership:", e);
* }
* ```
*/
requestOwnershipAsync(): Promise<OwnershipModel>;
/**
* Requests ownership of this object from the networking server.
* Ownership may not be granted immediately - check `hasOwnership` property or use `requestOwnershipAsync()`.
* @returns this OwnershipModel instance for method chaining
*/
requestOwnership(): OwnershipModel;
/**
* Releases ownership of this object, allowing others to take control.
* Call this when you're done modifying an object to allow other users to interact with it.
* @returns this OwnershipModel instance for method chaining
*/
freeOwnership(): OwnershipModel;
/**
* Cleans up event listeners and resources.
* Call this when the OwnershipModel is no longer needed (e.g., in `onDestroy()`).
*/
destroy(): void;
private onGainedOwnership;
private onLostOwnership;
}
export declare type BinaryCallback = {
(data: any | flatbuffers.ByteBuffer): void;
};
/**
* Main class for multiuser networking. Access via `this.context.connection` from any component.
*
* **About GUIDs:**
* In Needle Engine networking, GUIDs (Globally Unique Identifiers) are used to identify objects and components across the network.
* Every GameObject and Component has a unique `guid` property that remains consistent across all clients.
* GUIDs are automatically assigned (e.g. during export from Unity/Blender) and are essential for:
* - Object ownership management (see {@link OwnershipModel})
* - State synchronization (storing and retrieving object state)
* - Identifying which object received a network message
*
* When working with networking, you'll typically use `this.guid` to identify your component or `this.gameObject.guid` for the GameObject.
*
* @example Joining a room
* ```ts
* this.context.connection.connect();
* this.context.connection.joinRoom("my-room");
* ```
* @example Listening to events
* ```ts
* this.context.connection.beginListen("my-event", (data) => {
* console.log("Received:", data);
* });
* ```
* @example Sending data
* ```ts
* this.context.connection.send("my-event", { message: "Hello" });
* ```
* @example Using GUIDs for object identification
* ```ts
* // Get state for a specific object by its GUID
* const state = this.context.connection.tryGetState(this.guid);
*
* // Delete remote state for an object
* this.context.connection.sendDeleteRemoteState(this.guid);
* ```
* @see {@link RoomEvents} for room lifecycle events
* @see {@link OwnershipModel} for object ownership
* @link https://engine.needle.tools/docs/how-to-guides/networking/
*/
export declare class NetworkConnection implements INetworkConnection {
private context;
private _peer;
constructor(context: Context);
/** Experimental: networking via peerjs */
get peer(): PeerNetworking;
/**
* Returns the cached network state for a given GUID.
* The state is stored locally whenever network updates are received for that object.
* @param guid The unique identifier of the object whose state you want to retrieve
* @returns The cached state object, or `null` if no state exists for this GUID
* @example
* ```ts
* // Get the last known state for this component
* const myState = this.context.connection.tryGetState(this.guid);
* if (myState) {
* console.log("Found cached state:", myState);
* }
* ```
*/
tryGetState(guid: string): IModel | null;
/** The connection id of the local user - it is given by the networking backend and can not be changed */
get connectionId(): string | null;
/** Returns true if the networking backend is in debug mode.
* To see all networking messages in the console use `?debugnet` in the url
*/
get isDebugEnabled(): boolean;
/**
* Checks if Needle Engine networking is connected to a websocket. Note that this is **not equal** to being connected to a *room*. If you want to check if Needle Engine is connected to a networking room use the `{@link isInRoom}` property.
* @returns true if connected to the websocket.
*/
get isConnected(): boolean;
/** The name of the room the user is currently connected to */
get currentRoomName(): string | null;
/** True when connected to a room via a regular url, otherwise (when using a view only url) false indicating that the user should not be able to modify the scene */
get allowEditing(): boolean;
/**
* The view id of the room the user is currently connected to.
*/
get currentRoomViewId(): string | null;
/**
* Returns a url that can be shared with others to view the current room in view only mode.
* This is useful for sharing a room with others without allowing them to modify the scene.
* Use `connection.allowEditing` to check if the current room is in view only mode.
*/
getViewOnlyUrl(): string | null;
/** True if connected to a networked room. Use the joinRoom function or a `SyncedRoom` component */
get isInRoom(): boolean;
/** Latency to currently connected backend server */
get currentLatency(): number;
/**
* The current server url that the networking backend is connected to (e.g. the url of the websocket server)
*/
get currentServerUrl(): string | null;
/** A ping is sent to the server at a regular interval while the browser tab is active. This method can be used to send additional ping messages when needed so that the user doesn't get disconnected from the networking backend */
sendPing(): void;
/** Returns true if a user with the given connectionId is in the room */
userIsInRoom(id: string): boolean;
private _usersInRoomCopy;
/** Returns a list of all user ids in the current room */
usersInRoom(target?: string[] | null): string[];
/** Joins a networked room. If you don't want to manage a connection yourself you can use a `{@link SyncedRoom}` component as well */
joinRoom(room: string, viewOnly?: boolean): boolean;
/** Use to leave a room that you are currently connected to (use `leaveRoom()` to disconnect from the currently active room but you can also specify a room name) */
leaveRoom(room?: string | null): boolean;
/** Send a message to the networking backend - it will broadcasted to all connected users in the same room by default */
send<T extends WebsocketSendType>(key: string | OwnershipEvent, data?: T | null, queue?: SendQueue): void;
/**
* Deletes the network state for a specific object on the server.
* This removes the object's state from the room, preventing it from being sent to newly joining users.
* @param guid The unique identifier of the object whose state should be deleted
* @example
* ```ts
* // When destroying a networked object, clean up its server state
* onDestroy() {
* this.context.connection.sendDeleteRemoteState(this.guid);
* }
* ```
*/
sendDeleteRemoteState(guid: string): void;
/** Use to delete all state in the currently connected room on the server */
sendDeleteRemoteStateAll(): void;
/** Send a binary message to the server (broadcasted to all connected users) */
sendBinary(bin: Uint8Array): void;
private _defaultMessagesBuffer;
private _defaultMessagesBufferArray;
sendBufferedMessagesNow(): void;
/** Use to start listening to networking events.
* To unsubscribe from events use the `{@link stopListen}` method.
* See the example below for typical usage:
*
* ### Component Example
* ```ts
* // Make sure to unsubscribe from events when the component is disabled
* export class MyComponent extends Behaviour {
* onEnable() {
* this.connection.beginListen("joined-room", this.onJoinedRoom)
* }
* onDisable() {
* this.connection.stopListen("joined-room", this.onJoinedRoom)
* }
* onJoinedRoom = () => {
* console.log("I joined a networked room")
* }
* }
* ```
* @link https://engine.needle.tools/docs/networking.html
*
*/
beginListen(key: (string & {}) | OwnershipEvent | OwnershipEventNamesIncoming | RoomEventsIncoming | RoomEvents, callback: Function): Function;
/**@deprecated please use stopListen instead (2.65.2-pre) */
stopListening(key: (string & {}) | OwnershipEvent | OwnershipEventNamesIncoming | RoomEventsIncoming | RoomEvents, callback: Function | null): void;
/** Use to stop listening to networking events
* To subscribe to events use the `{@link beginListen}` method.
* See the example below for typical usage:
*
* ### Component Example
* ```ts
* // Make sure to unsubscribe from events when the component is disabled
* export class MyComponent extends Behaviour {
* onEnable() {
* this.connection.beginListen("joined-room", this.onJoinedRoom)
* }
* onDisable() {
* this.connection.stopListen("joined-room", this.onJoinedRoom)
* }
* onJoinedRoom = () => {
* console.log("I joined a networked room")
* }
* }
* ```
*/
stopListen(key: (string & {}) | OwnershipEvent | OwnershipEventNamesIncoming | RoomEventsIncoming | RoomEvents, callback: Function | null): void;
/** Use to start listening to networking binary events */
beginListenBinary(identifier: string, callback: BinaryCallback): BinaryCallback;
/** Use to stop listening to networking binary events */
stopListenBinary(identifier: string, callback: any): void;
private netWebSocketUrlProvider?;
/** Use to override the networking server backend url.
* This is what the `{@link Networking}` component uses to modify the backend url.
**/
registerProvider(prov: INetworkingWebsocketUrlProvider): void;
/** Used to connect to the networking server
* @param url Optional url to connect to. If not provided, it will use the url from the registered `INetworkingWebsocketUrlProvider` or the default backend networking url. If you want to change the url after connecting, you need to disconnect first and then connect again with the new url.
*/
connect(url?: string): Promise<boolean>;
/** Disconnect from the networking backend + reset internal state */
disconnect(): void;
private _listeners;
private _listenersBinary;
private connected;
private channelId;
private _connectionId;
private _ws;
private _waitingForSocket;
private _isInRoom;
private _currentRoomName;
private _currentRoomViewId;
private _currentRoomAllowEditing;
private _currentInRoom;
private _state;
private _currentDelay;
private _connectingToWebsocketPromise;
private connectWebsocket;
private onMessage;
private handleIncomingBinaryMessage;
private handleIncomingStringMessage;
private toMessage;
private sendWithWebsocket;
private onSendQueued;
}
export {};