Chameleon WebSocket Server

As an alternative to using Chameleon.api which uses WCF, Chameleon WebSocket Server uses WebSockets to send commands to Chameleon.api. It receives json commands, formats them for Chameleon.api and communicates with Chameleon.api.

 

image-20240704-180902.png

Before starting the WebSocket server, add the ip address and port for the WebSocket. Also, add the Chameleon.api ip and port in order to talk to Chameleon.api.

Commands sent to the Chameleon WebSocket should use the Chameleon WebSocket schema.

Commands are strictly defined by the schema. They look like:

{
"command":"on",
"project":"hello world",
"scene":"hello world",
"tags":[
{"k":"^helloworld","v":"Hello World!","t":"text", “visible”:true}
]
}

The valid commands are:

  • on

  • update

  • off

A project and scene must be specified followed by an array of tags. Each tag has properties:

  • k - key

  • v - value

  • t - type

  • visible - Boolean

A simple WebSocket Client (using c# and .net) might look like:

public async Task ConnectAsync(Uri serverUri) { using (ClientWebSocket client = new ClientWebSocket()) { try { await client.ConnectAsync(serverUri, CancellationToken.None); ArraySegment<byte> bytesToSend = new ArraySegment<byte>(Encoding.UTF8.GetBytes(textBoxCommand.Text)); await client.SendAsync(bytesToSend, WebSocketMessageType.Text, true, CancellationToken.None); ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[1024]); WebSocketReceiveResult result = await client.ReceiveAsync(bytesReceived, CancellationToken.None); string receivedMessage = Encoding.UTF8.GetString(bytesReceived.Array, 0, result.Count); Console.WriteLine("Received: " + receivedMessage); await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Goodbye", CancellationToken.None); } catch (WebSocketException ex) { Console.WriteLine("WebSocket error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } private async void buttonSend_Click(object sender, EventArgs e) { Uri serverUri = new Uri(Globals.url); await ConnectAsync(serverUri); }