tevrocsdk 0.1.42 → 0.1.44
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/package.json +1 -1
- package/tevroc-sdk.cjs.js +48602 -0
- package/tevroc-sdk.cjs.js.map +7 -0
- package/tevroc-sdk.esm.js +71 -10
- package/tevroc-sdk.esm.js.map +7 -0
- package/tevroc-sdk.min.js +20 -20
- package/tevroc-sdk.umd.js +71 -10
package/tevroc-sdk.esm.js
CHANGED
|
@@ -41268,6 +41268,18 @@ var TevrocError = class extends Error {
|
|
|
41268
41268
|
this.response = options.response;
|
|
41269
41269
|
}
|
|
41270
41270
|
};
|
|
41271
|
+
function buildRealtimeRoomName(value, appId = "") {
|
|
41272
|
+
if (!value) return "";
|
|
41273
|
+
const appKey = String(appId || "tevrocdev");
|
|
41274
|
+
const text2 = String(value);
|
|
41275
|
+
let encoded = "";
|
|
41276
|
+
for (let index2 = 0; index2 < text2.length; index2 += 1) {
|
|
41277
|
+
const charCode = text2.charCodeAt(index2);
|
|
41278
|
+
const keyCode = appKey.charCodeAt(index2 % appKey.length);
|
|
41279
|
+
encoded += (charCode ^ keyCode + index2 + 1).toString(16).padStart(2, "0");
|
|
41280
|
+
}
|
|
41281
|
+
return `chat:${encoded}`;
|
|
41282
|
+
}
|
|
41271
41283
|
function createClient(options = {}) {
|
|
41272
41284
|
return new TevrocClient(options);
|
|
41273
41285
|
}
|
|
@@ -41387,11 +41399,30 @@ function createShiplogicApi(client) {
|
|
|
41387
41399
|
}
|
|
41388
41400
|
function createRealtimeApi(client) {
|
|
41389
41401
|
const toWebSocketUrl = (baseUrl) => baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
41390
|
-
|
|
41391
|
-
|
|
41392
|
-
|
|
41393
|
-
|
|
41394
|
-
|
|
41402
|
+
const encryptRoomName = (room) => buildRealtimeRoomName(room, client.appId);
|
|
41403
|
+
const connect = (room, handlers = {}, options = {}) => {
|
|
41404
|
+
const roomName = encryptRoomName(room || options.room || "default");
|
|
41405
|
+
const url = `${toWebSocketUrl(client.baseUrl)}/realtime/${encodeURIComponent(roomName || "default")}`;
|
|
41406
|
+
const reconnectMs = options.reconnectMs ?? 3e3;
|
|
41407
|
+
const maxReconnects = options.maxReconnects ?? 5;
|
|
41408
|
+
let socket = null;
|
|
41409
|
+
let reconnectTimer = null;
|
|
41410
|
+
let reconnectCount = 0;
|
|
41411
|
+
let closedByUser = false;
|
|
41412
|
+
const cleanup = () => {
|
|
41413
|
+
if (reconnectTimer) {
|
|
41414
|
+
clearTimeout(reconnectTimer);
|
|
41415
|
+
reconnectTimer = null;
|
|
41416
|
+
}
|
|
41417
|
+
};
|
|
41418
|
+
const openSocket = () => {
|
|
41419
|
+
cleanup();
|
|
41420
|
+
if (closedByUser) return;
|
|
41421
|
+
socket = new WebSocket(url);
|
|
41422
|
+
socket.addEventListener("open", (event) => {
|
|
41423
|
+
reconnectCount = 0;
|
|
41424
|
+
handlers.open?.(event);
|
|
41425
|
+
});
|
|
41395
41426
|
socket.addEventListener("message", (event) => {
|
|
41396
41427
|
let data = event.data;
|
|
41397
41428
|
try {
|
|
@@ -41400,12 +41431,42 @@ function createRealtimeApi(client) {
|
|
|
41400
41431
|
}
|
|
41401
41432
|
handlers.message?.(data, event);
|
|
41402
41433
|
});
|
|
41403
|
-
socket.addEventListener("close", (event) =>
|
|
41404
|
-
|
|
41434
|
+
socket.addEventListener("close", (event) => {
|
|
41435
|
+
handlers.close?.(event);
|
|
41436
|
+
if (closedByUser || reconnectCount >= maxReconnects) return;
|
|
41437
|
+
reconnectTimer = setTimeout(() => {
|
|
41438
|
+
reconnectCount += 1;
|
|
41439
|
+
openSocket();
|
|
41440
|
+
}, reconnectMs);
|
|
41441
|
+
});
|
|
41442
|
+
socket.addEventListener("error", (event) => {
|
|
41443
|
+
handlers.error?.(event);
|
|
41444
|
+
});
|
|
41445
|
+
};
|
|
41446
|
+
openSocket();
|
|
41447
|
+
return {
|
|
41448
|
+
socket: () => socket,
|
|
41449
|
+
send: (payload) => socket?.readyState === WebSocket.OPEN && socket.send(typeof payload === "string" ? payload : JSON.stringify(payload)),
|
|
41450
|
+
close: () => {
|
|
41451
|
+
closedByUser = true;
|
|
41452
|
+
cleanup();
|
|
41453
|
+
socket?.close();
|
|
41454
|
+
},
|
|
41455
|
+
reconnect: () => {
|
|
41456
|
+
closedByUser = false;
|
|
41457
|
+
openSocket();
|
|
41458
|
+
}
|
|
41459
|
+
};
|
|
41460
|
+
};
|
|
41461
|
+
return {
|
|
41462
|
+
encryptRoomName,
|
|
41463
|
+
roomName: encryptRoomName,
|
|
41464
|
+
connect,
|
|
41465
|
+
room: (room, handlers = {}, options = {}) => {
|
|
41466
|
+
const connection = connect(room, handlers, options);
|
|
41405
41467
|
return {
|
|
41406
|
-
|
|
41407
|
-
|
|
41408
|
-
close: () => socket.close()
|
|
41468
|
+
...connection,
|
|
41469
|
+
publish: (payload) => connection.send(payload)
|
|
41409
41470
|
};
|
|
41410
41471
|
},
|
|
41411
41472
|
iceServers: (config = {}) => {
|