Skip to content

Commit 8e1ead7

Browse files
authored
Pass error message to onSocketError method (#481)
1 parent f706c03 commit 8e1ead7

File tree

8 files changed

+56
-73
lines changed

8 files changed

+56
-73
lines changed

internal/fishjam-chat/screens/RoomScreen/RoomScreen.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
leaveRoom,
2+
useConnection,
33
useAudioSettings,
44
useCamera,
55
useForegroundService,
@@ -46,10 +46,12 @@ const RoomScreen = ({ navigation, route }: Props) => {
4646

4747
const { toggleScreenShare, isScreenShareOn } = useScreenShare();
4848

49+
const { leaveRoom } = useConnection();
50+
4951
const onDisconnectPress = useCallback(() => {
5052
leaveRoom();
5153
navigation.navigate('Home');
52-
}, [navigation]);
54+
}, [navigation, leaveRoom]);
5355

5456
useForegroundService({
5557
channelId: 'io.fishjam.example.fishjamchat.foregroundservice.channel',

packages/ios-client/Sources/FishjamClient/FishjamClientInternal.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ class FishjamClientInternal {
452452

453453
func websocketDidReceiveMessage(text: String) {
454454
sdkLogger.error("Unsupported socket callback 'websocketDidReceiveMessage' was called.")
455-
onSocketError()
455+
onSocketError("Unsupported socket callback 'websocketDidReceiveMessage' was called.")
456456
}
457457

458458
func onSocketClose(code: UInt16, reason: String) {
@@ -466,9 +466,9 @@ class FishjamClientInternal {
466466
listener.onAuthError(reason: reason)
467467
}
468468

469-
func onSocketError() {
469+
func onSocketError(_ errorMessage: String? = nil) {
470470
roomState.isAuthenticated = false
471-
listener.onSocketError()
471+
listener.onSocketError(errorMessage)
472472
}
473473

474474
func onDisconnected() {
@@ -547,7 +547,7 @@ extension FishjamClientInternal: WebSocketDelegate {
547547
///viabilityChanged is called when there is no internet
548548
case .viabilityChanged(let isViable):
549549
if !isViable {
550-
onSocketError()
550+
onSocketError("No internet connection")
551551
commandsQueue.clear()
552552
prepareToReconnect()
553553
reconnectionManager?.onDisconnected()
@@ -556,8 +556,8 @@ extension FishjamClientInternal: WebSocketDelegate {
556556
case .cancelled:
557557
onDisconnected()
558558
break
559-
case .error(_):
560-
onSocketError()
559+
case .error(let error):
560+
onSocketError(error?.localizedDescription)
561561
commandsQueue.clear()
562562
prepareToReconnect()
563563
reconnectionManager?.onDisconnected()

packages/ios-client/Sources/FishjamClient/FishjamClientListener.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public protocol FishjamClientListener: ReconnectionManagerListener {
77
func onSocketClose(code: UInt16, reason: String)
88

99
/**
10-
* Emitted when occurs an error in the websocket connection
10+
* Emitted when an error occurs in the websocket connection
1111
*/
12-
func onSocketError()
12+
func onSocketError(_ errorMessage: String?)
1313

1414
/**
1515
* Emitted when authentication fails

packages/react-native-client/ios/RNFishjamClient.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,10 +866,8 @@ class RNFishjamClient: FishjamClientListener {
866866
peerStatus = .idle
867867
}
868868

869-
func onSocketError() {
870-
if let connectPromise = connectPromise {
871-
connectPromise.reject("E_MEMBRANE_CONNECT", "Failed to connect: socket error")
872-
}
869+
func onSocketError(_ errorMessage: String? = nil) {
870+
connectPromise?.reject("E_MEMBRANE_CONNECT", "Failed to connect: socket error\(errorMessage.map { " - \($0)" } ?? "")")
873871
connectPromise = nil
874872
peerStatus = .error
875873
}

packages/react-native-client/src/RNFishjamClientModule.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { PermissionResponse, requireNativeModule } from 'expo-modules-core';
22

33
import type { NativeModule } from 'expo-modules-core/types';
4-
import type { ConnectionConfig } from './common/client';
54
import type { RTCStats } from './debug/stats/types';
65
import type { OnAudioDeviceEvent } from './hooks/useAudioSettings';
76
import type {
87
Camera,
98
CameraConfigInternal,
109
CurrentCameraChangedType,
1110
} from './hooks/useCamera';
12-
import type { PeerStatus, ReconnectionStatus } from './hooks/useConnection';
11+
import type {
12+
PeerStatus,
13+
ReconnectionStatus,
14+
ConnectionConfig,
15+
} from './hooks/useConnection';
1316
import type { ForegroundServiceConfig } from './hooks/useForegroundService';
1417
import type { Peer } from './hooks/usePeers';
1518
import type { ScreenShareOptionsInternal } from './hooks/useScreenShare';

packages/react-native-client/src/common/client.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/react-native-client/src/hooks/useConnection.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { useCallback } from 'react';
2-
import {
3-
ConnectionConfig,
4-
joinRoom as joinRoomClient,
5-
leaveRoom as leaveRoomClient,
6-
} from '../common/client';
7-
82
import RNFishjamClientModule, {
93
ReceivableEvents,
104
} from '../RNFishjamClientModule';
@@ -79,6 +73,42 @@ export type JoinRoomConfig<
7973
}
8074
);
8175

76+
export type ConnectionConfig = {
77+
/**
78+
* Configuration for automatic reconnection
79+
* sdk uses a linear backoff algorithm, that is the formula
80+
* for the delay of the nth attempt is
81+
* n * delayMs + initialDelayMs
82+
*
83+
* Pass 0 for maxAttempts to disable automatic reconnection
84+
*/
85+
reconnectConfig?: {
86+
maxAttempts?: number;
87+
initialDelayMs?: number;
88+
delayMs?: number;
89+
};
90+
};
91+
92+
async function joinRoomClient<
93+
PeerMetadata extends GenericMetadata = GenericMetadata,
94+
>(
95+
url: string,
96+
peerToken: string,
97+
peerMetadata?: PeerMetadata,
98+
config?: ConnectionConfig,
99+
) {
100+
await RNFishjamClientModule.joinRoom(
101+
url,
102+
peerToken,
103+
peerMetadata ?? {},
104+
config ?? {},
105+
);
106+
}
107+
108+
async function leaveRoomClient() {
109+
await RNFishjamClientModule.leaveRoom();
110+
}
111+
82112
/**
83113
* Connect/leave room. And get connection status.
84114
* @group Hooks

packages/react-native-client/src/index.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@ export type {
99
Brand,
1010
RoomType,
1111
} from './types';
12-
export type { ConnectionConfig } from './common/client';
1312
// #endregion
1413

1514
// #region methods
1615
export { updatePeerMetadata } from './common/metadata';
17-
export {
18-
/** @deprecated */
19-
joinRoom,
20-
/** @deprecated */
21-
leaveRoom,
22-
} from './common/client';
23-
// #endregion
2416

2517
// #region components
2618
export type { VideoPreviewViewProps } from './components/VideoPreviewView';
@@ -78,6 +70,7 @@ export type {
7870
JoinRoomConfig,
7971
ReconnectionStatus,
8072
PeerStatus,
73+
ConnectionConfig,
8174
} from './hooks/useConnection';
8275
export type { AppScreenShareData } from './hooks/useAppScreenShare';
8376
export type { useLivestreamViewerResult } from './hooks/useLivestreamViewer';

0 commit comments

Comments
 (0)