Small tweaks to puter.peer (#2734)

* small tweaks to puter.peer

* document connections field
This commit is contained in:
velzie
2026-03-26 19:34:07 -04:00
committed by GitHub
parent 5c913fc359
commit 017308a741
4 changed files with 20 additions and 12 deletions
+1
View File
@@ -45,6 +45,7 @@ A `Promise` that resolves to a `PuterPeerConnection` instance.
- `send(data)` - Send a message to the peer. Supports strings, `Blob`, `ArrayBuffer`, or `ArrayBufferView`.
- `close(reason)` - Close the connection.
- `owner` (`object`) - Information about the user who created the server.
- `open` event: Fired when the data channel is ready.
- `message` event: Fired when a message is received (`event.data`).
- `close` event: Fired when the connection closes (`event.reason`).
+1
View File
@@ -40,6 +40,7 @@ A `Promise` that resolves to a `PuterPeerServer` instance.
### `PuterPeerServer` properties and events
- `inviteCode` (`string`) The code you share with other clients.
- `connections` (`Map<string, PuterPeerConnection>`) map of all connected clients
- `connection` event: Fired when a client connects.
- `event.conn` (`PuterPeerConnection`) The connection to the client.
- `event.user` (`object`) Metadata about the connecting user (if available).
+7 -6
View File
@@ -42,8 +42,7 @@ class PuterPeerServer extends EventTarget {
#wsconn;
#oncreateresolve;
/** @type {Map<string, PuterPeerConnection>} */
#connections = new Map();
connections = new Map();
inviteCode;
#peerConfig;
@@ -112,7 +111,7 @@ class PuterPeerServer extends EventTarget {
if ( data.server.connect ) {
let uuid = data.server.connect.id;
let connection = new PuterPeerConnection(this.#peerConfig);
this.#connections.set(uuid, connection);
this.connections.set(uuid, connection);
connection.peerconnection.onicecandidate = (e) => {
if ( e.candidate ) {
this.#wsconn.send(
@@ -137,7 +136,7 @@ class PuterPeerServer extends EventTarget {
if ( data.server.candidate ) {
let uuid = data.server.candidate.id;
let connection = this.#connections.get(uuid);
let connection = this.connections.get(uuid);
if ( connection ) {
await connection.addIceCandidate(
data.server.candidate.candidate,
@@ -147,7 +146,7 @@ class PuterPeerServer extends EventTarget {
if ( data.server.offer ) {
let uuid = data.server.offer.id;
let connection = this.#connections.get(uuid);
let connection = this.connections.get(uuid);
if ( connection ) {
await connection.setRemoteDescription(
new RTCSessionDescription(data.server.offer.offer),
@@ -169,7 +168,7 @@ class PuterPeerServer extends EventTarget {
}
close () {
for ( const [uuid, connection] of this.#connections ) {
for ( const [uuid, connection] of this.connections ) {
connection.close();
}
this.#wsconn.onclose = null;
@@ -180,6 +179,7 @@ class PuterPeerServer extends EventTarget {
class PuterPeerConnection extends EventTarget {
#wsconn;
peerconnection;
owner;
#peerConfig;
#datachannel;
connected = false;
@@ -270,6 +270,7 @@ class PuterPeerConnection extends EventTarget {
}
if ( msg.connect ) {
if ( msg.connect.success ) {
this.owner = msg.connect.owner;
const offer = await this.createOffer();
this.#wsconn.send(
JSON.stringify({
+11 -6
View File
@@ -2,7 +2,10 @@ export interface PuterPeerOptions {
iceServers?: RTCIceServer[];
}
export interface PuterPeerUser extends Record<string, unknown> {}
export interface PuterPeerUser {
username: string;
uuid: string;
}
export type PuterPeerMessage = string | Blob | ArrayBuffer | ArrayBufferView;
export type PuterPeerDescription = RTCSessionDescription | RTCSessionDescriptionInit;
@@ -14,17 +17,17 @@ export class PuterPeerServerConnectionEvent extends Event {
}
export class PuterPeerConnectionMessageEvent extends Event {
readonly data: unknown;
readonly data: ArrayBuffer | string;
}
export class PuterPeerConnectionOpenEvent extends Event {}
export class PuterPeerConnectionCloseEvent extends Event {
readonly reason?: unknown;
readonly reason?: string;
}
export class PuterPeerConnectionErrorEvent extends Event {
readonly error: unknown;
readonly error: string;
}
export interface PuterPeerServerEventMap {
@@ -40,9 +43,10 @@ export interface PuterPeerConnectionEventMap {
export class PuterPeerServer extends EventTarget {
inviteCode?: string;
connections: Map<string, PuterPeerConnection>;
start (): Promise<string>;
message (data: unknown): Promise<void>;
message (data: ArrayBuffer | string): Promise<void>;
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
addEventListener<K extends keyof PuterPeerServerEventMap>(
@@ -60,11 +64,12 @@ export class PuterPeerServer extends EventTarget {
export class PuterPeerConnection extends EventTarget {
peerconnection: RTCPeerConnection;
owner?: PuterPeerUser;
connected: boolean;
closed: boolean;
connect (invitecode: string): Promise<void>;
close (reason?: unknown): void;
close (reason?: string): void;
createOffer (): Promise<RTCSessionDescriptionInit>;
createAnswer (): Promise<RTCSessionDescriptionInit>;
setRemoteDescription (description: PuterPeerDescription): void;