@mml-io/3d-web-user-networking 0.0.0-experimental-1dac27a-20240426 → 0.0.0-experimental-b569a23-20240430
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/UserNetworkingServer.d.ts +1 -1
- package/build/index.js +6 -6
- package/build/index.js.map +2 -2
- package/package.json +2 -2
@@ -16,7 +16,7 @@ export type UserNetworkingServerOptions = {
|
|
16
16
|
};
|
17
17
|
export declare class UserNetworkingServer {
|
18
18
|
private options;
|
19
|
-
private
|
19
|
+
private allClientsById;
|
20
20
|
private authenticatedClientsById;
|
21
21
|
constructor(options: UserNetworkingServerOptions);
|
22
22
|
private heartBeat;
|
package/build/index.js
CHANGED
@@ -46,7 +46,7 @@ var WebSocketOpenStatus = 1;
|
|
46
46
|
var UserNetworkingServer = class {
|
47
47
|
constructor(options) {
|
48
48
|
this.options = options;
|
49
|
-
this.
|
49
|
+
this.allClientsById = /* @__PURE__ */ new Map();
|
50
50
|
this.authenticatedClientsById = /* @__PURE__ */ new Map();
|
51
51
|
setInterval(this.sendUpdates.bind(this), packetsUpdateRate);
|
52
52
|
setInterval(this.pingClients.bind(this), pingPongRate);
|
@@ -54,7 +54,7 @@ var UserNetworkingServer = class {
|
|
54
54
|
}
|
55
55
|
heartBeat() {
|
56
56
|
const now = Date.now();
|
57
|
-
this.
|
57
|
+
this.allClientsById.forEach((client) => {
|
58
58
|
if (now - client.lastPong > heartBeatRate) {
|
59
59
|
client.socket.close();
|
60
60
|
this.handleDisconnectedClient(client);
|
@@ -70,7 +70,7 @@ var UserNetworkingServer = class {
|
|
70
70
|
}
|
71
71
|
getId() {
|
72
72
|
let id = 1;
|
73
|
-
while (this.
|
73
|
+
while (this.allClientsById.has(id)) {
|
74
74
|
id++;
|
75
75
|
}
|
76
76
|
return id;
|
@@ -90,7 +90,7 @@ var UserNetworkingServer = class {
|
|
90
90
|
state: 0
|
91
91
|
}
|
92
92
|
};
|
93
|
-
this.
|
93
|
+
this.allClientsById.set(id, client);
|
94
94
|
socket.on("message", (message, _isBinary) => {
|
95
95
|
if (message instanceof Buffer) {
|
96
96
|
const arrayBuffer = new Uint8Array(message).buffer;
|
@@ -138,10 +138,10 @@ var UserNetworkingServer = class {
|
|
138
138
|
});
|
139
139
|
}
|
140
140
|
handleDisconnectedClient(client) {
|
141
|
-
if (!this.
|
141
|
+
if (!this.allClientsById.has(client.id)) {
|
142
142
|
return;
|
143
143
|
}
|
144
|
-
this.
|
144
|
+
this.allClientsById.delete(client.id);
|
145
145
|
if (client.authenticatedUser !== null) {
|
146
146
|
this.options.onClientDisconnect(client.id);
|
147
147
|
this.authenticatedClientsById.delete(client.id);
|
package/build/index.js.map
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"version": 3,
|
3
3
|
"sources": ["../src/UserNetworkingCodec.ts", "../src/user-networking-settings.ts", "../src/UserNetworkingMessages.ts", "../src/UserNetworkingServer.ts", "../src/ReconnectingWebSocket.ts", "../src/UserNetworkingClient.ts"],
|
4
|
-
"sourcesContent": ["export type UserNetworkingClientUpdate = {\n id: number;\n position: { x: number; y: number; z: number };\n rotation: { quaternionY: number; quaternionW: number };\n state: number;\n};\n\nexport class UserNetworkingCodec {\n static encodeUpdate(update: UserNetworkingClientUpdate): Uint8Array {\n const buffer = new ArrayBuffer(19);\n const dataView = new DataView(buffer);\n dataView.setUint16(0, update.id); // id\n dataView.setFloat32(2, update.position.x); // position.x\n dataView.setFloat32(6, update.position.y); // position.y\n dataView.setFloat32(10, update.position.z); // position.z\n dataView.setInt16(14, update.rotation.quaternionY * 32767); // quaternion.y\n dataView.setInt16(16, update.rotation.quaternionW * 32767); // quaternion.w\n dataView.setUint8(18, update.state); // animationState\n return new Uint8Array(buffer);\n }\n\n static decodeUpdate(buffer: ArrayBuffer): UserNetworkingClientUpdate {\n const dataView = new DataView(buffer);\n const id = dataView.getUint16(0); // id\n const x = dataView.getFloat32(2); // position.x\n const y = dataView.getFloat32(6); // position.y\n const z = dataView.getFloat32(10); // position.z\n const quaternionY = dataView.getInt16(14) / 32767; // quaternion.y\n const quaternionW = dataView.getInt16(16) / 32767; // quaternion.w\n const state = dataView.getUint8(18); // animationState\n const position = { x, y, z };\n const rotation = { quaternionY, quaternionW };\n return { id, position, rotation, state };\n }\n}\n", "export const pingPongRate: number = 1500;\nexport const heartBeatRate: number = 15000;\nexport const packetsUpdateRate: number = (1 / 30) * 1000;\n", "export const DISCONNECTED_MESSAGE_TYPE = \"disconnected\";\nexport const IDENTITY_MESSAGE_TYPE = \"identity\";\nexport const USER_AUTHENTICATE_MESSAGE_TYPE = \"user_auth\";\nexport const USER_PROFILE_MESSAGE_TYPE = \"user_profile\";\nexport const USER_UPDATE_MESSAGE_TYPE = \"user_update\";\nexport const PING_MESSAGE_TYPE = \"ping\";\nexport const PONG_MESSAGE_TYPE = \"pong\";\n\nexport type IdentityMessage = {\n type: typeof IDENTITY_MESSAGE_TYPE;\n id: number;\n};\n\nexport type CharacterDescription = {\n meshFileUrl?: string;\n mmlCharacterUrl?: string;\n mmlCharacterString?: string;\n} & (\n | {\n meshFileUrl: string;\n }\n | {\n mmlCharacterUrl: string;\n }\n | {\n mmlCharacterString: string;\n }\n);\n\nexport type UserProfileMessage = {\n type: typeof USER_PROFILE_MESSAGE_TYPE;\n id: number;\n username: string;\n characterDescription: CharacterDescription;\n};\n\nexport type DisconnectedMessage = {\n type: typeof DISCONNECTED_MESSAGE_TYPE;\n id: number;\n};\n\nexport type FromServerPingMessage = {\n type: typeof PING_MESSAGE_TYPE;\n};\n\nexport type FromServerMessage =\n | IdentityMessage\n | UserProfileMessage\n | DisconnectedMessage\n | FromServerPingMessage;\n\nexport type FromClientPongMessage = {\n type: typeof PONG_MESSAGE_TYPE;\n};\n\nexport type UserIdentity = {\n characterDescription: CharacterDescription | null;\n username: string | null;\n};\n\nexport type UserAuthenticateMessage = {\n type: typeof USER_AUTHENTICATE_MESSAGE_TYPE;\n sessionToken: string;\n // The client can send a UserIdentity to use as the initial user profile and the server can choose to accept it or not\n userIdentity?: UserIdentity;\n};\n\nexport type UserUpdateMessage = {\n type: typeof USER_UPDATE_MESSAGE_TYPE;\n userIdentity: UserIdentity;\n};\n\nexport type FromClientMessage = FromClientPongMessage | UserAuthenticateMessage | UserUpdateMessage;\n", "import WebSocket from \"ws\";\n\nimport { heartBeatRate, packetsUpdateRate, pingPongRate } from \"./user-networking-settings\";\nimport { UserData } from \"./UserData\";\nimport { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\nimport {\n DISCONNECTED_MESSAGE_TYPE,\n FromClientMessage,\n FromServerMessage,\n IDENTITY_MESSAGE_TYPE,\n PONG_MESSAGE_TYPE,\n USER_AUTHENTICATE_MESSAGE_TYPE,\n USER_PROFILE_MESSAGE_TYPE,\n USER_UPDATE_MESSAGE_TYPE as USER_UPDATE_MESSAGE_TYPE,\n UserAuthenticateMessage,\n UserIdentity,\n UserUpdateMessage,\n} from \"./UserNetworkingMessages\";\n\nexport type Client = {\n socket: WebSocket;\n id: number;\n lastPong: number;\n update: UserNetworkingClientUpdate;\n authenticatedUser: UserData | null;\n};\n\nconst WebSocketOpenStatus = 1;\n\nexport type UserNetworkingServerOptions = {\n onClientConnect: (\n clientId: number,\n sessionToken: string,\n userIdentity?: UserIdentity,\n ) => Promise<UserData | null> | UserData | null;\n onClientUserIdentityUpdate: (\n clientId: number,\n userIdentity: UserIdentity,\n ) => Promise<UserData | null> | UserData | null;\n onClientDisconnect: (clientId: number) => void;\n};\n\nexport class UserNetworkingServer {\n private allClients = new Set<Client>();\n private authenticatedClientsById: Map<number, Client> = new Map();\n\n constructor(private options: UserNetworkingServerOptions) {\n setInterval(this.sendUpdates.bind(this), packetsUpdateRate);\n setInterval(this.pingClients.bind(this), pingPongRate);\n setInterval(this.heartBeat.bind(this), heartBeatRate);\n }\n\n private heartBeat() {\n const now = Date.now();\n this.allClients.forEach((client) => {\n if (now - client.lastPong > heartBeatRate) {\n client.socket.close();\n this.handleDisconnectedClient(client);\n }\n });\n }\n\n private pingClients() {\n this.authenticatedClientsById.forEach((client) => {\n if (client.socket.readyState === WebSocketOpenStatus) {\n client.socket.send(JSON.stringify({ type: \"ping\" } as FromServerMessage));\n }\n });\n }\n\n private getId(): number {\n let id = 1;\n while (this.authenticatedClientsById.has(id)) {\n id++;\n }\n return id;\n }\n\n public connectClient(socket: WebSocket) {\n const id = this.getId();\n console.log(`Client ID: ${id} joined, waiting for user-identification`);\n\n // Create a client but without user-information\n const client: Client = {\n id,\n lastPong: Date.now(),\n socket: socket as WebSocket,\n authenticatedUser: null,\n update: {\n id,\n position: { x: 0, y: 0, z: 0 },\n rotation: { quaternionY: 0, quaternionW: 1 },\n state: 0,\n },\n };\n this.allClients.add(client);\n\n socket.on(\"message\", (message: WebSocket.Data, _isBinary: boolean) => {\n if (message instanceof Buffer) {\n const arrayBuffer = new Uint8Array(message).buffer;\n const update = UserNetworkingCodec.decodeUpdate(arrayBuffer);\n update.id = id;\n client.update = update;\n } else {\n let parsed;\n try {\n parsed = JSON.parse(message as string) as FromClientMessage;\n } catch (e) {\n console.error(\"Error parsing JSON message\", message, e);\n return;\n }\n if (!client.authenticatedUser) {\n if (parsed.type === USER_AUTHENTICATE_MESSAGE_TYPE) {\n this.handleUserAuth(client, parsed).then((authResult) => {\n if (!authResult) {\n // If the user is not authorized, disconnect the client\n socket.close();\n } else {\n this.authenticatedClientsById.set(id, client);\n }\n });\n } else {\n console.error(`Unhandled message pre-auth: ${JSON.stringify(parsed)}`);\n socket.close();\n }\n } else {\n switch (parsed.type) {\n case PONG_MESSAGE_TYPE:\n client.lastPong = Date.now();\n break;\n\n case USER_UPDATE_MESSAGE_TYPE:\n this.handleUserUpdate(id, parsed as UserUpdateMessage);\n break;\n\n default:\n console.error(`Unhandled message: ${JSON.stringify(parsed)}`);\n }\n }\n }\n });\n\n socket.on(\"close\", () => {\n console.log(\"Client disconnected\", id);\n this.handleDisconnectedClient(client);\n });\n }\n\n private handleDisconnectedClient(client: Client) {\n if (!this.allClients.has(client)) {\n return;\n }\n this.allClients.delete(client);\n if (client.authenticatedUser !== null) {\n // Only report disconnections of clients that were authenticated\n this.options.onClientDisconnect(client.id);\n this.authenticatedClientsById.delete(client.id);\n const disconnectMessage = JSON.stringify({\n id: client.id,\n type: DISCONNECTED_MESSAGE_TYPE,\n } as FromServerMessage);\n for (const [, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(disconnectMessage);\n }\n }\n }\n }\n\n private async handleUserAuth(\n client: Client,\n credentials: UserAuthenticateMessage,\n ): Promise<boolean> {\n const userData = this.options.onClientConnect(\n client.id,\n credentials.sessionToken,\n credentials.userIdentity,\n );\n let resolvedUserData;\n if (userData instanceof Promise) {\n resolvedUserData = await userData;\n } else {\n resolvedUserData = userData;\n }\n if (resolvedUserData === null) {\n console.error(`Client-id ${client.id} user_auth unauthorized and ignored`);\n return false;\n }\n\n console.log(\"Client authenticated\", client.id, resolvedUserData);\n client.authenticatedUser = resolvedUserData;\n\n const identityMessage = JSON.stringify({\n id: client.id,\n type: IDENTITY_MESSAGE_TYPE,\n } as FromServerMessage);\n\n const userProfileMessage = JSON.stringify({\n id: client.id,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: resolvedUserData.username,\n characterDescription: resolvedUserData.characterDescription,\n } as FromServerMessage);\n\n client.socket.send(userProfileMessage);\n client.socket.send(identityMessage);\n\n const userUpdateMessage = UserNetworkingCodec.encodeUpdate(client.update);\n\n // Send information about all other clients to the freshly connected client and vice versa\n for (const [, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState !== WebSocketOpenStatus || otherClient === client) {\n // Do not send updates for any clients which have not yet authenticated or not yet connected\n continue;\n }\n // Send the character information\n client.socket.send(\n JSON.stringify({\n id: otherClient.update.id,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: otherClient.authenticatedUser?.username,\n characterDescription: otherClient.authenticatedUser?.characterDescription,\n } as FromServerMessage),\n );\n client.socket.send(UserNetworkingCodec.encodeUpdate(otherClient.update));\n\n otherClient.socket.send(userProfileMessage);\n otherClient.socket.send(userUpdateMessage);\n }\n\n console.log(\"Client authenticated\", client.id);\n\n return true;\n }\n\n public updateUserCharacter(clientId: number, userData: UserData) {\n this.internalUpdateUser(clientId, userData);\n }\n\n private internalUpdateUser(clientId: number, userData: UserData) {\n // This function assumes authorization has already been done\n const client = this.authenticatedClientsById.get(clientId)!;\n\n client.authenticatedUser = userData;\n this.authenticatedClientsById.set(clientId, client);\n\n const newUserData = JSON.stringify({\n id: clientId,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: userData.username,\n characterDescription: userData.characterDescription,\n } as FromServerMessage);\n\n // Broadcast the new userdata to all sockets, INCLUDING the user of the calling socket\n // Clients will always render based on the public userProfile.\n // This makes it intuitive, as it is \"what you see is what other's see\" from a user's perspective.\n for (const [otherClientId, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(newUserData);\n }\n }\n }\n\n private async handleUserUpdate(clientId: number, message: UserUpdateMessage): Promise<void> {\n const client = this.authenticatedClientsById.get(clientId);\n if (!client) {\n console.error(`Client-id ${clientId} user_update ignored, client not found`);\n return;\n }\n\n // Verify using the user authenticator what the allowed version of this update is\n const authorizedUserData = this.options.onClientUserIdentityUpdate(\n clientId,\n message.userIdentity,\n );\n let resolvedAuthorizedUserData;\n if (authorizedUserData instanceof Promise) {\n resolvedAuthorizedUserData = await authorizedUserData;\n } else {\n resolvedAuthorizedUserData = authorizedUserData;\n }\n if (!resolvedAuthorizedUserData) {\n // TODO - inform the client about the unauthorized update\n console.warn(`Client-id ${clientId} user_update unauthorized and ignored`);\n return;\n }\n\n this.internalUpdateUser(clientId, resolvedAuthorizedUserData);\n }\n\n private sendUpdates(): void {\n for (const [clientId, client] of this.authenticatedClientsById) {\n const update = client.update;\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n\n for (const [otherClientId, otherClient] of this.authenticatedClientsById) {\n if (otherClientId !== clientId && otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(encodedUpdate);\n }\n }\n }\n }\n}\n", "import { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\n\nexport type WebsocketFactory = (url: string) => WebSocket;\n\nexport enum WebsocketStatus {\n Connecting,\n Connected,\n Reconnecting,\n Disconnected,\n}\n\nconst startingBackoffTimeMilliseconds = 100;\nconst maximumBackoffTimeMilliseconds = 10000;\nconst maximumWebsocketConnectionTimeout = 5000;\n\nexport abstract class ReconnectingWebSocket {\n private websocket: WebSocket | null = null;\n private status: WebsocketStatus | null = null;\n private receivedMessageSinceOpen = false;\n private backoffTime = startingBackoffTimeMilliseconds;\n private stopped = false;\n\n constructor(\n private url: string,\n private websocketFactory: WebsocketFactory,\n private statusUpdateCallback: (status: WebsocketStatus) => void,\n ) {\n this.setStatus(WebsocketStatus.Connecting);\n this.startWebSocketConnectionAttempt();\n }\n\n private setStatus(status: WebsocketStatus) {\n if (this.status !== status) {\n this.status = status;\n this.statusUpdateCallback(status);\n }\n }\n\n public sendUpdate(update: UserNetworkingClientUpdate): void {\n if (!this.websocket) {\n console.error(\"Not connected to the server\");\n return;\n }\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n this.send(encodedUpdate);\n }\n\n private async startWebSocketConnectionAttempt() {\n if (this.stopped) {\n return;\n }\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (this.stopped) {\n return;\n }\n try {\n await this.createWebsocketWithTimeout(maximumWebsocketConnectionTimeout);\n break;\n } catch (e) {\n // Connection failed, retry with backoff\n this.setStatus(WebsocketStatus.Reconnecting);\n await this.waitBackoffTime();\n }\n }\n }\n\n private async waitBackoffTime(): Promise<void> {\n console.warn(`Websocket connection to '${this.url}' failed: retrying in ${this.backoffTime}ms`);\n await new Promise((resolve) => setTimeout(resolve, this.backoffTime));\n this.backoffTime = Math.min(\n // Introduce a small amount of randomness to prevent clients from retrying in lockstep\n this.backoffTime * (1.5 + Math.random() * 0.5),\n maximumBackoffTimeMilliseconds,\n );\n }\n\n protected abstract handleIncomingWebsocketMessage(message: MessageEvent): void;\n\n protected send(message: object | Uint8Array): void {\n if (!this.websocket) {\n console.error(\"Not connected to the server\");\n return;\n }\n if (message instanceof Uint8Array) {\n this.websocket.send(message);\n } else {\n this.websocket.send(JSON.stringify(message));\n }\n }\n\n private createWebsocketWithTimeout(timeout: number): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n reject(new Error(\"websocket connection timed out\"));\n }, timeout);\n const websocket = this.websocketFactory(this.url);\n websocket.binaryType = \"arraybuffer\";\n websocket.addEventListener(\"open\", () => {\n clearTimeout(timeoutId);\n this.receivedMessageSinceOpen = false;\n this.websocket = websocket;\n this.setStatus(WebsocketStatus.Connected);\n\n websocket.addEventListener(\"message\", (event) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket message event because it is no longer current\");\n websocket.close();\n return;\n }\n if (!this.receivedMessageSinceOpen) {\n this.receivedMessageSinceOpen = true;\n }\n this.handleIncomingWebsocketMessage(event);\n });\n\n const onWebsocketClose = async () => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n this.websocket = null;\n if (this.stopped) {\n // This closing is expected. The client closed the websocket.\n this.setStatus(WebsocketStatus.Disconnected);\n return;\n }\n if (!this.receivedMessageSinceOpen) {\n // The websocket did not deliver any contents. It may have been successfully opened, but immediately closed. This client should back off to prevent this happening in a rapid loop.\n await this.waitBackoffTime();\n }\n // The websocket closed unexpectedly. Try to reconnect.\n this.setStatus(WebsocketStatus.Reconnecting);\n this.startWebSocketConnectionAttempt();\n };\n\n websocket.addEventListener(\"close\", (e) => {\n if (websocket !== this.websocket) {\n console.warn(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n console.log(\"NetworkedDOMWebsocket close\", e);\n onWebsocketClose();\n });\n websocket.addEventListener(\"error\", (e) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket error event because it is no longer current\");\n return;\n }\n console.error(\"NetworkedDOMWebsocket error\", e);\n onWebsocketClose();\n });\n\n resolve(websocket);\n });\n websocket.addEventListener(\"error\", (e) => {\n clearTimeout(timeoutId);\n reject(e);\n });\n });\n }\n\n public stop() {\n this.stopped = true;\n if (this.websocket !== null) {\n this.websocket.close();\n this.websocket = null;\n }\n }\n}\n", "import { ReconnectingWebSocket, WebsocketFactory, WebsocketStatus } from \"./ReconnectingWebSocket\";\nimport { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\nimport {\n CharacterDescription,\n DISCONNECTED_MESSAGE_TYPE,\n FromClientMessage,\n FromServerMessage,\n IDENTITY_MESSAGE_TYPE,\n PING_MESSAGE_TYPE,\n USER_AUTHENTICATE_MESSAGE_TYPE,\n USER_PROFILE_MESSAGE_TYPE,\n} from \"./UserNetworkingMessages\";\n\nexport type UserNetworkingClientConfig = {\n url: string;\n sessionToken: string;\n websocketFactory: WebsocketFactory;\n statusUpdateCallback: (status: WebsocketStatus) => void;\n assignedIdentity: (clientId: number) => void;\n clientUpdate: (id: number, update: null | UserNetworkingClientUpdate) => void;\n clientProfileUpdated: (\n id: number,\n username: string,\n characterDescription: CharacterDescription,\n ) => void;\n};\n\nexport class UserNetworkingClient extends ReconnectingWebSocket {\n constructor(private config: UserNetworkingClientConfig) {\n super(config.url, config.websocketFactory, (status: WebsocketStatus) => {\n if (status === WebsocketStatus.Connected) {\n this.sendMessage({\n type: USER_AUTHENTICATE_MESSAGE_TYPE,\n sessionToken: config.sessionToken,\n });\n }\n config.statusUpdateCallback(status);\n });\n }\n\n public sendUpdate(update: UserNetworkingClientUpdate): void {\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n this.send(encodedUpdate);\n }\n\n public sendMessage(message: FromClientMessage): void {\n this.send(message);\n }\n\n protected handleIncomingWebsocketMessage(message: MessageEvent) {\n if (typeof message.data === \"string\") {\n const parsed = JSON.parse(message.data) as FromServerMessage;\n switch (parsed.type) {\n case DISCONNECTED_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} left`);\n this.config.clientUpdate(parsed.id, null);\n break;\n case IDENTITY_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} assigned to self`);\n this.config.assignedIdentity(parsed.id);\n break;\n case USER_PROFILE_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} updated profile`);\n this.config.clientProfileUpdated(parsed.id, parsed.username, parsed.characterDescription);\n break;\n case PING_MESSAGE_TYPE: {\n this.sendMessage({ type: \"pong\" } as FromClientMessage);\n break;\n }\n default:\n console.error(\"Unhandled message\", parsed);\n }\n } else if (message.data instanceof ArrayBuffer) {\n const userNetworkingClientUpdate = UserNetworkingCodec.decodeUpdate(message.data);\n this.config.clientUpdate(userNetworkingClientUpdate.id, userNetworkingClientUpdate);\n } else {\n console.error(\"Unhandled message type\", message.data);\n }\n }\n}\n"],
|
5
|
-
"mappings": ";AAOO,IAAM,sBAAN,MAA0B;AAAA,EAC/B,OAAO,aAAa,QAAgD;AAClE,UAAM,SAAS,IAAI,YAAY,EAAE;AACjC,UAAM,WAAW,IAAI,SAAS,MAAM;AACpC,aAAS,UAAU,GAAG,OAAO,EAAE;AAC/B,aAAS,WAAW,GAAG,OAAO,SAAS,CAAC;AACxC,aAAS,WAAW,GAAG,OAAO,SAAS,CAAC;AACxC,aAAS,WAAW,IAAI,OAAO,SAAS,CAAC;AACzC,aAAS,SAAS,IAAI,OAAO,SAAS,cAAc,KAAK;AACzD,aAAS,SAAS,IAAI,OAAO,SAAS,cAAc,KAAK;AACzD,aAAS,SAAS,IAAI,OAAO,KAAK;AAClC,WAAO,IAAI,WAAW,MAAM;AAAA,EAC9B;AAAA,EAEA,OAAO,aAAa,QAAiD;AACnE,UAAM,WAAW,IAAI,SAAS,MAAM;AACpC,UAAM,KAAK,SAAS,UAAU,CAAC;AAC/B,UAAM,IAAI,SAAS,WAAW,CAAC;AAC/B,UAAM,IAAI,SAAS,WAAW,CAAC;AAC/B,UAAM,IAAI,SAAS,WAAW,EAAE;AAChC,UAAM,cAAc,SAAS,SAAS,EAAE,IAAI;AAC5C,UAAM,cAAc,SAAS,SAAS,EAAE,IAAI;AAC5C,UAAM,QAAQ,SAAS,SAAS,EAAE;AAClC,UAAM,WAAW,EAAE,GAAG,GAAG,EAAE;AAC3B,UAAM,WAAW,EAAE,aAAa,YAAY;AAC5C,WAAO,EAAE,IAAI,UAAU,UAAU,MAAM;AAAA,EACzC;AACF;;;AClCO,IAAM,eAAuB;AAC7B,IAAM,gBAAwB;AAC9B,IAAM,oBAA6B,IAAI,KAAM;;;ACF7C,IAAM,4BAA4B;AAClC,IAAM,wBAAwB;AAC9B,IAAM,iCAAiC;AACvC,IAAM,4BAA4B;AAClC,IAAM,2BAA2B;AACjC,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;;;ACqBjC,IAAM,sBAAsB;AAerB,IAAM,uBAAN,MAA2B;AAAA,EAIhC,YAAoB,SAAsC;AAAtC;AAHpB,SAAQ,
|
4
|
+
"sourcesContent": ["export type UserNetworkingClientUpdate = {\n id: number;\n position: { x: number; y: number; z: number };\n rotation: { quaternionY: number; quaternionW: number };\n state: number;\n};\n\nexport class UserNetworkingCodec {\n static encodeUpdate(update: UserNetworkingClientUpdate): Uint8Array {\n const buffer = new ArrayBuffer(19);\n const dataView = new DataView(buffer);\n dataView.setUint16(0, update.id); // id\n dataView.setFloat32(2, update.position.x); // position.x\n dataView.setFloat32(6, update.position.y); // position.y\n dataView.setFloat32(10, update.position.z); // position.z\n dataView.setInt16(14, update.rotation.quaternionY * 32767); // quaternion.y\n dataView.setInt16(16, update.rotation.quaternionW * 32767); // quaternion.w\n dataView.setUint8(18, update.state); // animationState\n return new Uint8Array(buffer);\n }\n\n static decodeUpdate(buffer: ArrayBuffer): UserNetworkingClientUpdate {\n const dataView = new DataView(buffer);\n const id = dataView.getUint16(0); // id\n const x = dataView.getFloat32(2); // position.x\n const y = dataView.getFloat32(6); // position.y\n const z = dataView.getFloat32(10); // position.z\n const quaternionY = dataView.getInt16(14) / 32767; // quaternion.y\n const quaternionW = dataView.getInt16(16) / 32767; // quaternion.w\n const state = dataView.getUint8(18); // animationState\n const position = { x, y, z };\n const rotation = { quaternionY, quaternionW };\n return { id, position, rotation, state };\n }\n}\n", "export const pingPongRate: number = 1500;\nexport const heartBeatRate: number = 15000;\nexport const packetsUpdateRate: number = (1 / 30) * 1000;\n", "export const DISCONNECTED_MESSAGE_TYPE = \"disconnected\";\nexport const IDENTITY_MESSAGE_TYPE = \"identity\";\nexport const USER_AUTHENTICATE_MESSAGE_TYPE = \"user_auth\";\nexport const USER_PROFILE_MESSAGE_TYPE = \"user_profile\";\nexport const USER_UPDATE_MESSAGE_TYPE = \"user_update\";\nexport const PING_MESSAGE_TYPE = \"ping\";\nexport const PONG_MESSAGE_TYPE = \"pong\";\n\nexport type IdentityMessage = {\n type: typeof IDENTITY_MESSAGE_TYPE;\n id: number;\n};\n\nexport type CharacterDescription = {\n meshFileUrl?: string;\n mmlCharacterUrl?: string;\n mmlCharacterString?: string;\n} & (\n | {\n meshFileUrl: string;\n }\n | {\n mmlCharacterUrl: string;\n }\n | {\n mmlCharacterString: string;\n }\n);\n\nexport type UserProfileMessage = {\n type: typeof USER_PROFILE_MESSAGE_TYPE;\n id: number;\n username: string;\n characterDescription: CharacterDescription;\n};\n\nexport type DisconnectedMessage = {\n type: typeof DISCONNECTED_MESSAGE_TYPE;\n id: number;\n};\n\nexport type FromServerPingMessage = {\n type: typeof PING_MESSAGE_TYPE;\n};\n\nexport type FromServerMessage =\n | IdentityMessage\n | UserProfileMessage\n | DisconnectedMessage\n | FromServerPingMessage;\n\nexport type FromClientPongMessage = {\n type: typeof PONG_MESSAGE_TYPE;\n};\n\nexport type UserIdentity = {\n characterDescription: CharacterDescription | null;\n username: string | null;\n};\n\nexport type UserAuthenticateMessage = {\n type: typeof USER_AUTHENTICATE_MESSAGE_TYPE;\n sessionToken: string;\n // The client can send a UserIdentity to use as the initial user profile and the server can choose to accept it or not\n userIdentity?: UserIdentity;\n};\n\nexport type UserUpdateMessage = {\n type: typeof USER_UPDATE_MESSAGE_TYPE;\n userIdentity: UserIdentity;\n};\n\nexport type FromClientMessage = FromClientPongMessage | UserAuthenticateMessage | UserUpdateMessage;\n", "import WebSocket from \"ws\";\n\nimport { heartBeatRate, packetsUpdateRate, pingPongRate } from \"./user-networking-settings\";\nimport { UserData } from \"./UserData\";\nimport { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\nimport {\n DISCONNECTED_MESSAGE_TYPE,\n FromClientMessage,\n FromServerMessage,\n IDENTITY_MESSAGE_TYPE,\n PONG_MESSAGE_TYPE,\n USER_AUTHENTICATE_MESSAGE_TYPE,\n USER_PROFILE_MESSAGE_TYPE,\n USER_UPDATE_MESSAGE_TYPE as USER_UPDATE_MESSAGE_TYPE,\n UserAuthenticateMessage,\n UserIdentity,\n UserUpdateMessage,\n} from \"./UserNetworkingMessages\";\n\nexport type Client = {\n socket: WebSocket;\n id: number;\n lastPong: number;\n update: UserNetworkingClientUpdate;\n authenticatedUser: UserData | null;\n};\n\nconst WebSocketOpenStatus = 1;\n\nexport type UserNetworkingServerOptions = {\n onClientConnect: (\n clientId: number,\n sessionToken: string,\n userIdentity?: UserIdentity,\n ) => Promise<UserData | null> | UserData | null;\n onClientUserIdentityUpdate: (\n clientId: number,\n userIdentity: UserIdentity,\n ) => Promise<UserData | null> | UserData | null;\n onClientDisconnect: (clientId: number) => void;\n};\n\nexport class UserNetworkingServer {\n private allClientsById = new Map<number, Client>();\n private authenticatedClientsById: Map<number, Client> = new Map();\n\n constructor(private options: UserNetworkingServerOptions) {\n setInterval(this.sendUpdates.bind(this), packetsUpdateRate);\n setInterval(this.pingClients.bind(this), pingPongRate);\n setInterval(this.heartBeat.bind(this), heartBeatRate);\n }\n\n private heartBeat() {\n const now = Date.now();\n this.allClientsById.forEach((client) => {\n if (now - client.lastPong > heartBeatRate) {\n client.socket.close();\n this.handleDisconnectedClient(client);\n }\n });\n }\n\n private pingClients() {\n this.authenticatedClientsById.forEach((client) => {\n if (client.socket.readyState === WebSocketOpenStatus) {\n client.socket.send(JSON.stringify({ type: \"ping\" } as FromServerMessage));\n }\n });\n }\n\n private getId(): number {\n let id = 1;\n while (this.allClientsById.has(id)) {\n id++;\n }\n return id;\n }\n\n public connectClient(socket: WebSocket) {\n const id = this.getId();\n console.log(`Client ID: ${id} joined, waiting for user-identification`);\n\n // Create a client but without user-information\n const client: Client = {\n id,\n lastPong: Date.now(),\n socket: socket as WebSocket,\n authenticatedUser: null,\n update: {\n id,\n position: { x: 0, y: 0, z: 0 },\n rotation: { quaternionY: 0, quaternionW: 1 },\n state: 0,\n },\n };\n this.allClientsById.set(id, client);\n\n socket.on(\"message\", (message: WebSocket.Data, _isBinary: boolean) => {\n if (message instanceof Buffer) {\n const arrayBuffer = new Uint8Array(message).buffer;\n const update = UserNetworkingCodec.decodeUpdate(arrayBuffer);\n update.id = id;\n client.update = update;\n } else {\n let parsed;\n try {\n parsed = JSON.parse(message as string) as FromClientMessage;\n } catch (e) {\n console.error(\"Error parsing JSON message\", message, e);\n return;\n }\n if (!client.authenticatedUser) {\n if (parsed.type === USER_AUTHENTICATE_MESSAGE_TYPE) {\n this.handleUserAuth(client, parsed).then((authResult) => {\n if (!authResult) {\n // If the user is not authorized, disconnect the client\n socket.close();\n } else {\n this.authenticatedClientsById.set(id, client);\n }\n });\n } else {\n console.error(`Unhandled message pre-auth: ${JSON.stringify(parsed)}`);\n socket.close();\n }\n } else {\n switch (parsed.type) {\n case PONG_MESSAGE_TYPE:\n client.lastPong = Date.now();\n break;\n\n case USER_UPDATE_MESSAGE_TYPE:\n this.handleUserUpdate(id, parsed as UserUpdateMessage);\n break;\n\n default:\n console.error(`Unhandled message: ${JSON.stringify(parsed)}`);\n }\n }\n }\n });\n\n socket.on(\"close\", () => {\n console.log(\"Client disconnected\", id);\n this.handleDisconnectedClient(client);\n });\n }\n\n private handleDisconnectedClient(client: Client) {\n if (!this.allClientsById.has(client.id)) {\n return;\n }\n this.allClientsById.delete(client.id);\n if (client.authenticatedUser !== null) {\n // Only report disconnections of clients that were authenticated\n this.options.onClientDisconnect(client.id);\n this.authenticatedClientsById.delete(client.id);\n const disconnectMessage = JSON.stringify({\n id: client.id,\n type: DISCONNECTED_MESSAGE_TYPE,\n } as FromServerMessage);\n for (const [, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(disconnectMessage);\n }\n }\n }\n }\n\n private async handleUserAuth(\n client: Client,\n credentials: UserAuthenticateMessage,\n ): Promise<boolean> {\n const userData = this.options.onClientConnect(\n client.id,\n credentials.sessionToken,\n credentials.userIdentity,\n );\n let resolvedUserData;\n if (userData instanceof Promise) {\n resolvedUserData = await userData;\n } else {\n resolvedUserData = userData;\n }\n if (resolvedUserData === null) {\n console.error(`Client-id ${client.id} user_auth unauthorized and ignored`);\n return false;\n }\n\n console.log(\"Client authenticated\", client.id, resolvedUserData);\n client.authenticatedUser = resolvedUserData;\n\n const identityMessage = JSON.stringify({\n id: client.id,\n type: IDENTITY_MESSAGE_TYPE,\n } as FromServerMessage);\n\n const userProfileMessage = JSON.stringify({\n id: client.id,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: resolvedUserData.username,\n characterDescription: resolvedUserData.characterDescription,\n } as FromServerMessage);\n\n client.socket.send(userProfileMessage);\n client.socket.send(identityMessage);\n\n const userUpdateMessage = UserNetworkingCodec.encodeUpdate(client.update);\n\n // Send information about all other clients to the freshly connected client and vice versa\n for (const [, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState !== WebSocketOpenStatus || otherClient === client) {\n // Do not send updates for any clients which have not yet authenticated or not yet connected\n continue;\n }\n // Send the character information\n client.socket.send(\n JSON.stringify({\n id: otherClient.update.id,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: otherClient.authenticatedUser?.username,\n characterDescription: otherClient.authenticatedUser?.characterDescription,\n } as FromServerMessage),\n );\n client.socket.send(UserNetworkingCodec.encodeUpdate(otherClient.update));\n\n otherClient.socket.send(userProfileMessage);\n otherClient.socket.send(userUpdateMessage);\n }\n\n console.log(\"Client authenticated\", client.id);\n\n return true;\n }\n\n public updateUserCharacter(clientId: number, userData: UserData) {\n this.internalUpdateUser(clientId, userData);\n }\n\n private internalUpdateUser(clientId: number, userData: UserData) {\n // This function assumes authorization has already been done\n const client = this.authenticatedClientsById.get(clientId)!;\n\n client.authenticatedUser = userData;\n this.authenticatedClientsById.set(clientId, client);\n\n const newUserData = JSON.stringify({\n id: clientId,\n type: USER_PROFILE_MESSAGE_TYPE,\n username: userData.username,\n characterDescription: userData.characterDescription,\n } as FromServerMessage);\n\n // Broadcast the new userdata to all sockets, INCLUDING the user of the calling socket\n // Clients will always render based on the public userProfile.\n // This makes it intuitive, as it is \"what you see is what other's see\" from a user's perspective.\n for (const [otherClientId, otherClient] of this.authenticatedClientsById) {\n if (otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(newUserData);\n }\n }\n }\n\n private async handleUserUpdate(clientId: number, message: UserUpdateMessage): Promise<void> {\n const client = this.authenticatedClientsById.get(clientId);\n if (!client) {\n console.error(`Client-id ${clientId} user_update ignored, client not found`);\n return;\n }\n\n // Verify using the user authenticator what the allowed version of this update is\n const authorizedUserData = this.options.onClientUserIdentityUpdate(\n clientId,\n message.userIdentity,\n );\n let resolvedAuthorizedUserData;\n if (authorizedUserData instanceof Promise) {\n resolvedAuthorizedUserData = await authorizedUserData;\n } else {\n resolvedAuthorizedUserData = authorizedUserData;\n }\n if (!resolvedAuthorizedUserData) {\n // TODO - inform the client about the unauthorized update\n console.warn(`Client-id ${clientId} user_update unauthorized and ignored`);\n return;\n }\n\n this.internalUpdateUser(clientId, resolvedAuthorizedUserData);\n }\n\n private sendUpdates(): void {\n for (const [clientId, client] of this.authenticatedClientsById) {\n const update = client.update;\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n\n for (const [otherClientId, otherClient] of this.authenticatedClientsById) {\n if (otherClientId !== clientId && otherClient.socket.readyState === WebSocketOpenStatus) {\n otherClient.socket.send(encodedUpdate);\n }\n }\n }\n }\n}\n", "import { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\n\nexport type WebsocketFactory = (url: string) => WebSocket;\n\nexport enum WebsocketStatus {\n Connecting,\n Connected,\n Reconnecting,\n Disconnected,\n}\n\nconst startingBackoffTimeMilliseconds = 100;\nconst maximumBackoffTimeMilliseconds = 10000;\nconst maximumWebsocketConnectionTimeout = 5000;\n\nexport abstract class ReconnectingWebSocket {\n private websocket: WebSocket | null = null;\n private status: WebsocketStatus | null = null;\n private receivedMessageSinceOpen = false;\n private backoffTime = startingBackoffTimeMilliseconds;\n private stopped = false;\n\n constructor(\n private url: string,\n private websocketFactory: WebsocketFactory,\n private statusUpdateCallback: (status: WebsocketStatus) => void,\n ) {\n this.setStatus(WebsocketStatus.Connecting);\n this.startWebSocketConnectionAttempt();\n }\n\n private setStatus(status: WebsocketStatus) {\n if (this.status !== status) {\n this.status = status;\n this.statusUpdateCallback(status);\n }\n }\n\n public sendUpdate(update: UserNetworkingClientUpdate): void {\n if (!this.websocket) {\n console.error(\"Not connected to the server\");\n return;\n }\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n this.send(encodedUpdate);\n }\n\n private async startWebSocketConnectionAttempt() {\n if (this.stopped) {\n return;\n }\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (this.stopped) {\n return;\n }\n try {\n await this.createWebsocketWithTimeout(maximumWebsocketConnectionTimeout);\n break;\n } catch (e) {\n // Connection failed, retry with backoff\n this.setStatus(WebsocketStatus.Reconnecting);\n await this.waitBackoffTime();\n }\n }\n }\n\n private async waitBackoffTime(): Promise<void> {\n console.warn(`Websocket connection to '${this.url}' failed: retrying in ${this.backoffTime}ms`);\n await new Promise((resolve) => setTimeout(resolve, this.backoffTime));\n this.backoffTime = Math.min(\n // Introduce a small amount of randomness to prevent clients from retrying in lockstep\n this.backoffTime * (1.5 + Math.random() * 0.5),\n maximumBackoffTimeMilliseconds,\n );\n }\n\n protected abstract handleIncomingWebsocketMessage(message: MessageEvent): void;\n\n protected send(message: object | Uint8Array): void {\n if (!this.websocket) {\n console.error(\"Not connected to the server\");\n return;\n }\n if (message instanceof Uint8Array) {\n this.websocket.send(message);\n } else {\n this.websocket.send(JSON.stringify(message));\n }\n }\n\n private createWebsocketWithTimeout(timeout: number): Promise<WebSocket> {\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n reject(new Error(\"websocket connection timed out\"));\n }, timeout);\n const websocket = this.websocketFactory(this.url);\n websocket.binaryType = \"arraybuffer\";\n websocket.addEventListener(\"open\", () => {\n clearTimeout(timeoutId);\n this.receivedMessageSinceOpen = false;\n this.websocket = websocket;\n this.setStatus(WebsocketStatus.Connected);\n\n websocket.addEventListener(\"message\", (event) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket message event because it is no longer current\");\n websocket.close();\n return;\n }\n if (!this.receivedMessageSinceOpen) {\n this.receivedMessageSinceOpen = true;\n }\n this.handleIncomingWebsocketMessage(event);\n });\n\n const onWebsocketClose = async () => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n this.websocket = null;\n if (this.stopped) {\n // This closing is expected. The client closed the websocket.\n this.setStatus(WebsocketStatus.Disconnected);\n return;\n }\n if (!this.receivedMessageSinceOpen) {\n // The websocket did not deliver any contents. It may have been successfully opened, but immediately closed. This client should back off to prevent this happening in a rapid loop.\n await this.waitBackoffTime();\n }\n // The websocket closed unexpectedly. Try to reconnect.\n this.setStatus(WebsocketStatus.Reconnecting);\n this.startWebSocketConnectionAttempt();\n };\n\n websocket.addEventListener(\"close\", (e) => {\n if (websocket !== this.websocket) {\n console.warn(\"Ignoring websocket close event because it is no longer current\");\n return;\n }\n console.log(\"NetworkedDOMWebsocket close\", e);\n onWebsocketClose();\n });\n websocket.addEventListener(\"error\", (e) => {\n if (websocket !== this.websocket) {\n console.log(\"Ignoring websocket error event because it is no longer current\");\n return;\n }\n console.error(\"NetworkedDOMWebsocket error\", e);\n onWebsocketClose();\n });\n\n resolve(websocket);\n });\n websocket.addEventListener(\"error\", (e) => {\n clearTimeout(timeoutId);\n reject(e);\n });\n });\n }\n\n public stop() {\n this.stopped = true;\n if (this.websocket !== null) {\n this.websocket.close();\n this.websocket = null;\n }\n }\n}\n", "import { ReconnectingWebSocket, WebsocketFactory, WebsocketStatus } from \"./ReconnectingWebSocket\";\nimport { UserNetworkingClientUpdate, UserNetworkingCodec } from \"./UserNetworkingCodec\";\nimport {\n CharacterDescription,\n DISCONNECTED_MESSAGE_TYPE,\n FromClientMessage,\n FromServerMessage,\n IDENTITY_MESSAGE_TYPE,\n PING_MESSAGE_TYPE,\n USER_AUTHENTICATE_MESSAGE_TYPE,\n USER_PROFILE_MESSAGE_TYPE,\n} from \"./UserNetworkingMessages\";\n\nexport type UserNetworkingClientConfig = {\n url: string;\n sessionToken: string;\n websocketFactory: WebsocketFactory;\n statusUpdateCallback: (status: WebsocketStatus) => void;\n assignedIdentity: (clientId: number) => void;\n clientUpdate: (id: number, update: null | UserNetworkingClientUpdate) => void;\n clientProfileUpdated: (\n id: number,\n username: string,\n characterDescription: CharacterDescription,\n ) => void;\n};\n\nexport class UserNetworkingClient extends ReconnectingWebSocket {\n constructor(private config: UserNetworkingClientConfig) {\n super(config.url, config.websocketFactory, (status: WebsocketStatus) => {\n if (status === WebsocketStatus.Connected) {\n this.sendMessage({\n type: USER_AUTHENTICATE_MESSAGE_TYPE,\n sessionToken: config.sessionToken,\n });\n }\n config.statusUpdateCallback(status);\n });\n }\n\n public sendUpdate(update: UserNetworkingClientUpdate): void {\n const encodedUpdate = UserNetworkingCodec.encodeUpdate(update);\n this.send(encodedUpdate);\n }\n\n public sendMessage(message: FromClientMessage): void {\n this.send(message);\n }\n\n protected handleIncomingWebsocketMessage(message: MessageEvent) {\n if (typeof message.data === \"string\") {\n const parsed = JSON.parse(message.data) as FromServerMessage;\n switch (parsed.type) {\n case DISCONNECTED_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} left`);\n this.config.clientUpdate(parsed.id, null);\n break;\n case IDENTITY_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} assigned to self`);\n this.config.assignedIdentity(parsed.id);\n break;\n case USER_PROFILE_MESSAGE_TYPE:\n console.log(`Client ID: ${parsed.id} updated profile`);\n this.config.clientProfileUpdated(parsed.id, parsed.username, parsed.characterDescription);\n break;\n case PING_MESSAGE_TYPE: {\n this.sendMessage({ type: \"pong\" } as FromClientMessage);\n break;\n }\n default:\n console.error(\"Unhandled message\", parsed);\n }\n } else if (message.data instanceof ArrayBuffer) {\n const userNetworkingClientUpdate = UserNetworkingCodec.decodeUpdate(message.data);\n this.config.clientUpdate(userNetworkingClientUpdate.id, userNetworkingClientUpdate);\n } else {\n console.error(\"Unhandled message type\", message.data);\n }\n }\n}\n"],
|
5
|
+
"mappings": ";AAOO,IAAM,sBAAN,MAA0B;AAAA,EAC/B,OAAO,aAAa,QAAgD;AAClE,UAAM,SAAS,IAAI,YAAY,EAAE;AACjC,UAAM,WAAW,IAAI,SAAS,MAAM;AACpC,aAAS,UAAU,GAAG,OAAO,EAAE;AAC/B,aAAS,WAAW,GAAG,OAAO,SAAS,CAAC;AACxC,aAAS,WAAW,GAAG,OAAO,SAAS,CAAC;AACxC,aAAS,WAAW,IAAI,OAAO,SAAS,CAAC;AACzC,aAAS,SAAS,IAAI,OAAO,SAAS,cAAc,KAAK;AACzD,aAAS,SAAS,IAAI,OAAO,SAAS,cAAc,KAAK;AACzD,aAAS,SAAS,IAAI,OAAO,KAAK;AAClC,WAAO,IAAI,WAAW,MAAM;AAAA,EAC9B;AAAA,EAEA,OAAO,aAAa,QAAiD;AACnE,UAAM,WAAW,IAAI,SAAS,MAAM;AACpC,U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AACH,kBAAQ,IAAI,cAAc,OAAO,EAAE,OAAO;AAC1C,eAAK,OAAO,aAAa,OAAO,IAAI,IAAI;AACxC;AAAA,QACF,KAAK;AACH,kBAAQ,IAAI,cAAc,OAAO,EAAE,mBAAmB;AACtD,eAAK,OAAO,iBAAiB,OAAO,EAAE;AACtC;AAAA,QACF,KAAK;AACH,kBAAQ,IAAI,cAAc,OAAO,EAAE,kBAAkB;AACrD,eAAK,OAAO,qBAAqB,OAAO,IAAI,OAAO,UAAU,OAAO,oBAAoB;AACxF;AAAA,QACF,KAAK,mBAAmB;AACtB,eAAK,YAAY,EAAE,MAAM,OAAO,CAAsB;AACtD;AAAA,QACF;AAAA,QACA;AACE,kBAAQ,MAAM,qBAAqB,MAAM;AAAA,MAC7C;AAAA,IACF,WAAW,QAAQ,gBAAgB,aAAa;AAC9C,YAAM,6BAA6B,oBAAoB,aAAa,QAAQ,IAAI;AAChF,WAAK,OAAO,aAAa,2BAA2B,IAAI,0BAA0B;AAAA,IACpF,OAAO;AACL,cAAQ,MAAM,0BAA0B,QAAQ,IAAI;AAAA,IACtD;AAAA,EACF;AACF;",
|
6
6
|
"names": ["WebsocketStatus"]
|
7
7
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@mml-io/3d-web-user-networking",
|
3
|
-
"version": "0.0.0-experimental-
|
3
|
+
"version": "0.0.0-experimental-b569a23-20240430",
|
4
4
|
"publishConfig": {
|
5
5
|
"access": "public"
|
6
6
|
},
|
@@ -29,5 +29,5 @@
|
|
29
29
|
"express": "4.19.2",
|
30
30
|
"express-ws": "5.0.2"
|
31
31
|
},
|
32
|
-
"gitHead": "
|
32
|
+
"gitHead": "d2e8da35cfca7dd158d5d9354aa65f15dd95a37b"
|
33
33
|
}
|