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.umd.js
CHANGED
|
@@ -41278,6 +41278,18 @@ var Tevroc = (() => {
|
|
|
41278
41278
|
this.response = options.response;
|
|
41279
41279
|
}
|
|
41280
41280
|
};
|
|
41281
|
+
function buildRealtimeRoomName(value, appId = "") {
|
|
41282
|
+
if (!value) return "";
|
|
41283
|
+
const appKey = String(appId || "tevrocdev");
|
|
41284
|
+
const text2 = String(value);
|
|
41285
|
+
let encoded = "";
|
|
41286
|
+
for (let index2 = 0; index2 < text2.length; index2 += 1) {
|
|
41287
|
+
const charCode = text2.charCodeAt(index2);
|
|
41288
|
+
const keyCode = appKey.charCodeAt(index2 % appKey.length);
|
|
41289
|
+
encoded += (charCode ^ keyCode + index2 + 1).toString(16).padStart(2, "0");
|
|
41290
|
+
}
|
|
41291
|
+
return `chat:${encoded}`;
|
|
41292
|
+
}
|
|
41281
41293
|
function createClient(options = {}) {
|
|
41282
41294
|
return new TevrocClient(options);
|
|
41283
41295
|
}
|
|
@@ -41397,11 +41409,30 @@ var Tevroc = (() => {
|
|
|
41397
41409
|
}
|
|
41398
41410
|
function createRealtimeApi(client) {
|
|
41399
41411
|
const toWebSocketUrl = (baseUrl) => baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
41400
|
-
|
|
41401
|
-
|
|
41402
|
-
|
|
41403
|
-
|
|
41404
|
-
|
|
41412
|
+
const encryptRoomName = (room) => buildRealtimeRoomName(room, client.appId);
|
|
41413
|
+
const connect = (room, handlers = {}, options = {}) => {
|
|
41414
|
+
const roomName = encryptRoomName(room || options.room || "default");
|
|
41415
|
+
const url = `${toWebSocketUrl(client.baseUrl)}/realtime/${encodeURIComponent(roomName || "default")}`;
|
|
41416
|
+
const reconnectMs = options.reconnectMs ?? 3e3;
|
|
41417
|
+
const maxReconnects = options.maxReconnects ?? 5;
|
|
41418
|
+
let socket = null;
|
|
41419
|
+
let reconnectTimer = null;
|
|
41420
|
+
let reconnectCount = 0;
|
|
41421
|
+
let closedByUser = false;
|
|
41422
|
+
const cleanup = () => {
|
|
41423
|
+
if (reconnectTimer) {
|
|
41424
|
+
clearTimeout(reconnectTimer);
|
|
41425
|
+
reconnectTimer = null;
|
|
41426
|
+
}
|
|
41427
|
+
};
|
|
41428
|
+
const openSocket = () => {
|
|
41429
|
+
cleanup();
|
|
41430
|
+
if (closedByUser) return;
|
|
41431
|
+
socket = new WebSocket(url);
|
|
41432
|
+
socket.addEventListener("open", (event) => {
|
|
41433
|
+
reconnectCount = 0;
|
|
41434
|
+
handlers.open?.(event);
|
|
41435
|
+
});
|
|
41405
41436
|
socket.addEventListener("message", (event) => {
|
|
41406
41437
|
let data = event.data;
|
|
41407
41438
|
try {
|
|
@@ -41410,12 +41441,42 @@ var Tevroc = (() => {
|
|
|
41410
41441
|
}
|
|
41411
41442
|
handlers.message?.(data, event);
|
|
41412
41443
|
});
|
|
41413
|
-
socket.addEventListener("close", (event) =>
|
|
41414
|
-
|
|
41444
|
+
socket.addEventListener("close", (event) => {
|
|
41445
|
+
handlers.close?.(event);
|
|
41446
|
+
if (closedByUser || reconnectCount >= maxReconnects) return;
|
|
41447
|
+
reconnectTimer = setTimeout(() => {
|
|
41448
|
+
reconnectCount += 1;
|
|
41449
|
+
openSocket();
|
|
41450
|
+
}, reconnectMs);
|
|
41451
|
+
});
|
|
41452
|
+
socket.addEventListener("error", (event) => {
|
|
41453
|
+
handlers.error?.(event);
|
|
41454
|
+
});
|
|
41455
|
+
};
|
|
41456
|
+
openSocket();
|
|
41457
|
+
return {
|
|
41458
|
+
socket: () => socket,
|
|
41459
|
+
send: (payload) => socket?.readyState === WebSocket.OPEN && socket.send(typeof payload === "string" ? payload : JSON.stringify(payload)),
|
|
41460
|
+
close: () => {
|
|
41461
|
+
closedByUser = true;
|
|
41462
|
+
cleanup();
|
|
41463
|
+
socket?.close();
|
|
41464
|
+
},
|
|
41465
|
+
reconnect: () => {
|
|
41466
|
+
closedByUser = false;
|
|
41467
|
+
openSocket();
|
|
41468
|
+
}
|
|
41469
|
+
};
|
|
41470
|
+
};
|
|
41471
|
+
return {
|
|
41472
|
+
encryptRoomName,
|
|
41473
|
+
roomName: encryptRoomName,
|
|
41474
|
+
connect,
|
|
41475
|
+
room: (room, handlers = {}, options = {}) => {
|
|
41476
|
+
const connection = connect(room, handlers, options);
|
|
41415
41477
|
return {
|
|
41416
|
-
|
|
41417
|
-
|
|
41418
|
-
close: () => socket.close()
|
|
41478
|
+
...connection,
|
|
41479
|
+
publish: (payload) => connection.send(payload)
|
|
41419
41480
|
};
|
|
41420
41481
|
},
|
|
41421
41482
|
iceServers: (config = {}) => {
|