Skip to content

Commit aa38365

Browse files
committed
Merge pull request #11 from jbenet/master
DataChannel support
2 parents 243c217 + af0231f commit aa38365

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

example/index.html

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@
9393
return msg.replace(/</g, '&lt;');
9494
}
9595

96-
function initFullScreen() {
96+
function initFullScreen() {
9797
var button = document.getElementById("fullscreen");
9898
button.addEventListener('click', function(event) {
99-
var elem = document.getElementById("videos");
100-
//show full screen
99+
var elem = document.getElementById("videos");
100+
//show full screen
101101
elem.webkitRequestFullScreen();
102102
});
103-
}
103+
}
104104

105105
function initNewRoom() {
106106
var button = document.getElementById("newRoom");
@@ -114,37 +114,68 @@
114114
var rnum = Math.floor(Math.random() * chars.length);
115115
randomstring += chars.substring(rnum,rnum+1);
116116
}
117-
117+
118118
window.location.hash = randomstring;
119119
location.reload();
120120
})
121121
}
122122

123+
124+
var websocketChat = {
125+
send: function (message) {
126+
rtc._socket.send(message);
127+
},
128+
recv: function (message) {
129+
return message;
130+
},
131+
event: 'receive_chat_msg'
132+
};
133+
134+
var dataChannelChat = {
135+
send: function (message) {
136+
for (var connection in rtc.dataChannels) {
137+
var channel = rtc.dataChannels[connection];
138+
channel.send(message);
139+
}
140+
},
141+
recv: function (channel, message) {
142+
return JSON.parse(message).data;
143+
},
144+
event: 'data stream data'
145+
};
146+
123147
function initChat() {
148+
var chat;
149+
150+
if (rtc.dataChannelSupport) {
151+
console.log('initializing data channel chat');
152+
chat = dataChannelChat;
153+
} else {
154+
console.log('initializing websocket chat');
155+
chat = websocketChat;
156+
}
157+
124158
var input = document.getElementById("chatinput");
125159
var room = window.location.hash.slice(1);
126160
var color = "#"+((1<<24)*Math.random()|0).toString(16);
127161

128162
input.addEventListener('keydown', function(event) {
129163
var key = event.which || event.keyCode;
130164
if (key === 13) {
131-
rtc._socket.send(JSON.stringify({
165+
chat.send(JSON.stringify({
132166
"eventName": "chat_msg",
133167
"data": {
134168
"messages": input.value,
135169
"room": room,
136170
"color": color
137171
}
138-
}), function(error) {
139-
if (error) {
140-
console.log(error);
141-
}
142-
});
172+
}));
143173
addToChat(input.value);
144174
input.value = "";
145175
}
146176
}, false);
147-
rtc.on('receive_chat_msg', function(data) {
177+
rtc.on(chat.event, function() {
178+
data = chat.recv.apply(this, arguments);
148179
console.log(data.color);
149180
addToChat(data.messages, data.color.toString(16));
150181
});
@@ -163,7 +194,7 @@
163194
alert('Your browser is not supported or you have to turn on flags. In chrome you go to chrome://flags and turn on Enable PeerConnection remember to restart chrome');
164195
}
165196

166-
197+
167198
var room = window.location.hash.slice(1);
168199

169200
//When using localhost
@@ -184,7 +215,7 @@
184215
initNewRoom();
185216
initChat();
186217
}
187-
218+
188219
window.onresize = function(event) {
189220
subdivideVideos();
190221
};

example/webrtc.io.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../node_modules/webrtc.io/node_modules/webrtc.io-client/lib/webrtc.io.js
1+
../node_modules/webrtc.io-client/lib/webrtc.io.js

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"webrtc.io": "latest",
10+
"webrtc.io-client": "latest",
1011
"express": "2.5.1",
1112
"ws": "latest"
1213
},

0 commit comments

Comments
 (0)