|
93 | 93 | return msg.replace(/</g, '<');
|
94 | 94 | }
|
95 | 95 |
|
96 |
| - function initFullScreen() { |
| 96 | + function initFullScreen() { |
97 | 97 | var button = document.getElementById("fullscreen");
|
98 | 98 | 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 |
101 | 101 | elem.webkitRequestFullScreen();
|
102 | 102 | });
|
103 |
| - } |
| 103 | + } |
104 | 104 |
|
105 | 105 | function initNewRoom() {
|
106 | 106 | var button = document.getElementById("newRoom");
|
|
114 | 114 | var rnum = Math.floor(Math.random() * chars.length);
|
115 | 115 | randomstring += chars.substring(rnum,rnum+1);
|
116 | 116 | }
|
117 |
| - |
| 117 | + |
118 | 118 | window.location.hash = randomstring;
|
119 | 119 | location.reload();
|
120 | 120 | })
|
121 | 121 | }
|
122 | 122 |
|
| 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 | + |
123 | 147 | 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 | + |
124 | 158 | var input = document.getElementById("chatinput");
|
125 | 159 | var room = window.location.hash.slice(1);
|
126 | 160 | var color = "#"+((1<<24)*Math.random()|0).toString(16);
|
127 | 161 |
|
128 | 162 | input.addEventListener('keydown', function(event) {
|
129 | 163 | var key = event.which || event.keyCode;
|
130 | 164 | if (key === 13) {
|
131 |
| - rtc._socket.send(JSON.stringify({ |
| 165 | + chat.send(JSON.stringify({ |
132 | 166 | "eventName": "chat_msg",
|
133 | 167 | "data": {
|
134 | 168 | "messages": input.value,
|
135 | 169 | "room": room,
|
136 | 170 | "color": color
|
137 | 171 | }
|
138 |
| - }), function(error) { |
139 |
| - if (error) { |
140 |
| - console.log(error); |
141 |
| - } |
142 |
| - }); |
| 172 | + })); |
143 | 173 | addToChat(input.value);
|
144 | 174 | input.value = "";
|
145 | 175 | }
|
146 | 176 | }, false);
|
147 |
| - rtc.on('receive_chat_msg', function(data) { |
| 177 | + rtc.on(chat.event, function() { |
| 178 | + data = chat.recv.apply(this, arguments); |
148 | 179 | console.log(data.color);
|
149 | 180 | addToChat(data.messages, data.color.toString(16));
|
150 | 181 | });
|
|
163 | 194 | 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');
|
164 | 195 | }
|
165 | 196 |
|
166 |
| - |
| 197 | + |
167 | 198 | var room = window.location.hash.slice(1);
|
168 | 199 |
|
169 | 200 | //When using localhost
|
|
184 | 215 | initNewRoom();
|
185 | 216 | initChat();
|
186 | 217 | }
|
187 |
| - |
| 218 | + |
188 | 219 | window.onresize = function(event) {
|
189 | 220 | subdivideVideos();
|
190 | 221 | };
|
|
0 commit comments