@tiktool/live 2.9.0 → 2.10.0
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/dist/index.d.mts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +72 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +72 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2119,6 +2119,7 @@ var TikTokLive = class _TikTokLive extends EventEmitter {
|
|
|
2119
2119
|
_presetRoomId;
|
|
2120
2120
|
_presetSessionId;
|
|
2121
2121
|
proxyUrl;
|
|
2122
|
+
mode;
|
|
2122
2123
|
constructor(options) {
|
|
2123
2124
|
super();
|
|
2124
2125
|
this.setMaxListeners(20);
|
|
@@ -2133,6 +2134,7 @@ var TikTokLive = class _TikTokLive extends EventEmitter {
|
|
|
2133
2134
|
this._presetRoomId = options.roomId || "";
|
|
2134
2135
|
this._presetSessionId = options.sessionId || "";
|
|
2135
2136
|
this.proxyUrl = options.proxy || "";
|
|
2137
|
+
this.mode = options.mode || "direct";
|
|
2136
2138
|
}
|
|
2137
2139
|
/**
|
|
2138
2140
|
* Build an HttpsProxyAgent when `proxy` option is set, else undefined.
|
|
@@ -2152,6 +2154,9 @@ var TikTokLive = class _TikTokLive extends EventEmitter {
|
|
|
2152
2154
|
async connect() {
|
|
2153
2155
|
if (this._destroyed) throw new Error("Client has been destroyed. Create a new instance.");
|
|
2154
2156
|
this.intentionalClose = false;
|
|
2157
|
+
if (this.mode === "relayed") {
|
|
2158
|
+
return this._connectRelayed();
|
|
2159
|
+
}
|
|
2155
2160
|
let ttwid = this._presetSessionId;
|
|
2156
2161
|
let roomId = this._presetRoomId;
|
|
2157
2162
|
let clusterRegion = "";
|
|
@@ -2607,6 +2612,73 @@ var TikTokLive = class _TikTokLive extends EventEmitter {
|
|
|
2607
2612
|
this.heartbeatTimer = null;
|
|
2608
2613
|
}
|
|
2609
2614
|
}
|
|
2615
|
+
/**
|
|
2616
|
+
* Relayed-mode connect. Opens a plain WebSocket to TikTools' managed relay
|
|
2617
|
+
* endpoint. Events arrive pre-decoded as `{event, data}` JSON envelopes.
|
|
2618
|
+
* The SDK re-emits them on the same event names as Direct mode so user
|
|
2619
|
+
* code is identical regardless of mode.
|
|
2620
|
+
*/
|
|
2621
|
+
async _connectRelayed() {
|
|
2622
|
+
const host = this.signServerUrl.replace(/^https:\/\//, "wss://").replace(/^http:\/\//, "ws://");
|
|
2623
|
+
const wsUrl = `${host}/?uniqueId=${encodeURIComponent(this.uniqueId)}&apiKey=${encodeURIComponent(this.apiKey)}`;
|
|
2624
|
+
if (this.debug) console.log(`[TikTokLive] Relayed mode: ${wsUrl.replace(this.apiKey, "***")}`);
|
|
2625
|
+
return new Promise((resolve, reject) => {
|
|
2626
|
+
this.ws = new WebSocket(wsUrl);
|
|
2627
|
+
let firstOpen = true;
|
|
2628
|
+
this.ws.on("open", () => {
|
|
2629
|
+
this._connected = true;
|
|
2630
|
+
this.reconnectAttempts = 0;
|
|
2631
|
+
if (firstOpen) {
|
|
2632
|
+
firstOpen = false;
|
|
2633
|
+
resolve();
|
|
2634
|
+
}
|
|
2635
|
+
this.emit("connected");
|
|
2636
|
+
});
|
|
2637
|
+
this.ws.on("message", (raw) => {
|
|
2638
|
+
try {
|
|
2639
|
+
const msg = JSON.parse(raw.toString());
|
|
2640
|
+
if (typeof msg !== "object" || !msg) return;
|
|
2641
|
+
const evName = msg.event;
|
|
2642
|
+
const evData = msg.data ?? msg;
|
|
2643
|
+
if (!evName) return;
|
|
2644
|
+
if (evName === "connected" || evName === "_journal" || evName === "ping" || evName === "pong") return;
|
|
2645
|
+
if (evName === "roomInfo") {
|
|
2646
|
+
const ri = {
|
|
2647
|
+
roomId: evData.roomId || "",
|
|
2648
|
+
wsHost: evData.wsHost || "",
|
|
2649
|
+
clusterRegion: evData.clusterRegion || "",
|
|
2650
|
+
connectedAt: evData.connectedAt || (/* @__PURE__ */ new Date()).toISOString()
|
|
2651
|
+
};
|
|
2652
|
+
this.emit("roomInfo", ri);
|
|
2653
|
+
return;
|
|
2654
|
+
}
|
|
2655
|
+
this.emit(evName, evData);
|
|
2656
|
+
this.emit("event", evData);
|
|
2657
|
+
} catch {
|
|
2658
|
+
}
|
|
2659
|
+
});
|
|
2660
|
+
this.ws.on("close", (code, reason) => {
|
|
2661
|
+
this._connected = false;
|
|
2662
|
+
const reasonStr = reason?.toString() || "";
|
|
2663
|
+
this.emit("disconnected", code, reasonStr);
|
|
2664
|
+
if (!this.intentionalClose && this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
2665
|
+
this.reconnectAttempts++;
|
|
2666
|
+
const delay = Math.min(1e3 * Math.pow(2, this.reconnectAttempts), 3e4);
|
|
2667
|
+
if (this.debug) console.log(`[TikTokLive] Relayed reconnect in ${delay}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
2668
|
+
setTimeout(() => {
|
|
2669
|
+
this._connectRelayed().catch(() => {
|
|
2670
|
+
});
|
|
2671
|
+
}, delay);
|
|
2672
|
+
}
|
|
2673
|
+
});
|
|
2674
|
+
this.ws.on("error", (err) => {
|
|
2675
|
+
if (firstOpen) {
|
|
2676
|
+
firstOpen = false;
|
|
2677
|
+
reject(err);
|
|
2678
|
+
} else this.emit("error", err);
|
|
2679
|
+
});
|
|
2680
|
+
});
|
|
2681
|
+
}
|
|
2610
2682
|
};
|
|
2611
2683
|
export {
|
|
2612
2684
|
TikTokLive
|