1
1
"use strict" ;
2
2
3
3
const request = require ( 'request' ) ,
4
- WebSocket = require ( 'ws ' ) ,
4
+ Socket = require ( './socket ' ) ,
5
5
fs = require ( 'fs' ) ;
6
6
7
7
class RapidAPI {
@@ -129,7 +129,7 @@ class RapidAPI {
129
129
onClose = ( ) => { }
130
130
} = callbacks ;
131
131
request ( {
132
- uri : `${ RapidAPI . callbackBaseURL ( ) } /api/get_token?user_id=${ user_id } ¶ms= ${ JSON . stringify ( params ) } ` ,
132
+ uri : `${ RapidAPI . callbackBaseURL ( ) } /api/get_token?user_id=${ user_id } ` ,
133
133
method : 'GET' ,
134
134
headers : {
135
135
"Content-Type" : "application/json"
@@ -142,24 +142,16 @@ class RapidAPI {
142
142
} , ( error , response , body ) => {
143
143
const { token } = JSON . parse ( body ) ;
144
144
const sock_url = `${ RapidAPI . websocketBaseURL ( ) } /socket/websocket?token=${ token } ` ;
145
- const ws = new WebSocket ( sock_url ) ;
146
- ws . onmessage = ( msg ) => {
147
- const { payload, event } = JSON . parse ( msg . data ) ;
148
- if ( ! event . startsWith ( "phx_" ) ) {
149
- onMessage ( payload . body ) ;
150
- }
151
- } ;
152
- ws . onclose = ( code , reason ) => {
153
- onClose ( code , reason ) ;
154
- } ;
155
- ws . onopen = ( ) => {
156
- ws . send ( JSON . stringify ( {
157
- topic : `users_socket:${ user_id } ` ,
158
- event : "phx_join" ,
159
- payload : { } ,
160
- ref : '1'
161
- } ) ) ;
162
- } ;
145
+ const socket = new Socket . Socket ( sock_url , {
146
+ params : { token}
147
+ } ) ;
148
+ socket . connect ( ) ;
149
+ const channel = socket . channel ( `users_socket:${ user_id } ` , params ) ;
150
+ channel . on ( 'new_msg' , msg => onMessage ( msg . body ) ) ;
151
+ channel . join ( )
152
+ . receive ( 'error' , reason => onError ( reason ) )
153
+ . receive ( 'timeout' , ( ) => console . log ( "network issue" ) ) ;
154
+ channel . onClose ( onClose ) ;
163
155
} ) ;
164
156
}
165
157
}
0 commit comments