javascript - Error in WebRTC connection -
i'm writing app android based on ionic2 , webrtc.
used library https://github.com/feross/simple-peer
use push notifications exchange signaling data between 2 apps.
when user wants contact other called connecttopeer() method , subsequently other receives request , returns response.
works fine except point have establish real connection, i have error.
class code:
var self; var peer; import {injectable} 'angular2/core'; import {storage, localstorage, events} 'ionic-angular'; @injectable() export class vallasciappdata { static parameters() { return [[events]]; } constructor(events) { //conacts data if(window.localstorage.getitem("contacts") == null || window.localstorage.getitem("contacts") == 'false'){ this.contacts = [ { username: 'this contact', id:'1' }, { username: 'another contact', id:'2' } ]; }else{ this.contacts = json.parse(window.localstorage.getitem("contacts")); } //messages data if(window.localstorage.getitem("messages") == null || window.localstorage.getitem("messages") == 'false'){ this.data = [{ username: 'myapp', id: '1', lastmessage: "welcome myapp", messages: [{ emitter: '1', type: 'text', text: 'welcome myapp', url: '0', date: '0/0/0', time: '0:0' }] }]; }else{ this.data = json.parse(window.localstorage.getitem("messages")); } window.localstorage.setitem("messages", json.stringify(this.data)); self=this; this.peers = new array(); this.savedata(); this.events = events; this.showprogressbar = true; var push = pushnotification.init({ android: { senderid: "xxxxxxxxxx" } }); push.on('registration', (data) => { // data.registrationid alert(data.registrationid); this.sendmyrid(data.registrationid); }); push.on('notification', (data) => { // data.message, // data.title, // data.count, // data.sound, // data.image, // data.additionaldata alert(data.title+data.message); console.log(data); //this.peers[sessionid] //var additionaldata = data.additionaldata if(data.additionaldata.additionaldata.type == "offer"){ console.log("offer ok"); peer = new simplepeer({initiator: false, trickle: false }); peer.on('error', (err) => { console.log('error', err) }); peer.on('signal', (datap) => { console.log('signal', json.stringify(datap)) var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function () { if (xhttp.readystate == 4 && xhttp.status == 200) { console.log(xhttp.responsetext); var xmlresponse = xhttp.responsexml; var reply = xmlresponse.getelementsbytagname("response")[0].childnodes[0].nodevalue; console.log(reply); if (reply == "send ok") {} else {} } }; var additionaldata = { 'sid': window.localstorage.getitem("id"), 'rid': data.additionaldata.additionaldata.sid, 'type': "answer", 'response': json.stringify(datap) }; console.log("ok1"); xhttp.open("post", "http://xxxxxxxxxxxxxxxxxxxx", true); xhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhttp.send("username=" + window.localstorage.getitem("username") + "&password=" + window.localstorage.getitem("password") + "&id=" + window.localstorage.getitem("id")+ "&rid=" + data.additionaldata.additionaldata.sid+ "&additionaldata=" + json.stringify(additionaldata)); console.log("ok2"); }); peer.signal(json.parse(data.additionaldata.additionaldata.response)); peer.on('connect', () => { console.log('connect'); peer.send('whatever' + math.random()); }); peer.on('data', (data) => { console.log('data: ' + data) }); } if(data.additionaldata.additionaldata.type == "answer"){ console.log("answer ok"); peer.signal(json.parse(data.additionaldata.additionaldata.response)); console.log("peer signal ok"); peer.send('prova messaggio'); console.log("prova messaggio"); } }); push.on('error', (e) => { // e.message }); } connecttopeer(sessionid){ if(this.peers[sessionid] == null){ console.log("okk"); peer = new simplepeer({ initiator: true, trickle: false }); peer.on('error', (err) => { console.log('error', err) }); peer.on('signal', (data) => { console.log('signal', json.stringify(data)); var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function () { if (xhttp.readystate == 4 && xhttp.status == 200) { console.log(xhttp.responsetext); var xmlresponse = xhttp.responsexml; var reply = xmlresponse.getelementsbytagname("response")[0].childnodes[0].nodevalue; console.log(reply); if (reply == "send ok") {} else {} } }; var additionaldata = { 'sid': window.localstorage.getitem("id"), 'rid': sessionid, 'type': "offer", 'response': json.stringify(data) }; console.log("ok1"); xhttp.open("post", "http://xxxxxxxxxxxxxxxxxxxxxxxx", true); xhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhttp.send("username=" + window.localstorage.getitem("username") + "&password=" + window.localstorage.getitem("password") + "&id=" + window.localstorage.getitem("id")+ "&rid=" + sessionid+ "&additionaldata=" + json.stringify(additionaldata)); console.log("ok2"); }); peer.on('connect', () => { console.log('connect'); peer.send('whatever' + math.random()); }); peer.on('data', (data) => { console.log('data: ' + data); }); }else{ console.log("noting"); } } sendmyrid(id) { var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function () { if (xhttp.readystate == 4 && xhttp.status == 200) { console.log(xhttp.responsetext); var xmlresponse = xhttp.responsexml; var reply = xmlresponse.getelementsbytagname("response")[0].childnodes[0].nodevalue; console.log(reply); if (reply == "update ok") {} else {} } }; xhttp.open("post", "http://xxxxxxxxxxxxxxxxx", true); xhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhttp.send("regid=" + id); } savedata() { //if(this.data != self.data) this.data = self.data; //this.showprogressbar = self.showprogressbar; console.log("ciao"); //var sd = json.parse(window.localstorage.getitem("messages")); /*if (self.data != sd) { self.data = json.parse(window.localstorage.getitem("messages")); }*/ settimeout(() => { this.savedata(); }, 500); } }
i did tests , i've found if make connection twice connection established, don't know why.
Comments
Post a Comment