File size: 861 Bytes
cf0f72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Set RTC options.
var rtcOpts = {
	room: 'test-data',
	signaller: 'https://switchboard.rtc.io',
	capture: false
};

// call RTC module
var rtc = RTC(rtcOpts);

// A contenteditable element to show our messages
var messages = document.getElementById('messages');

// Bind to events happening on the data channel
function bindDataChannelEvents(id, channel, attributes, connection) {

	// Receive message
	channel.onmessage = function (evt) {
	    messages.innerHTML = evt.data;
	};

	// Send message
	messages.onkeyup = function () {
		channel.send(this.innerHTML);
	};
}

// Start working with the established session
function init(session) {
	session.createDataChannel('chat', {
  		ordered: true,
  		maxRetransmits: 12
 	});
  	session.on('channel:opened:chat', bindDataChannelEvents);
}

// Detect when RTC has established a session
rtc.on('ready', init);