-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
113 lines (98 loc) · 3.58 KB
/
node.js
File metadata and controls
113 lines (98 loc) · 3.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const RTCPeerConnection = require('wrtc').RTCPeerConnection;
const RTCSessionDescription = require('wrtc').RTCSessionDescription;
const RTCIceCandidate = require('wrtc').RTCIceCandidate;
const { io } = require("socket.io-client");
const myId = "NODE_" + Date.now();
const role = "NODE"
var IEpeers = [];
var nodePeers = [];
const CONNECTION_STRING = 'http://ec2-3-208-18-248.compute-1.amazonaws.com:8000';
// const CONNECTION_STRING = 'ws://localhost:8000';
var brokerSocket = io.connect(CONNECTION_STRING, {reconnect: true, query: {"id": myId, "role": role}});
brokerSocket.on('connect', function (s) {
console.log('Node Connected to Broker!');
});
brokerSocket.on('connect_error', (err) => {
console.log("Broker " + err.message);
})
brokerSocket.on("RECRUITMENT_BROADCAST", (payload) => {
console.log("Received RECRUITMENT_BROADCAST from " + payload.invokingEndpointId + " to connect to " + payload.initiatorId)
if(IEpeers.find(p => p.id === payload.initiatorId) === undefined){
console.log("Sending RECRUITMENT_ACCEPT to " + payload.initiatorId)
payload.answererId = myId;
brokerSocket.emit("RECRUITMENT_ACCEPT", payload);
}
})
brokerSocket.on("INCOMING_CONNECTION", (payload) => {
console.log("Received INCOMING_CONNECTION from " + payload.initiatorId)
const peer = new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.stunprotocol.org"
},
{
urls: 'turn:numb.viagenie.ca',
credential: 'muazkh',
username: 'webrtc@live.com'
},
]
});
peer.ondatachannel = (event) => {
console.log("ondatachannel")
console.log(event)
const testChannel = event.channel;
testChannel.onmessage = (e) => {
let value = parseInt(e.data);
console.log("received " + value++);
testChannel.send(value.toString())
}
}
if(payload.role === "NODE"){
nodePeers.push({
id: payload.initiatorId,
peer: peer
})
} else {
IEpeers.push({
id: payload.initiatorId,
peer: peer
})
}
peer.onicecandidate = handleICECandidateEvent(payload);
peer.onconnectionstatechange = (event) => {
console.log(event.type + " " + peer.connectionState)
};
const desc = new RTCSessionDescription(payload.sdp);
peer.setRemoteDescription(desc).then(() => {
return peer.createAnswer();
}).then(answer => {
return peer.setLocalDescription(answer);
}).then(() => {
console.log("Sending ANSWER_CONNECTION to " + payload.initiatorId)
payload.sdp = peer.localDescription;
brokerSocket.emit("ANSWER_CONNECTION", payload)
})
})
function handleICECandidateEvent(payload) {
return (e) => {
if (e.candidate) {
const icePayload = {
fromId: myId,
senderRole: role,
toId: payload.initiatorId,
receiverRole: payload.initiatorRole,
candidate: e.candidate
}
brokerSocket.emit("ICE_CANDIDATE", icePayload);
}
}
}
brokerSocket.on('ICE_CANDIDATE', payload => {
console.log("Received ICE_CANDIDATE from " + payload.fromId)
const candidate = new RTCIceCandidate(payload.candidate);
IEpeers
.find(p => p.id === payload.fromId)
.peer
.addIceCandidate(candidate)
.catch(e => console.log(e));
})