11import * as io from 'socket.io-client' ;
22
3- import { BehaviorSubject , Observable , Subject } from 'rxjs ' ;
3+ import remoteStore from '../../stores/remote.store ' ;
44
55// Types
66import {
99 DeckdeckgoEventNextPrevSlide ,
1010 DeckdeckgoEventSlideAction ,
1111 DeckdeckgoEventSlideTo ,
12- ConnectionState
12+ ConnectionState ,
1313} from '@deckdeckgo/types' ;
1414
1515// Services
@@ -20,17 +20,11 @@ const configuration: RTCConfiguration = {
2020 {
2121 urls : 'turn:api.deckdeckgo.com:3478' ,
2222 username : 'user' ,
23- credential : 'deckdeckgo'
24- }
25- ]
23+ credential : 'deckdeckgo' ,
24+ } ,
25+ ] ,
2626} ;
2727
28- export interface ActiveRoom {
29- room : string ;
30- clients : number ;
31- connected : boolean ;
32- }
33-
3428// @ts -ignore
3529const PeerConnection = window . RTCPeerConnection || window . mozRTCPeerConnection || window . webkitRTCPeerConnection || window . msRTCPeerConnection ;
3630
@@ -51,10 +45,6 @@ export class CommunicationService {
5145 . toString ( )
5246 . replace ( / \B (? = ( \d { 2 } ) + (? ! \d ) ) / g, ' ' ) ;
5347
54- private state : BehaviorSubject < ConnectionState > = new BehaviorSubject < ConnectionState > ( ConnectionState . DISCONNECTED ) ;
55- private event : Subject < DeckdeckgoEvent > = new Subject < DeckdeckgoEvent > ( ) ;
56- private rooms : BehaviorSubject < ActiveRoom [ ] > = new BehaviorSubject < ActiveRoom [ ] > ( [ ] ) ;
57-
5848 private constructor ( ) {
5949 // Private constructor, singleton
6050 }
@@ -73,30 +63,30 @@ export class CommunicationService {
7363 return ;
7464 }
7565
76- this . state . next ( ConnectionState . CONNECTING ) ;
66+ remoteStore . state . state = ConnectionState . CONNECTING ;
7767
7868 const url : string = EnvironmentConfigService . getInstance ( ) . get ( 'signalingServerUrl' ) ;
7969
8070 this . socket = io . connect ( url , {
8171 reconnectionAttempts : 5 ,
8272 transports : [ 'websocket' , 'xhr-polling' ] ,
83- query : 'type=app'
73+ query : 'type=app' ,
8474 } ) ;
8575
8676 this . socket . on ( 'connect' , async ( ) => {
8777 this . socket . emit ( 'rooms' ) ;
8878 } ) ;
8979
9080 this . socket . on ( 'active_rooms' , async ( data ) => {
91- this . rooms . next ( data . rooms ) ;
81+ remoteStore . state . rooms = [ ... data . rooms ] ;
9282 } ) ;
9383
9484 this . socket . on ( 'joined' , async ( ) => {
9585 this . startSignaling ( ) ;
9686
9787 this . sendApp ( ) ;
9888
99- this . state . next ( ConnectionState . CONNECTED_WITH_SIGNALING_SERVER ) ;
89+ remoteStore . state . state = ConnectionState . CONNECTED_WITH_SIGNALING_SERVER ;
10090 } ) ;
10191
10292 this . socket . on ( 'deck_joined' , async ( ) => {
@@ -124,7 +114,7 @@ export class CommunicationService {
124114 }
125115 } ,
126116 ( _err ) => {
127- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
117+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
128118 }
129119 ) ;
130120 } else {
@@ -134,23 +124,23 @@ export class CommunicationService {
134124 } ) ;
135125
136126 this . socket . on ( 'connect_error' , ( ) => {
137- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
127+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
138128 } ) ;
139129
140130 this . socket . on ( 'connect_timeout' , ( ) => {
141- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
131+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
142132 } ) ;
143133
144134 this . socket . on ( 'error' , ( ) => {
145- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
135+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
146136 } ) ;
147137
148138 this . socket . on ( 'reconnect_failed' , ( ) => {
149- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
139+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
150140 } ) ;
151141
152142 this . socket . on ( 'reconnect_error' , ( ) => {
153- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
143+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
154144 } ) ;
155145
156146 resolve ( ) ;
@@ -163,7 +153,7 @@ export class CommunicationService {
163153 }
164154
165155 this . socket . emit ( 'join' , {
166- room : this . room
156+ room : this . room ,
167157 } ) ;
168158 }
169159
@@ -182,13 +172,13 @@ export class CommunicationService {
182172
183173 if ( this . socket ) {
184174 this . socket . emit ( 'leave' , {
185- room : this . room
175+ room : this . room ,
186176 } ) ;
187177 this . socket . removeAllListeners ( ) ;
188178 this . socket . disconnect ( ) ;
189179 }
190180
191- this . state . next ( ConnectionState . DISCONNECTED ) ;
181+ remoteStore . state . state = ConnectionState . DISCONNECTED ;
192182
193183 resolve ( ) ;
194184 } ) ;
@@ -200,18 +190,6 @@ export class CommunicationService {
200190 }
201191 }
202192
203- watchState ( ) : Observable < ConnectionState > {
204- return this . state . asObservable ( ) ;
205- }
206-
207- watchEvent ( ) : Observable < DeckdeckgoEvent > {
208- return this . event . asObservable ( ) ;
209- }
210-
211- watchRooms ( ) : Observable < ActiveRoom [ ] > {
212- return this . rooms . asObservable ( ) ;
213- }
214-
215193 private startSignaling ( ) {
216194 this . rtcPeerConn = new PeerConnection ( configuration ) ;
217195
@@ -223,7 +201,7 @@ export class CommunicationService {
223201 this . socket . emit ( 'signal' , {
224202 type : 'ice_candidate' ,
225203 message : JSON . stringify ( { candidate : evt . candidate } ) ,
226- room : this . room
204+ room : this . room ,
227205 } ) ;
228206 }
229207 } ;
@@ -235,7 +213,7 @@ export class CommunicationService {
235213 this . socket . emit ( 'signal' , {
236214 type : 'app_here' ,
237215 room : this . room ,
238- message : this . clientId
216+ message : this . clientId ,
239217 } ) ;
240218 }
241219
@@ -245,11 +223,11 @@ export class CommunicationService {
245223 this . socket . emit ( 'signal' , {
246224 type : 'sending_local_description' ,
247225 message : JSON . stringify ( { sdp : this . rtcPeerConn . localDescription } ) ,
248- room : this . room
226+ room : this . room ,
249227 } ) ;
250228 } ,
251229 ( _err ) => {
252- this . state . next ( ConnectionState . NOT_CONNECTED ) ;
230+ remoteStore . state . state = ConnectionState . NOT_CONNECTED ;
253231 }
254232 ) ;
255233 }
@@ -261,7 +239,7 @@ export class CommunicationService {
261239 } ;
262240
263241 private connectedWhenOpened = ( ) => {
264- this . state . next ( ConnectionState . CONNECTED ) ;
242+ remoteStore . state . state = ConnectionState . CONNECTED ;
265243 } ;
266244
267245 private receiveDataChannelMessage = ( event ) => {
@@ -270,6 +248,6 @@ export class CommunicationService {
270248 }
271249
272250 const data : DeckdeckgoEvent = JSON . parse ( event . data ) ;
273- this . event . next ( data ) ;
251+ remoteStore . state . $ event = { ... data } ;
274252 } ;
275253}
0 commit comments