Add session id to websocket connection to get authenticated WS as well as optional customizable type for disconnecting websocket client in various ways.

This commit is contained in:
kohlerpop1
2025-05-21 18:43:14 -04:00
parent dd2f311539
commit 7e59099793
6 changed files with 34 additions and 13 deletions

View File

@@ -153,9 +153,9 @@ public class TikTokLiveClient implements LiveClient
tikTokEventHandler.publish(this, new TikTokRoomInfoEvent(roomInfo));
}
public void disconnect() {
public void disconnect(int type) {
if (webSocketClient.isConnected())
webSocketClient.stop();
webSocketClient.stop(type);
if (!roomInfo.hasConnectionState(ConnectionState.DISCONNECTED))
setState(ConnectionState.DISCONNECTED);
}

View File

@@ -199,6 +199,8 @@ public class TikTokLiveHttpClient implements LiveHttpClient
.withParam("client", "ttlive-java")
.withParam("room_id", room_id);
if (clientSettings.getSessionId() != null) // Allows receiving of all comments and Subscribe Events
builder.withParam("session_id", clientSettings.getSessionId());
if (clientSettings.getApiKey() != null)
builder.withParam("apiKey", clientSettings.getApiKey());

View File

@@ -28,6 +28,7 @@ import io.github.jwdeveloper.tiktok.data.settings.*;
import io.github.jwdeveloper.tiktok.exceptions.*;
import io.github.jwdeveloper.tiktok.live.*;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.CloseFrame;
import javax.net.ssl.*;
import java.net.Proxy;
@@ -56,18 +57,18 @@ public class TikTokWebSocketClient implements LiveSocketClient {
@Override
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
if (isConnected())
stop();
stop(0);
messageHandler.handle(liveClient, connectionData.getWebcastResponse());
var headers = new HashMap<>(clientSettings.getHttpSettings().getHeaders());
headers.put("Cookie", connectionData.getWebsocketCookies());
webSocketClient = new TikTokWebSocketListener(connectionData.getWebsocketUrl(),
headers,
clientSettings.getHttpSettings().getTimeout().toMillisPart(),
messageHandler,
tikTokEventHandler,
liveClient);
headers,
clientSettings.getHttpSettings().getTimeout().toMillisPart(),
messageHandler,
tikTokEventHandler,
liveClient);
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
if (proxyClientSettings.isEnabled() && proxyClientSettings.isAllowWebsocket())
@@ -128,9 +129,19 @@ public class TikTokWebSocketClient implements LiveSocketClient {
}
}
public void stop() {
public void stop(int type) {
if (isConnected()) {
webSocketClient.close();
switch (type) {
case 1 -> {
try {
webSocketClient.closeBlocking();
} catch (InterruptedException e) {
throw new TikTokLiveException("Failed to stop the websocket");
}
}
case 2 -> webSocketClient.closeConnection(CloseFrame.NORMAL, "");
default -> webSocketClient.close();
}
heartbeatTask.stop();
}
webSocketClient = null;

View File

@@ -44,7 +44,7 @@ public class TikTokWebSocketOfflineClient implements LiveSocketClient {
}
@Override
public void stop() {
public void stop(int type) {
if (liveClient != null)
handler.publish(liveClient, new TikTokDisconnectedEvent("Stopping"));
}