-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusingWebSocket.js
213 lines (168 loc) · 5.79 KB
/
usingWebSocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const WebSocket = require("ws");
class WebSocketClient {
constructor() {
this.webSocket = null;
this.registertoken = null;
this.callID = null;
}
async connect(url, username, password) {
return new Promise((resolve, reject) => {
this.webSocket = new WebSocket(url, {
headers: {
Authorization: `Basic ${btoa(`${username}:${password}`)}`,
},
});
this.webSocket.on("open", () => {
console.log("WebSocket connected");
resolve();
});
this.webSocket.on("close", () => {
console.log("WebSocket disconnected");
});
this.webSocket.on("message", (message) => {
console.log("Received message:", JSON.parse(message.toString("utf-8")));
try {
const response = JSON.parse(message.toString("utf-8"));
if (response.event === "response") {
// Check if the response is an "event" type response and has the expected correlationId
if (response.responseStatusCode === 200 ) {
if ( response.correlationId === "1171772563" ){
// Check if the response status code is 200 (success)
if (response.registerToken) {
// If it has a registerToken, resolve with it
this.registerToken = response.registerToken;
this.subscribeToCall(this.registerToken);
console.log("Got the token!");
}
}
if (response.correlationId === "11237085767"){
console.log("Call Accepted")
}
if( response.correlationId === "1437085762"){
console.log("Call Rejected")
}
if ( response.correlationId === "1433645762"){
console.log("Call Ended")
}
}
}
// if (response.event === "callAccepted"){
// // this.endCall(this.registerToken, this.callID)
// console.log("Call Ended!")
// }
if (response.event === "callPresented") {
if (response.call.callId) {
this.callID = response.call.callId;
this.callAccepted(this.registerToken, this.callID)
// this.rejectCall(this.registerToken, this.callID)
// this.endCall(this.registerToken, this.callID)
// This is a call object
const call = response.call;
console.log("Received call object:", call);
// Now you can work with the call object as needed
}
}
if (response.event === "messageReceived"){
if(response.message){
const body = "hello"
this.MessageSend(this.registerToken, this.callID, body ) // <-- ( Hello there ) would be the value of the textbox message in the website
console.log("Message sent")
}
}
} catch (error) {
// Handle JSON parsing errors
reject(error);
}
});
this.webSocket.on("error", (error) => {
console.error("WebSocket error:", error);
reject();
});
});
}
async disconnect() {
if (this.webSocket) {
this.webSocket.close();
}
}
async sendMessage(message) {
if (this.webSocket) {
this.webSocket.send(JSON.stringify(message));
}
}
async registerAgencyAction(agencyID, agencySecret) {
const requests = {
action: "registerAgency",
agencyIdentifier: "hackrtc-28",
correlationId: "1171772563",
secret: "tzwIcR7g",
maintenanceMode: false,
};
webSocketClient.sendMessage(requests);
}
async subscribeToCall(registerToken) {
const requests = {
action: "subscribe",
correlationId: "1647085767",
registerToken: registerToken,
};
webSocketClient.sendMessage(requests);
}
async callAccepted(registerToken, callID){
const requests = {
action: "acceptCall",
correlationId: "11237085767",
registerToken: registerToken,
callId: callID
}
webSocketClient.sendMessage(requests)
}
async rejectCall(registerToken, callID){
const requests = {
action: "rejectCall",
correlationId: "1437085762",
registerToken: registerToken,
callId: callID
}
webSocketClient.sendMessage(requests)
}
async endCall(registerToken, callID){
const requests = {
action: "endCall",
correlationId: "1433645762",
registerToken: registerToken,
callId: callID,
body: "Ending call..."
}
webSocketClient.sendMessage(requests)
}
async MessageSend(registerToken,callId, body){
const requests = {
action: "sendMessage",
correlationId: "1232645762",
registerToken: registerToken,
callId: callId,
body: body
}
webSocketClient.sendMessage(requests)
}
}
// Usage example
const webSocketClient = new WebSocketClient();
const url = "wss://hackrtc.indigital.dev/text-control-api/v3";
const username = "hackrtc-28";
const password = "tzwIcR7g";
const agencyID = "hackrtc-28";
const agencySecret = "tzwIcR7g";
webSocketClient
.connect(url, username, password)
.then(() => {
console.log("Connected!");
webSocketClient.registerAgencyAction(agencyID, agencySecret);
console.log("Registered");
// You can send and receive messages here
// Example: webSocketClient.sendMessage({ action: 'subscribe', correlationId: '123' });
})
.catch((error) => {
console.error("WebSocket connection error:", error);
});