-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
146 lines (108 loc) · 2.6 KB
/
index.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
'use strict';
const debug = require('debug')('jssip-node-websocket');
const debugerror = require('debug')('jssip-node-websocket:ERROR');
const parse = require('url-parse');
const W3CWebSocket = require('websocket').w3cwebsocket;
class NodeWebSocket
{
constructor(url, options)
{
debug('new() [url:"%s", options:%o]', url, options);
this._url = url;
this._options = options || {};
this._sipUri = null;
this._viaTransport = null;
this._ws = null;
var u = parse(url, true);
if (!u)
throw new TypeError('wrong url');
var scheme = u.protocol.toLowerCase().replace(/:/, '');
if ([ 'ws', 'wss' ].indexOf(scheme) === -1)
throw new TypeError('invalid WebSocket scheme');
this._sipUri = `sip:${u.hostname}${u.port ? ':' + u.port : ''};transport=ws`;
this._viaTransport = scheme.toUpperCase();
}
get url()
{
return this._url;
}
get sip_uri()
{
return this._sipUri;
}
get via_transport()
{
return this._viaTransport;
}
set via_transport(value)
{
this._viaTransport = value.toUpperCase();
}
connect()
{
debug('connect()');
if (this.isConnected())
{
debug('WebSocket already connected [url:"%s"]', this._url);
return;
}
else if (this.isConnecting())
{
debug('WebSocket already connecting [url:"%s"]', this._url);
return;
}
if (this._ws)
this._ws.close();
debug('WebSocket connecting [url:"%s"]', this._url);
var options = this._options;
this._ws = new W3CWebSocket(this._url, 'sip',
options.origin, options.headers, options.requestOptions, options.clientConfig);
this._ws.binaryType = 'arraybuffer';
this._ws.onopen = () =>
{
debug('WebSocket open [url:"%s"]', this._url);
this.onconnect();
};
this._ws.onclose = (event) =>
{
debug('WebSocket closed [url:"%s", code:%s, reason:"%s", wasClean:%s]',
this._url, event.code, event.reason, event.wasClean);
this.ondisconnect(event.wasClean, event.code, event.reason);
};
this._ws.onerror = () =>
{
debug('WebSocket error [url:"%s"]', this._url);
};
this._ws.onmessage = (event) =>
{
debug('WebSocket message received');
this.ondata(event.data);
};
}
disconnect()
{
debug('disconnect()');
this._ws.close();
this._ws = null;
}
send(message)
{
debug('send()');
if (!this.isConnected())
{
debugerror('send() | unable to send message, WebSocket is not open');
return false;
}
this._ws.send(message);
return true;
}
isConnected()
{
return this._ws && this._ws.readyState === this._ws.OPEN;
}
isConnecting()
{
return this._ws && this._ws.readyState === this._ws.CONNECTING;
}
}
module.exports = NodeWebSocket;