Unity VR WebSocket Connection to ThingsBoard Error 'Failed to parse websocket command!'
Hello,
I apologize in advance if there is another post for this (I could not find one) but may I please have some assistance. I am working on a VR program in Unity and I need to connect it to ThingsBoard via a WebSocket/REST API. Currently, I have a WebSocket working via Microsoft’s NuGet plugin websocket-sharp. The WebSocket works as in we can connect to ThingsBoard and this had been verified in both the ThingsBoard terminal (running in a Ubuntu VM terminal) and through Postman on Windows. The goal of this Project is when Unity is running, it connects to ThingsBoard while ThingsBoard is pulling data from IoT sensors then Unity will pull in a copy of the data from ThingsBoard via the WebSocket and displaying it as text inside the program in real-time.
The issue, however, is when we try and pull ALL telemetry data from Thingsboard while running it is providing a message in Unity saying ‘WebSocket command cannot be parsed’. It provides it as a error JSON object.
Now I have read ThingsBoard’s post on working with telemetry data (link below) and they state this specific code example will allow the connection via a WebSocket:
windowtext;mso-ansi-language:EN-US">‘ color:#212529">ws padding:0cm">(s none windowtext 0cm;padding:0cm">)://host:port/api/ws/plugins/telemetry?token padding:0cm">= border:none windowtext 1.0pt;mso-border-alt:none windowtext 0cm;padding:0cm">$JWT_TOKEN’ border:none windowtext 1.0pt;mso-border-alt:none windowtext 0cm;padding:0cm"> Now, they do not explain if this code example will allow a subscription to ALL telemetry updates or if one will need to specify a deviceType and deviceID to do it.
They do give a .HTML example and two links on code written in JAVA about the subscription, but it is confusing.
So, I am not sure if the possible issues are how I am calling the WebSocket, do I need to specify the specific device or can I call the entire DashBoard data I am not sure.
Now to be totally honest this is the first time I have ever touched WebSockets and REST so I am not knowledgeable and with saying that I cannot help but feel the solution to this is elementary, but I cannot see it.
Any assistance will be deeply appreciated.
Thank you.
Error, Code and links below:
Error:
Code:
CSharp]using UnityEngine;
using System.Collections;
using WebSocketSharp;
using System.Collections.Generic;
using UnityEngine.Networking;
using System.Web;
public class WebSocketController : MonoBehaviour
{
private WebSocket _webSocket;
// Use this for initialization
void Start()
{
string _jwt = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZW5hbnRAdGhpbmdzYm9hcmQub3JnIiwic2NvcGVzIjpbIlRFTkFOVF9BRE1JTiJdLCJ1c2VySWQiOiIxNjYyOTI4MC1mMGJmLTExZWEtYTdmZS1mOTEyNDkxODEyMTAiLCJlbmFibGVkIjp0cnVlLCJpc1B1YmxpYyI6ZmFsc2UsInRlbmFudElkIjoiMTYzMWU1OTAtZjBiZi0xMWVhLWE3ZmUtZjkxMjQ5MTgxMjEwIiwiY3VzdG9tZXJJZCI6IjEzODE0MDAwLTFkZDItMTFiMi04MDgwLTgwODA4MDgwODA4MCIsImlzcyI6InRoaW5nc2JvYXJkLmlvIiwiaWF0IjoxNjQ5ODE0NDg1LCJleHAiOjE2NDk4MjM0ODV9.T8oGpjUc3XC9f0A8WZewrJe6IpeJwqoObYVmpM2NR3BpCFprM-e8Kwx38LaKEBdb-CPwCo60fFTPoOkgzLAXlA";
_webSocket = new WebSocket("ws://localhost:8080/api/ws/plugins/telemetry?token=" + _jwt);
// Starts connection to the server.
Debug.Log("Is _webSocket alive BEFORE .Connect()? Result: " + _webSocket.IsAlive);
_webSocket.Connect();
Debug.Log("Is _webSocket alive AFTER .Connect()? Result: " + _webSocket.IsAlive);
_webSocket.OnOpen += (sender, e) =>
{
Debug.Log("OnOpen()");
};
// Event listens for all incoming data when Websocket is active.
_webSocket.OnMessage += (sender, e) =>
{
Debug.Log(e.Data);
Debug.Log("Message recieved from " + ((WebSocket)sender).Url + " , \nData : " + e.Data);
};
// Prints error log message if/when error is encountered.
_webSocket.OnError += (sender, e) =>
{
Debug.Log("OnError()");
Debug.Log("WEBSOCKET ERROR ENCOUNTERED! " + ((WebSocket)sender).Url + " , ERROR : " + e.Message.ToString() + "\n" + e.Exception.ToString());
};
}
// Update is called once per frame
void Update()
{
if (_webSocket == null)
{
return;
}
// Here for debugging.
if(Input.GetKeyDown(KeyCode.Space)){
_webSocket.Send("HELLO THERE! GENERAL KENOBI");
}
}
}
Links:
WebSocket Sharp: https://www.nuget.org/packages/WebSocketSharp
ThignsBoard Working with Telemetry: https://thingsboard.io/docs/pe/user-guide/telemetry/
ThingsBoard JWT: https://thingsboard.io/docs/pe/reference/rest-api/#rest-api-auth
Answer by myCookieDough99 · Apr 15 at 01:09 AM
Post Script, I am not sure why me normal text is unformatted (no spacing) in the post field it was correct.