forked from butorenjin/easyrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_data_channel_messaging.js
172 lines (150 loc) · 6.36 KB
/
demo_data_channel_messaging.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
//
//Copyright (c) 2016, Skedans Systems, Inc.
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
//LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
var selfEasyrtcid = "";
var connectList = {};
var channelIsActive = {}; // tracks which channels are active
function connect() {
easyrtc.enableDebug(false);
easyrtc.enableDataChannels(true);
easyrtc.enableVideo(false);
easyrtc.enableAudio(false);
easyrtc.enableVideoReceive(false);
easyrtc.enableAudioReceive(false);
easyrtc.setDataChannelOpenListener(openListener);
easyrtc.setDataChannelCloseListener(closeListener);
easyrtc.setPeerListener(addToConversation);
easyrtc.setRoomOccupantListener(convertListToButtons);
easyrtc.connect("easyrtc.dataMessaging", loginSuccess, loginFailure);
}
function addToConversation(who, msgType, content) {
// Escape html special characters, then add linefeeds.
content = content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
content = content.replace(/\n/g, '<br />');
document.getElementById('conversation').innerHTML +=
"<b>" + who + ":</b> " + content + "<br />";
}
function openListener(otherParty) {
channelIsActive[otherParty] = true;
updateButtonState(otherParty);
}
function closeListener(otherParty) {
channelIsActive[otherParty] = false;
updateButtonState(otherParty);
}
function convertListToButtons(roomName, occupantList, isPrimary) {
connectList = occupantList;
var otherClientDiv = document.getElementById('otherClients');
while (otherClientDiv.hasChildNodes()) {
otherClientDiv.removeChild(otherClientDiv.lastChild);
}
var label, button;
for (var easyrtcid in connectList) {
var rowGroup = document.createElement("span");
var rowLabel = document.createTextNode(easyrtc.idToName(easyrtcid));
rowGroup.appendChild(rowLabel);
button = document.createElement('button');
button.id = "connect_" + easyrtcid;
button.onclick = function(easyrtcid) {
return function() {
startCall(easyrtcid);
};
}(easyrtcid);
label = document.createTextNode("Connect");
button.appendChild(label);
rowGroup.appendChild(button);
button = document.createElement('button');
button.id = "send_" + easyrtcid;
button.onclick = function(easyrtcid) {
return function() {
sendStuffP2P(easyrtcid);
};
}(easyrtcid);
label = document.createTextNode("Send Message");
button.appendChild(label);
rowGroup.appendChild(button);
otherClientDiv.appendChild(rowGroup);
updateButtonState(easyrtcid);
}
if (!otherClientDiv.hasChildNodes()) {
otherClientDiv.innerHTML = "<em>Nobody else logged in to talk to...</em>";
}
}
function updateButtonState(otherEasyrtcid) {
var isConnected = channelIsActive[otherEasyrtcid];
if(document.getElementById('connect_' + otherEasyrtcid)) {
document.getElementById('connect_' + otherEasyrtcid).disabled = isConnected;
}
if( document.getElementById('send_' + otherEasyrtcid)) {
document.getElementById('send_' + otherEasyrtcid).disabled = !isConnected;
}
}
function startCall(otherEasyrtcid) {
if (easyrtc.getConnectStatus(otherEasyrtcid) === easyrtc.NOT_CONNECTED) {
try {
easyrtc.call(otherEasyrtcid,
function(caller, media) { // success callback
if (media === 'datachannel') {
// console.log("made call succesfully");
connectList[otherEasyrtcid] = true;
}
},
function(errorCode, errorText) {
connectList[otherEasyrtcid] = false;
easyrtc.showError(errorCode, errorText);
},
function(wasAccepted) {
// console.log("was accepted=" + wasAccepted);
}
);
}catch( callerror) {
console.log("saw call error ", callerror);
}
}
else {
easyrtc.showError("ALREADY-CONNECTED", "already connected to " + easyrtc.idToName(otherEasyrtcid));
}
}
function sendStuffP2P(otherEasyrtcid) {
var text = document.getElementById('sendMessageText').value;
if (text.replace(/\s/g, "").length === 0) { // Don't send just whitespace
return;
}
if (easyrtc.getConnectStatus(otherEasyrtcid) === easyrtc.IS_CONNECTED) {
easyrtc.sendDataP2P(otherEasyrtcid, 'msg', text);
}
else {
easyrtc.showError("NOT-CONNECTED", "not connected to " + easyrtc.idToName(otherEasyrtcid) + " yet.");
}
addToConversation("Me", "msgtype", text);
document.getElementById('sendMessageText').value = "";
}
function loginSuccess(easyrtcid) {
selfEasyrtcid = easyrtcid;
document.getElementById("iam").innerHTML = "I am " + easyrtcid;
}
function loginFailure(errorCode, message) {
easyrtc.showError(errorCode, "failure to login");
}