This repository was archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.js
71 lines (65 loc) · 2.11 KB
/
adapter.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
var util = require('util');
var webrtc = require('wrtc');
var WebSocketClient = require('websocket').client;
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
RTCPeerConnection = function() {
webrtc.RTCPeerConnection.call(this);
var that = this;
(function checkIceState() {
setTimeout(function() {
if (that.iceGatheringState === 'complete') {
var nullCandidate = new RTCIceCandidate();
nullCandidate.candidate = null;
that.onicecandidate(nullCandidate);
} else {
checkIceState();
}
}, 1000).unref();
})(); // miserable hack, waiting for https://github.com/js-platform/node-webrtc/issues/44
};
util.inherits(RTCPeerConnection, webrtc.RTCPeerConnection);
RTCIceCandidate = webrtc.RTCIceCandidate;
RTCSessionDescription = webrtc.RTCSessionDescription;
DataChannel = webrtc.DataChannel;
var CustomWebSocket = function(url) {
if (!(this instanceof WebSocket)) {
return new CustomWebSocket(url);
}
var websocketClient = new WebSocketClient();
var that = this;
websocketClient.on('connect', function(connection) {
connection.on('message', function(msg) {
msg.data = msg.utf8Data;
that.onmessage(msg);
});
that.send = function(msg) {
connection.send(msg);
};
});
websocketClient.on('connect', function(msg) {
that.onopen(msg);
});
websocketClient.on('error', function(err) {
that.onerror(err);
});
websocketClient.on('connectFailed', function(err) {
that.onerror(err);
});
websocketClient.on('close', function(err) {
that.onclose(msg);
});
setTimeout(function() { // break event loop to set callbacks
websocketClient.connect(url);
}, 0);
return this;
};
CustomWebSocket.prototype = {
onopen: function() {},
onerror: function(err) {},
onmessage: function(msg) {},
onclose: function(msg) {},
send: function(msg) {},
close: function() {}
};
WebSocket = CustomWebSocket;