@mml-io/3d-web-user-networking
Advanced tools
Comparing version 0.17.0 to 0.18.0
@@ -41,4 +41,7 @@ // src/UserNetworkingCodec.ts | ||
var USER_UPDATE_MESSAGE_TYPE = "user_update"; | ||
var SERVER_ERROR_MESSAGE_TYPE = "error"; | ||
var PING_MESSAGE_TYPE = "ping"; | ||
var PONG_MESSAGE_TYPE = "pong"; | ||
var CONNECTION_LIMIT_REACHED_ERROR_TYPE = "CONNECTION_LIMIT_REACHED"; | ||
var AUTHENTICATION_FAILED_ERROR_TYPE = "AUTHENTICATION_FAILED"; | ||
@@ -112,5 +115,52 @@ // src/UserNetworkingServer.ts | ||
this.handleUserAuth(client, parsed).then((authResult) => { | ||
var _a, _b; | ||
if (!authResult) { | ||
const serverError = JSON.stringify({ | ||
type: SERVER_ERROR_MESSAGE_TYPE, | ||
errorType: AUTHENTICATION_FAILED_ERROR_TYPE, | ||
message: "Authentication failed" | ||
}); | ||
socket.send(serverError); | ||
socket.close(); | ||
} else { | ||
if (this.options.connectionLimit !== void 0 && this.authenticatedClientsById.size >= this.options.connectionLimit) { | ||
const serverError = JSON.stringify({ | ||
type: SERVER_ERROR_MESSAGE_TYPE, | ||
errorType: CONNECTION_LIMIT_REACHED_ERROR_TYPE, | ||
message: "Connection limit reached" | ||
}); | ||
socket.send(serverError); | ||
socket.close(); | ||
return; | ||
} | ||
const userData = authResult; | ||
const userProfileMessage = JSON.stringify({ | ||
id: client.id, | ||
type: USER_PROFILE_MESSAGE_TYPE, | ||
username: userData.username, | ||
characterDescription: userData.characterDescription | ||
}); | ||
client.socket.send(userProfileMessage); | ||
const identityMessage = JSON.stringify({ | ||
id: client.id, | ||
type: IDENTITY_MESSAGE_TYPE | ||
}); | ||
client.socket.send(identityMessage); | ||
const userUpdateMessage = UserNetworkingCodec.encodeUpdate(client.update); | ||
for (const [, otherClient] of this.authenticatedClientsById) { | ||
if (otherClient.socket.readyState !== WebSocketOpenStatus || otherClient === client) { | ||
continue; | ||
} | ||
client.socket.send( | ||
JSON.stringify({ | ||
id: otherClient.update.id, | ||
type: USER_PROFILE_MESSAGE_TYPE, | ||
username: (_a = otherClient.authenticatedUser) == null ? void 0 : _a.username, | ||
characterDescription: (_b = otherClient.authenticatedUser) == null ? void 0 : _b.characterDescription | ||
}) | ||
); | ||
client.socket.send(UserNetworkingCodec.encodeUpdate(otherClient.update)); | ||
otherClient.socket.send(userProfileMessage); | ||
otherClient.socket.send(userUpdateMessage); | ||
} | ||
this.authenticatedClientsById.set(id, client); | ||
@@ -162,3 +212,2 @@ } | ||
async handleUserAuth(client, credentials) { | ||
var _a, _b; | ||
const userData = this.options.onClientConnect( | ||
@@ -181,33 +230,3 @@ client.id, | ||
client.authenticatedUser = resolvedUserData; | ||
const identityMessage = JSON.stringify({ | ||
id: client.id, | ||
type: IDENTITY_MESSAGE_TYPE | ||
}); | ||
const userProfileMessage = JSON.stringify({ | ||
id: client.id, | ||
type: USER_PROFILE_MESSAGE_TYPE, | ||
username: resolvedUserData.username, | ||
characterDescription: resolvedUserData.characterDescription | ||
}); | ||
client.socket.send(userProfileMessage); | ||
client.socket.send(identityMessage); | ||
const userUpdateMessage = UserNetworkingCodec.encodeUpdate(client.update); | ||
for (const [, otherClient] of this.authenticatedClientsById) { | ||
if (otherClient.socket.readyState !== WebSocketOpenStatus || otherClient === client) { | ||
continue; | ||
} | ||
client.socket.send( | ||
JSON.stringify({ | ||
id: otherClient.update.id, | ||
type: USER_PROFILE_MESSAGE_TYPE, | ||
username: (_a = otherClient.authenticatedUser) == null ? void 0 : _a.username, | ||
characterDescription: (_b = otherClient.authenticatedUser) == null ? void 0 : _b.characterDescription | ||
}) | ||
); | ||
client.socket.send(UserNetworkingCodec.encodeUpdate(otherClient.update)); | ||
otherClient.socket.send(userProfileMessage); | ||
otherClient.socket.send(userUpdateMessage); | ||
} | ||
console.log("Client authenticated", client.id); | ||
return true; | ||
return resolvedUserData; | ||
} | ||
@@ -387,3 +406,3 @@ updateUserCharacter(clientId, userData) { | ||
} | ||
console.log("NetworkedDOMWebsocket close", e); | ||
console.log("ReconnectingWebSocket close", e); | ||
onWebsocketClose(); | ||
@@ -396,3 +415,3 @@ }); | ||
} | ||
console.error("NetworkedDOMWebsocket error", e); | ||
console.error("ReconnectingWebSocket error", e); | ||
onWebsocketClose(); | ||
@@ -442,2 +461,6 @@ }); | ||
switch (parsed.type) { | ||
case SERVER_ERROR_MESSAGE_TYPE: | ||
console.error(`Server error: ${parsed.message}. errorType: ${parsed.errorType}`); | ||
this.config.onServerError(parsed); | ||
break; | ||
case DISCONNECTED_MESSAGE_TYPE: | ||
@@ -471,2 +494,4 @@ console.log(`Client ID: ${parsed.id} left`); | ||
export { | ||
AUTHENTICATION_FAILED_ERROR_TYPE, | ||
CONNECTION_LIMIT_REACHED_ERROR_TYPE, | ||
DISCONNECTED_MESSAGE_TYPE, | ||
@@ -477,2 +502,3 @@ IDENTITY_MESSAGE_TYPE, | ||
ReconnectingWebSocket, | ||
SERVER_ERROR_MESSAGE_TYPE, | ||
USER_AUTHENTICATE_MESSAGE_TYPE, | ||
@@ -479,0 +505,0 @@ USER_PROFILE_MESSAGE_TYPE, |
import { ReconnectingWebSocket, WebsocketFactory, WebsocketStatus } from "./ReconnectingWebSocket"; | ||
import { UserNetworkingClientUpdate } from "./UserNetworkingCodec"; | ||
import { CharacterDescription, FromClientMessage } from "./UserNetworkingMessages"; | ||
import { CharacterDescription, FromClientMessage, ServerErrorType } from "./UserNetworkingMessages"; | ||
export type UserNetworkingClientConfig = { | ||
@@ -12,2 +12,6 @@ url: string; | ||
clientProfileUpdated: (id: number, username: string, characterDescription: CharacterDescription) => void; | ||
onServerError: (error: { | ||
message: string; | ||
errorType: ServerErrorType; | ||
}) => void; | ||
}; | ||
@@ -14,0 +18,0 @@ export declare class UserNetworkingClient extends ReconnectingWebSocket { |
@@ -6,2 +6,3 @@ export declare const DISCONNECTED_MESSAGE_TYPE = "disconnected"; | ||
export declare const USER_UPDATE_MESSAGE_TYPE = "user_update"; | ||
export declare const SERVER_ERROR_MESSAGE_TYPE = "error"; | ||
export declare const PING_MESSAGE_TYPE = "ping"; | ||
@@ -34,6 +35,14 @@ export declare const PONG_MESSAGE_TYPE = "pong"; | ||
}; | ||
export declare const CONNECTION_LIMIT_REACHED_ERROR_TYPE = "CONNECTION_LIMIT_REACHED"; | ||
export declare const AUTHENTICATION_FAILED_ERROR_TYPE = "AUTHENTICATION_FAILED"; | ||
export type ServerErrorType = typeof CONNECTION_LIMIT_REACHED_ERROR_TYPE | typeof AUTHENTICATION_FAILED_ERROR_TYPE; | ||
export type ServerError = { | ||
type: typeof SERVER_ERROR_MESSAGE_TYPE; | ||
errorType: ServerErrorType; | ||
message: string; | ||
}; | ||
export type FromServerPingMessage = { | ||
type: typeof PING_MESSAGE_TYPE; | ||
}; | ||
export type FromServerMessage = IdentityMessage | UserProfileMessage | DisconnectedMessage | FromServerPingMessage; | ||
export type FromServerMessage = IdentityMessage | UserProfileMessage | DisconnectedMessage | FromServerPingMessage | ServerError; | ||
export type FromClientPongMessage = { | ||
@@ -40,0 +49,0 @@ type: typeof PONG_MESSAGE_TYPE; |
@@ -13,2 +13,3 @@ import WebSocket from "ws"; | ||
export type UserNetworkingServerOptions = { | ||
connectionLimit?: number; | ||
onClientConnect: (clientId: number, sessionToken: string, userIdentity?: UserIdentity) => Promise<UserData | null> | UserData | null; | ||
@@ -15,0 +16,0 @@ onClientUserIdentityUpdate: (clientId: number, userIdentity: UserIdentity) => Promise<UserData | null> | UserData | null; |
{ | ||
"name": "@mml-io/3d-web-user-networking", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"publishConfig": { | ||
@@ -22,3 +22,3 @@ "access": "public" | ||
"dependencies": { | ||
"ws": "^8.16.0" | ||
"ws": "^8.18.0" | ||
}, | ||
@@ -28,3 +28,3 @@ "devDependencies": { | ||
"@types/express-ws": "^3.0.4", | ||
"@types/node": "^20.12.7", | ||
"@types/node": "^20.14.10", | ||
"@types/ws": "^8.5.10", | ||
@@ -34,3 +34,3 @@ "express": "4.19.2", | ||
}, | ||
"gitHead": "bc97b49ea69d920e0824a6885f7e8fca440e2e12" | ||
"gitHead": "8eb8acbdc767b15eacf3e8d62c5a0c92d2690f37" | ||
} |
Sorry, the diff of this file is not supported yet
61800
12
674
Updatedws@^8.18.0