Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 013efdb

Browse files
Update protos (#75)
1 parent b357168 commit 013efdb

File tree

5 files changed

+895
-11
lines changed

5 files changed

+895
-11
lines changed

protos

src/components/ServerEvents.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
22
import { JsonComponent } from "./JsonComponent";
33
import { useServerSdk } from "./ServerSdkContext";
44
import { ServerMessage } from "../protos/jellyfish/server_notifications";
5+
56
export const ServerEvents = ({ displayed }: { displayed: boolean }) => {
67
const [serverMessages, setServerMessages] = useState<ServerMessage[]>([]);
78
const { serverWebsocket } = useServerSdk();
@@ -21,6 +22,10 @@ export const ServerEvents = ({ displayed }: { displayed: boolean }) => {
2122

2223
useEffect(() => {
2324
serverWebsocket?.addEventListener("message", handler);
25+
26+
return () => {
27+
serverWebsocket?.removeEventListener("message", handler);
28+
};
2429
}, [serverWebsocket]);
2530

2631
return (

src/components/ServerSdkContext.tsx

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext, useMemo } from "react";
1+
import React, { useContext, useEffect, useMemo, useState } from "react";
22
import { HlsApi, RoomApi } from "../server-sdk";
33
import axios from "axios";
44
import { ServerMessage } from "../protos/jellyfish/server_notifications";
@@ -57,18 +57,32 @@ export const ServerSDKProvider = ({
5757
[serverToken],
5858
);
5959
const httpServerUrl = signalingProtocol + "://" + signalingHost + signalingPath.replace("peer", "server");
60-
const serverWebsocket = useMemo(() => (httpServerUrl ? createWS(httpServerUrl) : null), [httpServerUrl]);
61-
if (serverWebsocket) {
62-
serverWebsocket.binaryType = "arraybuffer";
60+
61+
const [serverWebsocket, setServerWebsocket] = useState<WebSocket | null>(null);
62+
63+
useEffect(() => {
64+
const websocket = createWS(httpServerUrl);
65+
66+
if (!websocket) return () => {};
67+
68+
websocket.binaryType = "arraybuffer";
6369
// create a new writer
6470
const auth = ServerMessage.encode({ authRequest: { token: serverToken } }).finish();
6571
const subscr = ServerMessage.encode({ subscribeRequest: { eventType: 1 } }).finish();
6672

67-
serverWebsocket?.addEventListener("open", () => {
68-
serverWebsocket.send(auth);
69-
serverWebsocket.send(subscr);
73+
websocket?.addEventListener("open", () => {
74+
websocket.send(auth);
75+
websocket.send(subscr);
7076
});
71-
}
77+
78+
setServerWebsocket(websocket);
79+
80+
return () => {
81+
websocket.close();
82+
setServerWebsocket(null);
83+
};
84+
}, [httpServerUrl, serverToken]);
85+
7286
const roomApi = useMemo(() => (httpApiUrl ? new RoomApi(undefined, httpApiUrl, client) : null), [client, httpApiUrl]);
7387
const hlsApi = useMemo(() => (httpApiUrl ? new HlsApi(undefined, httpApiUrl, client) : null), [client, httpApiUrl]);
7488

src/containers/Client.tsx

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useRef, useState } from "react";
1+
import { Fragment, useEffect, useRef, useState } from "react";
22
import { JsonComponent } from "../components/JsonComponent";
33
import { getArrayValue, getStringValue, useLocalStorageState } from "../components/LogSelector";
44
import { CloseButton } from "../components/CloseButton";
@@ -122,6 +122,25 @@ export const Client = ({
122122
(getArrayValue("current-encodings") as TrackEncoding[]) || ["h", "m", "l"],
123123
);
124124

125+
const connectionErrorTimeoutId = useRef<NodeJS.Timeout | null>(null);
126+
127+
useEffect(() => {
128+
if (!jellyfishClient) return;
129+
130+
const cb = () => {
131+
if (connectionErrorTimeoutId.current) {
132+
clearInterval(connectionErrorTimeoutId.current);
133+
}
134+
connectionErrorTimeoutId.current = null;
135+
};
136+
137+
jellyfishClient.on("joined", cb);
138+
139+
return () => {
140+
jellyfishClient?.removeListener("joined", cb);
141+
};
142+
}, [jellyfishClient]);
143+
125144
const changeEncodingReceived = (trackId: string, encoding: TrackEncoding) => {
126145
if (!fullState) return;
127146
api?.setTargetTrackEncoding(trackId, encoding);
@@ -328,7 +347,7 @@ export const Client = ({
328347
setTimeout(() => {
329348
refetchIfNeeded();
330349
}, 500);
331-
setTimeout(() => {
350+
connectionErrorTimeoutId.current = setTimeout(() => {
332351
if (statusRef.current === "joined") return;
333352
disconnect();
334353
showToastError("Unable to connect, try again");

0 commit comments

Comments
 (0)