1
- import { useEffect , useRef , useState } from 'react' ;
1
+ import { MutableRefObject , useEffect , useRef , useState } from 'react' ;
2
2
import {
3
3
ICE_SERVERS ,
4
4
ICEServer ,
@@ -152,6 +152,7 @@ const subscribeToRoomActivity = async (
152
152
} ) ;
153
153
154
154
ws . addEventListener ( 'message' , ( event : MessageEvent ) => {
155
+ console . log ( '[RoomActivity] new message' , event ) ;
155
156
const data = JSON . parse ( event . data ) as UserMapChangeMessage ;
156
157
if ( data . type === USER_MAP_CHANGE_MSG && data . user_map ) {
157
158
onUserMapChange ( data . user_map ) ;
@@ -174,7 +175,7 @@ export const useRoomActivity = ({
174
175
provider_url,
175
176
getUserProfile,
176
177
getUserAccessToken
177
- } : UseRoomActivityParams ) : UserMapping => {
178
+ } : UseRoomActivityParams ) : [ UserMapping , MutableRefObject < WebSocket | null > ] => {
178
179
const [ allRoomsUserMapping , setAllRoomsUserMapping ] = useState < UserMapping > ( { } ) ;
179
180
const wsRef = useRef < WebSocket | null > ( null ) ;
180
181
@@ -191,10 +192,29 @@ export const useRoomActivity = ({
191
192
192
193
return ( ) => {
193
194
if ( ws ) {
195
+ console . log ( 'closing websocket' , ws ) ;
194
196
ws . close ( ) ;
195
197
}
196
198
} ;
197
199
} , [ provider_url , getUserProfile , getUserAccessToken ] ) ;
198
200
199
- return allRoomsUserMapping ;
201
+ return [ allRoomsUserMapping , wsRef ] ;
202
+ } ;
203
+
204
+ export const subscribeToRoom = ( ws : WebSocket , room : string ) => {
205
+ ws . send (
206
+ JSON . stringify ( {
207
+ type : 'subscribe' ,
208
+ topic : room
209
+ } )
210
+ ) ;
211
+ } ;
212
+
213
+ export const unSubscribeRoom = ( ws : WebSocket , room : string ) => {
214
+ ws . send (
215
+ JSON . stringify ( {
216
+ type : 'unsubscribe' ,
217
+ topic : room
218
+ } )
219
+ ) ;
200
220
} ;
0 commit comments