1
1
import { Channel } from "./Channel" ;
2
2
import { AxiosResponse } from "axios" ;
3
3
4
- export type Options = { authEndpoint : string , host : string } ;
4
+ export type ChannelAuthorizationCallback = (
5
+ error : Error | null ,
6
+ authData : any | null
7
+ ) => void ;
8
+
9
+ export interface DeprecatedChannelAuthorizer {
10
+ authorize ( socketId : string , channelName : string , callback : ChannelAuthorizationCallback ) : void ;
11
+ }
12
+
13
+ export interface ChannelAuthorizerGenerator {
14
+ (
15
+ channel : Channel ,
16
+ options : DeprecatedAuthorizerOptions
17
+ ) : DeprecatedChannelAuthorizer ;
18
+ }
19
+
20
+ export interface DeprecatedAuthOptions {
21
+ params ?: any ;
22
+ headers ?: any ;
23
+ }
24
+
25
+ export interface DeprecatedAuthorizerOptions {
26
+ authTransport : 'ajax' | 'jsonp' ;
27
+ authEndpoint : string ;
28
+ auth ?: DeprecatedAuthOptions ;
29
+ }
30
+
31
+ export type Options = { authEndpoint : string , host : string , authorizer ?: DeprecatedChannelAuthorizer } ;
32
+
5
33
export type MessageBody = { event : string , channel ?: string , data : object } ;
6
34
7
35
export class Websocket {
@@ -58,7 +86,7 @@ export class Websocket {
58
86
}
59
87
}
60
88
61
- this . on ( 'whoami' , ( { socket_id : socketId } ) => {
89
+ this . on ( 'whoami' , ( { socket_id : socketId } ) => {
62
90
this . socketId = socketId
63
91
64
92
console . log ( `just set socketId to ${ socketId } ` )
@@ -88,24 +116,10 @@ export class Websocket {
88
116
return this
89
117
}
90
118
91
- protected parseMessage ( body : string ) : MessageBody {
92
- try {
93
- return JSON . parse ( body )
94
- } catch ( error ) {
95
- console . error ( error )
96
-
97
- return undefined
98
- }
99
- }
100
-
101
119
getSocketId ( ) : string {
102
120
return this . socketId
103
121
}
104
122
105
- private socketIsReady ( ) : boolean {
106
- return this . websocket . readyState === this . websocket . OPEN
107
- }
108
-
109
123
send ( message : object ) : void {
110
124
if ( this . socketIsReady ( ) ) {
111
125
this . websocket . send ( JSON . stringify ( message ) )
@@ -115,7 +129,7 @@ export class Websocket {
115
129
this . buffer . push ( message )
116
130
}
117
131
118
- close ( ) : void {
132
+ close ( ) : void {
119
133
this . internalListeners = { }
120
134
121
135
clearInterval ( this . pingInterval )
@@ -132,39 +146,6 @@ export class Websocket {
132
146
}
133
147
}
134
148
135
- private actuallySubscribe ( channel : Channel ) : void {
136
- if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
137
- console . log ( `Sending auth request for channel ${ channel . name } ` )
138
-
139
- axios . post ( this . options . authEndpoint , {
140
- socket_id : this . getSocketId ( ) ,
141
- channel_name : channel . name ,
142
- } ) . then ( ( response : AxiosResponse ) => {
143
- console . log ( `Subscribing to channels ${ channel . name } ` )
144
-
145
- this . send ( {
146
- event : 'subscribe' ,
147
- data : {
148
- channel : channel . name ,
149
- ... response . data
150
- } ,
151
- } )
152
- } ) . catch ( ( error ) => {
153
- console . log ( `Auth request for channel ${ channel . name } failed` )
154
- console . error ( error )
155
- } )
156
- } else {
157
- console . log ( `Subscribing to channels ${ channel . name } ` )
158
-
159
- this . send ( {
160
- event : 'subscribe' ,
161
- data : {
162
- channel : channel . name ,
163
- } ,
164
- } )
165
- }
166
- }
167
-
168
149
unsubscribe ( channel : Channel ) : void {
169
150
this . send ( {
170
151
event : 'unsubscribe' ,
@@ -195,4 +176,71 @@ export class Websocket {
195
176
delete this . internalListeners [ event ]
196
177
}
197
178
}
179
+
180
+ protected parseMessage ( body : string ) : MessageBody {
181
+ try {
182
+ return JSON . parse ( body )
183
+ } catch ( error ) {
184
+ console . error ( error )
185
+
186
+ return undefined
187
+ }
188
+ }
189
+
190
+ private socketIsReady ( ) : boolean {
191
+ return this . websocket . readyState === this . websocket . OPEN
192
+ }
193
+
194
+ private actuallySubscribe ( channel : Channel ) : void {
195
+ if ( channel . name . startsWith ( 'private-' ) || channel . name . startsWith ( 'presence-' ) ) {
196
+ console . log ( `Sending auth request for channel ${ channel . name } ` )
197
+
198
+ if ( this . options . authorizer ) {
199
+ this . options . authorizer . authorize ( this . getSocketId ( ) , channel . name , ( error , authData ) => {
200
+ if ( ! error ) {
201
+ console . log ( `Subscribing to channels ${ channel . name } ` )
202
+
203
+ this . send ( {
204
+ event : 'subscribe' ,
205
+ data : {
206
+ channel : channel . name ,
207
+ ...authData
208
+ } ,
209
+ } )
210
+ } else {
211
+ console . log ( `Auth request for channel ${ channel . name } failed` )
212
+ console . error ( error )
213
+ }
214
+ } )
215
+ } else {
216
+ axios . post ( this . options . authEndpoint , {
217
+ socket_id : this . getSocketId ( ) ,
218
+ channel_name : channel . name ,
219
+ } ) . then ( ( response : AxiosResponse ) => {
220
+ console . log ( `Subscribing to channels ${ channel . name } ` )
221
+
222
+ this . send ( {
223
+ event : 'subscribe' ,
224
+ data : {
225
+ channel : channel . name ,
226
+ ...response . data
227
+ } ,
228
+ } )
229
+ } ) . catch ( ( error ) => {
230
+ console . log ( `Auth request for channel ${ channel . name } failed` )
231
+ console . error ( error )
232
+ } )
233
+ }
234
+
235
+ } else {
236
+ console . log ( `Subscribing to channels ${ channel . name } ` )
237
+
238
+ this . send ( {
239
+ event : 'subscribe' ,
240
+ data : {
241
+ channel : channel . name ,
242
+ } ,
243
+ } )
244
+ }
245
+ }
198
246
}
0 commit comments