Skip to content

Commit 81fd7dc

Browse files
authored
Merge pull request #131 from jwdeveloper/develop-1.10.6
Add session id to websocket connection to get authenticated WS as well as optional customizable type for disconnecting websocket client in various ways.
2 parents dd2f311 + 7e59099 commit 81fd7dc

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/live/LiveClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ public interface LiveClient {
5151

5252
/**
5353
* Disconnects the connection.
54+
* @param type
55+
* <p>0 - Normal - Initiates disconnection and returns
56+
* <p>1 - Disconnects blocking and returns after closure
57+
* <p>2 - Disconnects and kills connection to websocket
58+
* <p>Default {@link #disconnect()} is 0
5459
*/
55-
void disconnect();
60+
void disconnect(int type);
5661

62+
default void disconnect() {
63+
disconnect(0);
64+
}
5765

5866
/**
5967
* Use to manually invoke event

API/src/main/java/io/github/jwdeveloper/tiktok/websocket/LiveSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727

2828
public interface LiveSocketClient {
2929
void start(LiveConnectionData.Response webcastResponse, LiveClient tikTokLiveClient);
30-
void stop();
30+
void stop(int type);
3131
boolean isConnected();
3232
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ public void tryConnect() {
153153
tikTokEventHandler.publish(this, new TikTokRoomInfoEvent(roomInfo));
154154
}
155155

156-
public void disconnect() {
156+
public void disconnect(int type) {
157157
if (webSocketClient.isConnected())
158-
webSocketClient.stop();
158+
webSocketClient.stop(type);
159159
if (!roomInfo.hasConnectionState(ConnectionState.DISCONNECTED))
160160
setState(ConnectionState.DISCONNECTED);
161161
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveHttpClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ protected ActionResult<HttpResponse<byte[]>> getByteResponse(String room_id) {
199199
.withParam("client", "ttlive-java")
200200
.withParam("room_id", room_id);
201201

202+
if (clientSettings.getSessionId() != null) // Allows receiving of all comments and Subscribe Events
203+
builder.withParam("session_id", clientSettings.getSessionId());
202204
if (clientSettings.getApiKey() != null)
203205
builder.withParam("apiKey", clientSettings.getApiKey());
204206

Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.github.jwdeveloper.tiktok.exceptions.*;
2929
import io.github.jwdeveloper.tiktok.live.*;
3030
import org.java_websocket.client.WebSocketClient;
31+
import org.java_websocket.framing.CloseFrame;
3132

3233
import javax.net.ssl.*;
3334
import java.net.Proxy;
@@ -56,18 +57,18 @@ public TikTokWebSocketClient(
5657
@Override
5758
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
5859
if (isConnected())
59-
stop();
60+
stop(0);
6061

6162
messageHandler.handle(liveClient, connectionData.getWebcastResponse());
6263

6364
var headers = new HashMap<>(clientSettings.getHttpSettings().getHeaders());
6465
headers.put("Cookie", connectionData.getWebsocketCookies());
6566
webSocketClient = new TikTokWebSocketListener(connectionData.getWebsocketUrl(),
66-
headers,
67-
clientSettings.getHttpSettings().getTimeout().toMillisPart(),
68-
messageHandler,
69-
tikTokEventHandler,
70-
liveClient);
67+
headers,
68+
clientSettings.getHttpSettings().getTimeout().toMillisPart(),
69+
messageHandler,
70+
tikTokEventHandler,
71+
liveClient);
7172

7273
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
7374
if (proxyClientSettings.isEnabled() && proxyClientSettings.isAllowWebsocket())
@@ -128,9 +129,19 @@ public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData p
128129
}
129130
}
130131

131-
public void stop() {
132+
public void stop(int type) {
132133
if (isConnected()) {
133-
webSocketClient.close();
134+
switch (type) {
135+
case 1 -> {
136+
try {
137+
webSocketClient.closeBlocking();
138+
} catch (InterruptedException e) {
139+
throw new TikTokLiveException("Failed to stop the websocket");
140+
}
141+
}
142+
case 2 -> webSocketClient.closeConnection(CloseFrame.NORMAL, "");
143+
default -> webSocketClient.close();
144+
}
134145
heartbeatTask.stop();
135146
}
136147
webSocketClient = null;

Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketOfflineClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void start(LiveConnectionData.Response webcastResponse, LiveClient tikTok
4444
}
4545

4646
@Override
47-
public void stop() {
47+
public void stop(int type) {
4848
if (liveClient != null)
4949
handler.publish(liveClient, new TikTokDisconnectedEvent("Stopping"));
5050
}

0 commit comments

Comments
 (0)