97 | RTCMultiConnection 98 | All-in-One test 99 | ® 100 | Muaz Khan 101 |
102 |103 | HOME 104 | © 105 | Muaz Khan 106 | 107 | . 108 | @@WebRTCWeb 109 | 110 | . 111 | Github 112 | 113 | . 114 | Latest issues 115 | 116 | . 117 | What's New? 118 |
119 |131 | It is suggested to try MultiRTC! 132 |
133 | 134 |WebRTC DataChannel
170 |
173 | Text Chat174 | 175 | 176 | 177 | |
178 |
179 | Share Files180 | 181 | 182 | 183 | |
184 |
How to use XHR for RTCMultiConnection signaling?
497 | 498 |499 | // database has a single table; which has two columns: 500 | // 1) Message (required to store JSON data) 501 | // 2) ID (optional: as primary key) 502 | 503 | // a simple function to make XMLHttpRequests 504 | function xhr(url, callback, data) { 505 | if (!window.XMLHttpRequest || !window.JSON) return; 506 | 507 | var request = new XMLHttpRequest(); 508 | request.onreadystatechange = function () { 509 | if (callback && request.readyState == 4 && request.status == 200) { 510 | // server MUST return JSON text 511 | callback(JSON.parse(request.responseText)); 512 | } 513 | }; 514 | request.open('POST', url); 515 | 516 | var formData = new FormData(); 517 | 518 | // you're passing "message" parameter 519 | formData.append('message', data); 520 | 521 | request.send(formData); 522 | } 523 | 524 | // this object is used to store "onmessage" callbacks from "openSignalingChannel handler 525 | var onMessageCallbacks = {}; 526 | 527 | // this object is used to make sure identical messages are not used multiple times 528 | var messagesReceived = {}; 529 | 530 | function repeatedlyCheck() { 531 | xhr('/Home/GetData', function (data) { 532 | // if server says nothing; wait. 533 | if (data == false) return setTimeout(repeatedlyCheck, 400); 534 | 535 | // if already receied same message; skip. 536 | if (messagesReceived[data.ID]) return setTimeout(repeatedlyCheck, 400); 537 | messagesReceived[data.ID] = data.Message; 538 | 539 | // "Message" property is JSON-ified in "openSignalingChannel handler 540 | data = JSON.parse(data.Message); 541 | 542 | // don't pass self messages over "onmessage" handlers 543 | if (data.sender != connection.userid && onMessageCallbacks[data.channel]) { 544 | onMessageCallbacks[data.channel](data.message); 545 | } 546 | 547 | // repeatedly check the database 548 | setTimeout(repeatedlyCheck, 1); 549 | }); 550 | } 551 | 552 | repeatedlyCheck(); 553 | 554 | // overriding "openSignalingChannel handler 555 | connection.openSignalingChannel = function (config) { 556 | var channel = config.channel || this.channel; 557 | onMessageCallbacks[channel] = config.onmessage; 558 | 559 | // let RTCMultiConnection know that server connection is opened! 560 | if (config.onopen) setTimeout(config.onopen, 1); 561 | 562 | // returning an object to RTCMultiConnection 563 | // so it can send data using "send" method 564 | return { 565 | send: function (data) { 566 | data = { 567 | channel: channel, 568 | message: data, 569 | sender: connection.userid 570 | }; 571 | 572 | // posting data to server 573 | // data is also JSON-ified. 574 | xhr('/Home/PostData', null, JSON.stringify(data)); 575 | }, 576 | channel: channel 577 | }; 578 | }; 579 |580 |