From 47d18e14783741e51dd9b646bc2ecf0122be12a9 Mon Sep 17 00:00:00 2001 From: zerodytrash <59258980+zerodytrash@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:17:08 +0100 Subject: [PATCH] fix websocket upgrade error Fix "TikTok does not offer a websocket upgrade" error due to missing "Sec-Websocket-Protocol" header in response. --- package.json | 2 +- src/index.js | 4 ++-- src/lib/webcastConfig.js | 5 +++-- src/lib/webcastWebsocket.js | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index aa4bb89..a9ee683 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiktok-live-connector", - "version": "1.1.3", + "version": "1.1.4", "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", diff --git a/src/index.js b/src/index.js index 46179d2..060ceb8 100644 --- a/src/index.js +++ b/src/index.js @@ -405,7 +405,7 @@ class WebcastPushConnection extends EventEmitter { async #fetchRoomData(isInitial) { let webcastResponse = await this.#httpClient.getDeserializedObjectFromWebcastApi('im/fetch/', this.#clientParams, 'WebcastResponse', isInitial); - let upgradeToWsOffered = !!webcastResponse.wsUrl && !!webcastResponse.wsParam; + let upgradeToWsOffered = !!webcastResponse.wsUrl; if (!webcastResponse.cursor) { if (isInitial) { @@ -438,7 +438,7 @@ class WebcastPushConnection extends EventEmitter { try { // Websocket specific params let wsParams = { - imprp: webcastResponse.wsParam.value, + imprp: webcastResponse.wsParam?.value || '', compress: 'gzip', }; diff --git a/src/lib/webcastConfig.js b/src/lib/webcastConfig.js index f9fc44d..0ab9f56 100644 --- a/src/lib/webcastConfig.js +++ b/src/lib/webcastConfig.js @@ -17,7 +17,7 @@ module.exports = { device_platform: 'web', focus_state: true, from_page: 'user', - history_len: 4, + history_len: 0, is_fullscreen: false, is_page_visible: true, did_rule: 3, @@ -30,7 +30,8 @@ module.exports = { tz_name: 'Europe/Berlin', referer: 'https://www.tiktok.com/', root_referer: 'https://www.tiktok.com/', - version_code: 180800, + host: 'https://webcast.tiktok.com', + version_code: 270000, webcast_sdk_version: '1.3.0', update_version_code: '1.3.0', }, diff --git a/src/lib/webcastWebsocket.js b/src/lib/webcastWebsocket.js index dca28a1..65a9329 100644 --- a/src/lib/webcastWebsocket.js +++ b/src/lib/webcastWebsocket.js @@ -15,7 +15,7 @@ class WebcastWebsocket extends websocket.client { }; this.#handleEvents(); - this.connect(this.wsUrlWithParams, 'echo-protocol', Config.TIKTOK_URL_WEBCAST, this.wsHeaders, websocketOptions); + this.connect(this.wsUrlWithParams, '', Config.TIKTOK_URL_WEB, this.wsHeaders, websocketOptions); } #handleEvents() {