Skip to content

Commit

Permalink
Remove interval causing unexpected process waiting (#171)
Browse files Browse the repository at this point in the history
* Remove interval causing unexpected process waiting

There does not seem to be a reason to clear the uu field in tiktokUtils.js every 30 minutes, and the most you need to do is iterate it when a new webcast client is created. This is based on the observed implementation in the TikTokLive Python Library

* Modify logic to add/remove id from uuc utility

* Moved placement of tiktokUtils calls & simplified add/remove uid logic

* add expiration to uu entries

---------

Co-authored-by: zerodytrash <[email protected]>
  • Loading branch information
ItWasEnder and zerodytrash authored Feb 22, 2024
1 parent 2f03e41 commit af5d2ee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiktok-live-connector",
"version": "1.1.2",
"version": "1.1.3",
"description": "Node.js module to receive live stream chat events like comments and gifts from TikTok LIVE",
"main": "index.js",
"types": "./dist/index.d.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { EventEmitter } = require('node:events');

const TikTokHttpClient = require('./lib/tiktokHttpClient.js');
const WebcastWebsocket = require('./lib/webcastWebsocket.js');
const { getRoomIdFromMainPageHtml, validateAndNormalizeUniqueId } = require('./lib/tiktokUtils.js');
const { getRoomIdFromMainPageHtml, validateAndNormalizeUniqueId, addUniqueId, removeUniqueId } = require('./lib/tiktokUtils.js');
const { simplifyObject } = require('./lib/webcastDataConverter.js');
const { deserializeMessage, deserializeWebsocketMessage } = require('./lib/webcastProtobuf.js');

Expand Down Expand Up @@ -140,6 +140,9 @@ class WebcastPushConnection extends EventEmitter {

this.#isConnecting = true;

// add streamerId to uu
addUniqueId(this.#uniqueStreamerId);

try {
// roomId already specified?
if (roomId) {
Expand Down Expand Up @@ -192,6 +195,10 @@ class WebcastPushConnection extends EventEmitter {
return state;
} catch (err) {
this.#handleError(err, 'Error while connecting');

// remove streamerId from uu on connect fail
removeUniqueId(this.#uniqueStreamerId);

throw err;
} finally {
this.#isConnecting = false;
Expand All @@ -210,6 +217,9 @@ class WebcastPushConnection extends EventEmitter {
// Reset state
this.#setUnconnected();

// remove streamerId from uu
removeUniqueId(this.#uniqueStreamerId);

this.emit(ControlEvents.DISCONNECTED);
}
}
Expand Down
26 changes: 18 additions & 8 deletions src/lib/tiktokUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,33 @@ function validateAndNormalizeUniqueId(uniqueId) {
uniqueId = uniqueId.replace('@', '');
uniqueId = uniqueId.trim();

if (!uu.includes(uniqueId)) {
uu.push(uniqueId);
return uniqueId;
}

function addUniqueId(uniqueId) {
const existingEntry = uu.find((x) => x.uniqueId === uniqueId);
if (existingEntry) {
existingEntry.ts = new Date().getTime();
} else {
uu.push({
uniqueId,
ts: new Date().getTime(),
});
}
}

return uniqueId;
function removeUniqueId(uniqueId) {
uu = uu.filter((x) => x.uniqueId !== uniqueId);
}

function getUuc() {
return uu.length;
return uu.filter((x) => x.ts > new Date().getTime() - 10 * 60000).length;
}

setInterval(() => {
uu = [];
}, 1000 * 60 * 30);

module.exports = {
getRoomIdFromMainPageHtml,
validateAndNormalizeUniqueId,
getUuc,
addUniqueId,
removeUniqueId,
};

0 comments on commit af5d2ee

Please sign in to comment.