-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
99 lines (84 loc) · 2.59 KB
/
socket.js
File metadata and controls
99 lines (84 loc) · 2.59 KB
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
/**
*
* Options:
*
*
*/
import { Connection } from './connection.js';
/**
* Generate a simple random string to identify the request/response.
* Function borrowed from: https://stackoverflow.com/a/1349426/7082336
*/
function make_id() {
let text = "";
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( let i = 0; i < 8; i++ ) {
text += possible.charAt( Math.floor( Math.random() * possible.length ) );
}
return text;
}
export class Socket extends Connection {
constructor( options = {} ) {
super( options );
if( typeof( options.url ) == 'undefined' || options.url == '' ) {
throw new Error( "Socket: missing required url" );
}
this.type = 'socket';
this.is_connected = new Promise( ( resolve, reject ) => {
this.socket = new WebSocket( options.url );
this.socket.addEventListener( 'message', this.receive );
this.socket.addEventListener( 'open', resolve );
this.socket.addEventListener( 'close', reject );
this.socket.addEventListener( 'error', this.error );
} );
}
/**
* Send data to the server, this function guarantees that the data will
* be sent to the server as long as the connection is opened.
*
* data can be either a string or object, if it's an object it will be
* flattened as JSON and returned to the server stringified.
*
* @param {Object/String} data - a data packet to send to the server.
*/
send( data ) {
this.is_connected.then( () => {
if( typeof( data ) !== 'object' ) {
data = {
data,
};
}
data.call_id = make_id();
let send_data = JSON.stringify( data );
this.socket.send( send_data );
} );
}
/**
* receive data from the server and process and dispatch it.
* @return {[type]} [description]
*/
receive( event ) {
console.log( event );
if( event.data ) {
}
}
/**
* error handler event
* @param {[type]} event [description]
* @return {[type]} [description]
*/
error( event ) {
}
/**
* close the socket, using a promise so you can chain events
* on closing the socket.
*
* @return {Promise} - Success means the connection is now closed.
*/
close() {
return new Promise( ( resolve, reject ) => {
this.socket.addEventListener( 'close', resolve );
this.socket.close();
} );
}
}