02-16-2019 09:50
02-16-2019 09:50
Hi!
I'm currently developing an application for managing my to do list and I've got some issue with the waking of the companion when I launch the app. Every time I'm wait a few hours I've the obligation to launch the fitbit app and synchronise the ionic. After that I can launch again my app and make working the communication between app and companion. It's seems not fire the peerSocket.onopen 😕
I've similary issue with another app I've made. Thank's in advance for your help!
// Listen for the onopen event
messaging.peerSocket.onopen = function() {
console.log('1 - messaging on open');
// ask for the list of projects
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
// Send the data to peer as a message
console.log("2 - send message for waking up companion")
messaging.peerSocket.send("getProjects")
}
}
Best Answer02-22-2019 04:20
02-22-2019 04:20
You should do it reversely.
The onopen event fires just once, and at the moment you are setting it, you don't know if it didn't fire already without you having a listener set on it.
So a surefire way to send the message if the socket is open is such:
function sendMessage(msg) {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
messaging.peerSocket.send(msg)
} else {
messaging.peerSocket.onopen = () => {
messaging.peerSocket.send(msg)
}
}
}
Best Answer