Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

wecsocket trouble using messaging API

I made companion using messaging API. 

I made server using node.JS

And I made a connection between fitbit simulator and server and HTML.
It worked well. 
With using fitbit simulator, there was no problem. 
But when using fitbit VERSA device, I can't connect with my server.
When I make a connection with URL : echo server. 
But when I use my localhost address, It doesn't work. 
I gave permissions of internet to Fitbit already. 


 

import * as messaging from "messaging";

messaging.peerSocket.onopen = () => {
  console.log("Ready");
  sendMessage();
}

messaging.peerSocket.onerror = (err) => {
  console.log(`Connection error: ${err.code} - ${err.message}`);
}

messaging.peerSocket.onmessage = (evt) => {
  console.log(JSON.stringify(evt.data));
}

function sendMessage() {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    // Send the data to peer as a message
    messaging.peerSocket.send({
      sampleData: 123456
    });
  }
}

import { me } from "companion";

if (!me.permissions.granted("access_internet")) {
  console.log("We're not allowed to access the internet!");
}

const wsUri = "ws://localhost:40510/";

let websocket;

function testWebSocket() {
  websocket = new WebSocket(wsUri);
  websocket.addEventListener("open", onOpen);
  websocket.addEventListener("close", onClose);
  websocket.addEventListener("message", onMessage);
  websocket.addEventListener("error", onError);
  websocket.addEventListener("send", doSend);
}

function onOpen(evt) {
  console.log("CONNECTED");
  doSend("WebSocket rocks");
}

function onClose(evt) {
  console.log("DISCONNECTED");
}

function onMessage(evt) {
  console.log(`MESSAGE: ${evt.data}`);
 // websocket.close();
}

 function onError(evt) {
  console.error(`ERROR: ${evt.data}`);
}

function doSend(message) {
  console.log(`SEND: ${message}`);
  websocket.send("sent");
}

testWebSocket();
Best Answer
0 Votes
2 REPLIES 2

It's possible that the physical devices prefer https.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

You mean that I can't use localhost URL???
I tried to chage my URL  from localhost:portnumber to IP address:portnumber. But it didn't work.
My laptop and fitbit are in same wifi network. 
I blowed ping from laptop IPaddress to fitbit device IPaddress. It works.

I don't know why it doesn't work. 

Best Answer
0 Votes