12-05-2018 07:00
12-05-2018 07:00
I try to send a message to the companion app frequently but it only works the first time and only if the the time interval is 0.5s.
The first time the message.peersocket.onopen() is executed but every further time it is not.
I can not figure out what is wrong. Following you can see my code:
app:
index.js
import document from "document"; import* as message from "./message"; let recBlue = document.getElementById("demoinstance"); //message.mess(); setInterval(message.mess,500);
message.js
import * as messaging from "messaging"; import document from "document"; export function mess(){ // Listen for the onopen event console.log("function"); messaging.peerSocket.onopen = function() { // Ready to send messages console.log("message"); sendMessage(); } // Listen for the onerror event messaging.peerSocket.onerror = function(err) { // Handle any errors console.log("Connection error: " + err.code + " - " + err.message); } // Send a message to the peer function sendMessage() { console.log("data"); // Sample data var data = { title: 'My test data', isTest: true, records: [1, 2, 3, 4] } if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { // Send the data to peer as a message console.log("send"); messaging.peerSocket.send(data); } } }
companion:
index.js
import * as messaging from "messaging"; // Listen for the onmessage event messaging.peerSocket.onmessage = function(evt) { // Output the message to the console console.log(JSON.stringify(evt.data)); }
console printout:
15:50:53]Sideload successful![HOST][15:50:52]App Closed[HOST][15:50:53]App Started[HOST][15:50:53]Launch complete - durations: foregrounding(99ms), first paint(1ms), total(426ms).app/message.js:6,3[15:50:53]functionapp/message.js:9,3[15:50:53]messageapp/message.js:21,3[15:50:53]dataapp/index.js:10,1[15:50:53]sendcompanion/index.js:6,11[15:50:53]{"title":"My test data","isTest":true,"records":[1,2,3,4]}app/message.js:6,3[15:50:54]functionapp/message.js:6,3[15:50:54]functionapp/message.js:6,3[15:50:55]functionapp/message.js:6,3[15:50:55]function
thank you for your help!
Answered! Go to the Best Answer.
12-06-2018 13:56 - edited 12-06-2018 14:12
12-06-2018 13:56 - edited 12-06-2018 14:12
I think you should optimize your code.
For e.g. function sendMessage() inside function mess()...
I think it would be better not to have another file (message.js) which has be imported, why not have this code inside index.js so it doesn´t need to import? This would be more better.
But I think the Problem is the following:
mess() is called every 0,5 sek which sets every time the onopen-event.
The onopen-Event fires only when it gets open, so it has to be closed to open again for fire again.
It would be better to set an interval set inside the onopen-event.
Inside the onclose-event clear interval.
Inside the interval put your sendMessage()
I would do it like this way:
index.js (not tested)
import document from "document"; import * as messaging from "messaging"; const recBlue = document.getElementById("demoinstance"); let message_interval = ""; // Send a message to the peer function sendMessage() { console.log("data"); // Sample data var data = { title: 'My test data', isTest: true, records: [1, 2, 3, 4] } if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { // Send the data to peer as a message console.log("send"); messaging.peerSocket.send(data); } } messaging.peerSocket.onopen = function() { // Ready to send messages console.log("message"); message_interval = setInterval(function(){ sendMessage(); }, 500); } // Listen for the onerror event messaging.peerSocket.onerror = function(err) { // Handle any errors console.log("Connection error: " + err.code + " - " + err.message); } messaging.peerSocket.onclose = () => { clearInterval(message_interval); };
12-06-2018 13:56 - edited 12-06-2018 14:12
12-06-2018 13:56 - edited 12-06-2018 14:12
I think you should optimize your code.
For e.g. function sendMessage() inside function mess()...
I think it would be better not to have another file (message.js) which has be imported, why not have this code inside index.js so it doesn´t need to import? This would be more better.
But I think the Problem is the following:
mess() is called every 0,5 sek which sets every time the onopen-event.
The onopen-Event fires only when it gets open, so it has to be closed to open again for fire again.
It would be better to set an interval set inside the onopen-event.
Inside the onclose-event clear interval.
Inside the interval put your sendMessage()
I would do it like this way:
index.js (not tested)
import document from "document"; import * as messaging from "messaging"; const recBlue = document.getElementById("demoinstance"); let message_interval = ""; // Send a message to the peer function sendMessage() { console.log("data"); // Sample data var data = { title: 'My test data', isTest: true, records: [1, 2, 3, 4] } if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { // Send the data to peer as a message console.log("send"); messaging.peerSocket.send(data); } } messaging.peerSocket.onopen = function() { // Ready to send messages console.log("message"); message_interval = setInterval(function(){ sendMessage(); }, 500); } // Listen for the onerror event messaging.peerSocket.onerror = function(err) { // Handle any errors console.log("Connection error: " + err.code + " - " + err.message); } messaging.peerSocket.onclose = () => { clearInterval(message_interval); };