@@ -47,9 +47,9 @@ interface UserMapping {
47
47
}
48
48
49
49
interface UseRoomActivityParams {
50
- provider_url : string ;
51
- getUserProfile : ( ) => Promise < { data : UserProfile } > ;
52
- getUserAccessToken : ( ) => Promise < { data : string } > ;
50
+ provider_url ? : string ;
51
+ getUserProfile ? : ( ) => Promise < { data : UserProfile } > ;
52
+ getUserAccessToken ? : ( ) => Promise < { data : string } > ;
53
53
}
54
54
55
55
const SUBSCRIBE_TO_ROOMS_ACTIVITY_MSG : SubscribeToRoomsActivityMessage = {
@@ -136,57 +136,73 @@ const subscribeToRoomActivity = async (
136
136
getUserProfile : ( ) => Promise < { data : UserProfile } > ,
137
137
getUserAccessToken : ( ) => Promise < { data : string } >
138
138
) : Promise < void > => {
139
- const config = await getCollaborationConfig ( {
140
- provider_url,
141
- getUserProfile,
142
- getUserAccessToken
143
- } ) ;
144
-
145
- // Create the websocket connection with proper headers
146
- const ws = new WebSocket ( config . signalingUrl [ 0 ] , [ 'auth' , config . authToken ] ) ;
147
- wsRef . current = ws ;
148
-
149
- ws . addEventListener ( 'open' , ( ) => {
150
- console . log ( '[RoomActivity] connected to room activity' ) ;
151
- ws . send ( JSON . stringify ( SUBSCRIBE_TO_ROOMS_ACTIVITY_MSG ) ) ;
152
- } ) ;
153
-
154
- ws . addEventListener ( 'message' , ( event : MessageEvent ) => {
155
- console . log ( '[RoomActivity] new message' , event ) ;
156
- const data = JSON . parse ( event . data ) as UserMapChangeMessage ;
157
- if ( data . type === USER_MAP_CHANGE_MSG && data . user_map ) {
158
- onUserMapChange ( data . user_map ) ;
159
- }
160
- } ) ;
139
+ if ( ! provider_url || ! getUserProfile || ! getUserAccessToken ) {
140
+ console . warn ( 'Missing required parameters for subscription' ) ;
141
+ return ;
142
+ }
143
+
144
+ try {
145
+ const config = await getCollaborationConfig ( {
146
+ provider_url,
147
+ getUserProfile,
148
+ getUserAccessToken
149
+ } ) ;
161
150
162
- ws . addEventListener ( 'close' , ( ) => {
163
- console . log ( '[RoomActivity] subscription to room activity closed' ) ;
164
- } ) ;
151
+ // Create the websocket connection with proper headers
152
+ const ws = new WebSocket ( config . signalingUrl [ 0 ] , [ 'auth' , config . authToken ] ) ;
153
+ wsRef . current = ws ;
154
+
155
+ ws . addEventListener ( 'open' , ( ) => {
156
+ console . log ( '[RoomActivity] connected to room activity' ) ;
157
+ if ( ws . readyState === WebSocket . OPEN ) {
158
+ ws . send ( JSON . stringify ( SUBSCRIBE_TO_ROOMS_ACTIVITY_MSG ) ) ;
159
+ }
160
+ } ) ;
165
161
166
- ws . addEventListener ( 'error' , ( err : Event ) => {
167
- console . error ( '[RoomActivity] error in room activity subscription' , err ) ;
168
- } ) ;
162
+ ws . addEventListener ( 'message' , ( event : MessageEvent ) => {
163
+ console . log ( '[RoomActivity] new message' , event ) ;
164
+ const data = JSON . parse ( event . data ) as UserMapChangeMessage ;
165
+ if ( data . type === USER_MAP_CHANGE_MSG && data . user_map ) {
166
+ onUserMapChange ( data . user_map ) ;
167
+ }
168
+ } ) ;
169
+
170
+ ws . addEventListener ( 'close' , ( ) => {
171
+ console . log ( '[RoomActivity] subscription to room activity closed' ) ;
172
+ } ) ;
173
+
174
+ ws . addEventListener ( 'error' , ( err : Event ) => {
175
+ console . error ( '[RoomActivity] error in room activity subscription' , err ) ;
176
+ } ) ;
177
+ } catch ( error ) {
178
+ console . error ( '[RoomActivity] Failed to subscribe to room activity:' , error ) ;
179
+ }
169
180
} ;
170
181
171
182
/**
172
183
* Hook to subscribe to and get room activity data
173
184
*/
174
- export const useRoomActivity = ( {
175
- provider_url,
176
- getUserProfile,
177
- getUserAccessToken
178
- } : UseRoomActivityParams ) : [ UserMapping , MutableRefObject < WebSocket | null > ] => {
185
+ export const useRoomActivity = (
186
+ {
187
+ provider_url,
188
+ getUserProfile,
189
+ getUserAccessToken
190
+ } : UseRoomActivityParams = { } as UseRoomActivityParams
191
+ ) : [ UserMapping , MutableRefObject < WebSocket | null > ] => {
179
192
const [ allRoomsUserMapping , setAllRoomsUserMapping ] = useState < UserMapping > ( { } ) ;
180
193
const wsRef = useRef < WebSocket | null > ( null ) ;
181
194
182
195
useEffect ( ( ) => {
183
- subscribeToRoomActivity (
184
- wsRef ,
185
- setAllRoomsUserMapping ,
186
- provider_url ,
187
- getUserProfile ,
188
- getUserAccessToken
189
- ) ;
196
+ // -> all parameters check
197
+ if ( provider_url && getUserProfile && getUserAccessToken ) {
198
+ subscribeToRoomActivity (
199
+ wsRef ,
200
+ setAllRoomsUserMapping ,
201
+ provider_url ,
202
+ getUserProfile ,
203
+ getUserAccessToken
204
+ ) ;
205
+ }
190
206
191
207
const ws = wsRef . current ;
192
208
@@ -202,12 +218,14 @@ export const useRoomActivity = ({
202
218
} ;
203
219
204
220
export const subscribeToRoom = ( ws : WebSocket , room : string ) => {
205
- ws . send (
206
- JSON . stringify ( {
207
- type : 'subscribe' ,
208
- topic : room
209
- } )
210
- ) ;
221
+ if ( ws . readyState === WebSocket . OPEN ) {
222
+ ws . send (
223
+ JSON . stringify ( {
224
+ type : 'subscribe' ,
225
+ topic : room
226
+ } )
227
+ ) ;
228
+ }
211
229
} ;
212
230
213
231
export const unSubscribeRoom = ( ws : WebSocket , room : string ) => {
0 commit comments