1- import { useEffect , useRef , useState } from 'react' ;
1+ import { MutableRefObject , useEffect , useRef , useState } from 'react' ;
22import {
33 ICE_SERVERS ,
44 ICEServer ,
@@ -152,6 +152,7 @@ const subscribeToRoomActivity = async (
152152 } ) ;
153153
154154 ws . addEventListener ( 'message' , ( event : MessageEvent ) => {
155+ console . log ( '[RoomActivity] new message' , event ) ;
155156 const data = JSON . parse ( event . data ) as UserMapChangeMessage ;
156157 if ( data . type === USER_MAP_CHANGE_MSG && data . user_map ) {
157158 onUserMapChange ( data . user_map ) ;
@@ -174,7 +175,7 @@ export const useRoomActivity = ({
174175 provider_url,
175176 getUserProfile,
176177 getUserAccessToken
177- } : UseRoomActivityParams ) : UserMapping => {
178+ } : UseRoomActivityParams ) : [ UserMapping , MutableRefObject < WebSocket | null > ] => {
178179 const [ allRoomsUserMapping , setAllRoomsUserMapping ] = useState < UserMapping > ( { } ) ;
179180 const wsRef = useRef < WebSocket | null > ( null ) ;
180181
@@ -191,10 +192,29 @@ export const useRoomActivity = ({
191192
192193 return ( ) => {
193194 if ( ws ) {
195+ console . log ( 'closing websocket' , ws ) ;
194196 ws . close ( ) ;
195197 }
196198 } ;
197199 } , [ provider_url , getUserProfile , getUserAccessToken ] ) ;
198200
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+ ) ;
200220} ;
0 commit comments