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

WebSocket is not defined

ANSWERED

I am new to this and unable to understand why this happening - how do I resolve this? Please help

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I just tested this on Android 9.

 

// companion/index.js

const wsUri = "wss://echo.websocket.org/";
let websocket;

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

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(message);
}

testWebSocket();

Which produces:

[11:22:40 AM] Companion: Companion launched by [launchedOnTracker]
[11:22:41 AM] Companion: CONNECTED                                                                                                                        (companion/index.js:9,3)
[11:22:41 AM] Companion: SEND: WebSocket rocks                                                                                                           (companion/index.js:23,3)
[11:22:41 AM] Companion: MESSAGE: WebSocket rocks                                                                                                        (companion/index.js:14,3)
[11:22:41 AM] Companion: DISCONNECTED                                                                                                                    (companion/index.js:14,3)
[11:22:42 AM] Companion: Companion unloaded

View best answer in original post

Best Answer
0 Votes
9 REPLIES 9

Are you getting this error in your /companion code?

Which mobile phone OS/version are you using?

Best Answer
0 Votes

Ipod, IOS

Best Answer
0 Votes

Which version of iOS?

Best Answer
0 Votes

Can you also post the code you're using too? Thanks

Best Answer
0 Votes

I am using below code in my companion app js and ios ver 10.3.3, android 9

const socket = new WebSocket('ws://localhost:8080');

socket.addEventListener('open', function (event) {
socket.send('Hi localserver!');
});

socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
});

Best Answer
0 Votes

I just tested this on Android 9.

 

// companion/index.js

const wsUri = "wss://echo.websocket.org/";
let websocket;

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

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(message);
}

testWebSocket();

Which produces:

[11:22:40 AM] Companion: Companion launched by [launchedOnTracker]
[11:22:41 AM] Companion: CONNECTED                                                                                                                        (companion/index.js:9,3)
[11:22:41 AM] Companion: SEND: WebSocket rocks                                                                                                           (companion/index.js:23,3)
[11:22:41 AM] Companion: MESSAGE: WebSocket rocks                                                                                                        (companion/index.js:14,3)
[11:22:41 AM] Companion: DISCONNECTED                                                                                                                    (companion/index.js:14,3)
[11:22:42 AM] Companion: Companion unloaded
Best Answer
0 Votes

Thanks, works fine, but when I use my URL "ws://127.0.0.1:9000/" then i am getting this error "WebSocket connection to 'ws://127.0.0.1:9000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED"

Best Answer
0 Votes

Connection refused sounds like your service isn't running on that port. Or, are you running that service on a server? If so, you'll need to change 127.0.0.1 to the IP address of the server.

Best Answer
0 Votes

Just based on the comments, I read that only secure connections are support by device.

Therefore,  wss:// protocol has to be declaired and not ws://.

Best Answer
0 Votes