Skip to content

Commit a91322c

Browse files
author
Spyes
committed
using phoenix library
1 parent 97016f5 commit a91322c

File tree

2 files changed

+1030
-20
lines changed

2 files changed

+1030
-20
lines changed

index.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
const request = require('request'),
4-
WebSocket = require('ws'),
4+
Socket = require('./socket'),
55
fs = require('fs');
66

77
class RapidAPI {
@@ -129,7 +129,7 @@ class RapidAPI {
129129
onClose = () => {}
130130
} = callbacks;
131131
request({
132-
uri: `${RapidAPI.callbackBaseURL()}/api/get_token?user_id=${user_id}&params=${JSON.stringify(params)}`,
132+
uri: `${RapidAPI.callbackBaseURL()}/api/get_token?user_id=${user_id}`,
133133
method: 'GET',
134134
headers: {
135135
"Content-Type": "application/json"
@@ -142,24 +142,16 @@ class RapidAPI {
142142
}, (error, response, body) => {
143143
const { token } = JSON.parse(body);
144144
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);
163155
});
164156
}
165157
}

0 commit comments

Comments
 (0)