¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Websockets and frames


 

For all the drivers I've written I've had to do lots of http communications with various devices.? However, I've never had to get into the nitty gritty details of WebSockets and formatting strings into frames.? I've been banging my head against the wall with this for a few days so I figured it was time to ask if anyone has any code for sending and receiving strings through a WebSocket that they would be willing to share.? Of course I'm trying to do this with VS2008 so the code will run on a 3 series processor.

Just to tell you how desperate I am I've even asked ChatGPT to write the code but it hasn't come back with anything workable. Not a big surprise?

Thanks in advance for the help

Jay


 

Hi Jay, this is my code I use for websocket communication, maybe for inspiration. Jan
?
using System;
using System.Text;
using Crestron.SimplSharp;// For Basic SIMPL# Classes
using Crestron.SimplSharp.CrestronWebSocketClient;
using Newtonsoft.Json;

namespace WebSocket
{
? ? public delegate void SendDigitalAnalogData(ushort Data);
? ? // ? ?public delegate void ReceivedDataEventHandler(string data);
? ? public delegate void ConnectionStatusChangedEventHandler(bool isConnected);
? ? public delegate void SerialChangedEventHandler(SerialChangeEventArgs e); ? ? ? ? ? ?//event posilani dat do S+
? ? public class MyWebSocket
? ? {
? ? ? ? // ? ? ? ?public event ReceivedDataEventHandler OnDataReceived;
? ? ? ? // ? ? ? ?public event ReceivedDataEventHandler OnReceivedValueChange;
? ? ? ? public event ConnectionStatusChangedEventHandler OnConnectionStatusChanged;
? ? ? ? private WebSocketClient myWSC = new WebSocketClient();
? ? ? ? public WebSocketClient.WEBSOCKET_RESULT_CODES rCode;
? ? ? ? public WebSocketClient.WEBSOCKET_PACKET_TYPES pType;
? ? ? ? public byte[] sendData;
? ? ? ? public byte[] receiveData;
? ? ? ? public int connected_i;
? ? ? ? WebSocketClient.WEBSOCKET_RESULT_CODES error;
? ? ? ? private CTimer heartbeatTimer;
? ? ? ? public SendDigitalAnalogData UpdateSocketStatus { get; set; }
? ? ? ? // ? ? public ReceivedDataEventHandler OnDataReceived { get; set; }
? ? ? ? public MyWebSocket()
? ? ? ? {
? ? ? ? ? ? // Inicializujte ?asova? pro odes¨ªl¨¢n¨ª heartbeat zpr¨¢v ka?d?ch 5 sekund
? ? ? ? ? ? heartbeatTimer = new CTimer(SendHeartbeat, null, 0, 5000);
? ? ? ? }
? ? ? ? public void Connect(String Url)
? ? ? ? {
? ? ? ? ? ? myWSC.URL = Url;
? ? ? ? ? ? CrestronConsole.PrintLine(myWSC.URL);
? ? ? ? ? ? error = myWSC.Connect();
? ? ? ? ? ? if (error == WebSocketClient.WEBSOCKET_RESULT_CODES.WEBSOCKET_CLIENT_SUCCESS)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UpdateSocketStatus(1);
? ? ? ? ? ? ? ? connected_i = 1;
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket connected");
? ? ? ? ? ? ? ? StartListening();
? ? ? ? ? ? ? ? if (OnConnectionStatusChanged != null) OnConnectionStatusChanged(true);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket could not connect to server. Connect return code: " + error.ToString());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void DisconnectWebSocket()
? ? ? ? {
? ? ? ? ? ? myWSC.Disconnect();
? ? ? ? ? ? connected_i = 0;
? ? ? ? ? ? UpdateSocketStatus(0);
? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket disconnected. \r\n");
? ? ? ? ? ? if (OnConnectionStatusChanged != null) OnConnectionStatusChanged(false);
? ? ? ? }
? ? ? ? private void StartListening()
? ? ? ? {
? ? ? ? ? ? CrestronInvoke.BeginInvoke((Crestron.SimplSharp.CrestronSharpHelperDelegate)ListenForData);
? ? ? ? }
? ? ? ? private void ListenForData(object obj)
? ? ? ? {
? ? ? ? ? ? while (connected_i == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? myWSC.Receive(out receiveData, out pType);
? ? ? ? ? ? ? ? ? ? if (receiveData != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? string receivedString = Encoding.ASCII.GetString(receiveData, 0, receiveData.Length);
? ? ? ? ? ? ? ? ? ? ? ? // ? ? ? ? ?if (OnReceivedValueChange != null) OnReceivedValueChange(receivedString);
? ? ? ? ? ?// ? ? ? ? ? CrestronConsole.Print("Data received: " + receivedString + "\r\n");
? ? ? ? ? ? ? ? ? ? ? ? SignalChangeEvents.SerialValueChange(0, receivedString, 0); ? ? ? ? ? ? ? ? ? ? //udalost co posle prijata data do S+
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? CrestronConsole.Print("Error receiving data - Error Code:" + e + "\r\n");
? ? ? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void SingleSendAndReceive(String data)
? ? ? ? {
? // ? ? ? ?CrestronConsole.PrintLine("Sending data");
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sendData = Encoding.ASCII.GetBytes(data);
? ? ? ? ? ? ? ? rCode = myWSC.Send(sendData, (uint)sendData.Length, WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__TEXT_FRAME, WebSocketClient.WEBSOCKET_PACKET_SEGMENT_CONTROL.WEBSOCKET_CLIENT_PACKET_END);
? ? ? ? ? ? ? ? StartListening();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? UpdateSocketStatus(0);
? ? ? ? ? ? ? ? CrestronConsole.Print("WSClient Something went wrong - Error Code:" + e + "\r\n");
? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void ReconnectWebSocket()
? ? ? ? {
? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Attempting to reconnect...");
? ? ? ? ? ? while (connected_i == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Crestron.SimplSharp.CrestronEnvironment.Sleep(5000); // Wait 5 seconds before reconnecting
? ? ? ? ? ? ? ? Connect(myWSC.URL);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void SendHeartbeat(object obj)
? ? ? ? {
? ? ? ? ? ? if (connected_i == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string heartbeatMessage = "ping";
? ? ? ? ? ? ? ? sendData = Encoding.ASCII.GetBytes(heartbeatMessage);
? ? ? ? ? ? ? ? rCode = myWSC.Send(sendData, (uint)sendData.Length, WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__PING, WebSocketClient.WEBSOCKET_PACKET_SEGMENT_CONTROL.WEBSOCKET_CLIENT_PACKET_END);
? ? ? ? ? ? ? ? //CrestronConsole.PrintLine("rCode na ping = " + rCode);
? ? ? ? ? ? ? ? // Pokud ping nen¨ª ¨²sp¨§?n?, odpojte a znovu p?ipojte websocket
? ? ? ? ? ? ? ? if (rCode != WebSocketClient.WEBSOCKET_RESULT_CODES.WEBSOCKET_CLIENT_SUCCESS)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Ping failed, reconnecting...");
? ? ? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ?// ? ? ? ? ? CrestronConsole.PrintLine("WSClient Heartbeat sent: " + heartbeatMessage);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public class SerialChangeEventArgs : EventArgs
? ? {
? ? ? ? public short msgType { get; set; }
? ? ? ? public string stringToSplus { get; set; }
? ? ? ? public short shortToSplus { get; set; }
? ? ? ? public SerialChangeEventArgs()
? ? ? ? {
? ? ? ? }
? ? ? ? public SerialChangeEventArgs(short msgType, string stringToSplus, short shortToSplus)
? ? ? ? {
? ? ? ? ? ? this.msgType = msgType;
? ? ? ? ? ? this.stringToSplus = stringToSplus;
? ? ? ? ? ? this.shortToSplus = shortToSplus;
? ? ? ? }
? ? }
? ? public static class SignalChangeEvents
? ? {
? ? ? ? public static event SerialChangedEventHandler onSerialValueChange;
? ? ? ? public static void SerialValueChange(short msgType, string stringToSplus, short shortToSplus)
? ? ? ? {
? ? ? ? ? ? SignalChangeEvents.onSerialValueChange(new SerialChangeEventArgs(msgType, stringToSplus, shortToSplus));
? ? ? ? }
? ? }
}
?
?
?
/*
? SIMPL+ Module Information
? (Fill in comments below)
*/
/*
Dealer Name:
System Name:
System Number:
Programmer:
Comments:
*/
?
/*
? Compiler Directives
? (Uncomment and declare compiler directives as needed)
*/
// #ENABLE_DYNAMIC
// #SYMBOL_NAME ""
// #HINT ""
// #DEFINE_CONSTANT
// #CATEGORY ""?
// #PRINT_TO_TRACE
// #DIGITAL_EXPAND?
// #ANALOG_SERIAL_EXPAND?
// #OUTPUT_SHIFT?
// #HELP_PDF_FILE ""
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ENABLE_TRACE
// #ENCODING_ASCII
// #ENCODING_UTF16
// #ENCODING_INHERIT_FROM_PARENT
// #ENCODING_INHERIT_FROM_PROGRAM
?
#HELP_BEGIN
p?¨ªklad url:
ws://192.168.22.126:80/rpc
ws://192.168.0.5:80/ws
?
?
?
#HELP_END
?
?
/*
? Include Libraries
? (Uncomment and include additional libraries as needed)
*/
//#INCLUDEPATH "C:\Crestron\Simpl\usrSIMPL#\WSClient\WSClient\bin\debug"
//#INCLUDEPATH "D:\Crestron\_Projekty\_testiky\WebSocketTest_v01\SIMPLSharp_WebSocketClient_Basic-master\WSClient\bin\Debug"
#INCLUDEPATH "D:\Crestron\_Projekty\_testiky\WebSocketTest_v01\SIMPLSharp_WebSocketClient_Basic-master\WSClient\bin\Release"
?
#USER_SIMPLSHARP_LIBRARY "WSClient"?
?
/*
? DIGITAL, ANALOG and SERIAL INPUTS and OUTPUTS
? (Uncomment and declare inputs and outputs as needed)
*/
DIGITAL_INPUT Connect,Disconnect;?
// ANALOG_INPUT?
STRING_INPUT Url$[256], Message$[65534];?
// BUFFER_INPUT?
?
DIGITAL_OUTPUT ConnectedFb;?
// ANALOG_OUTPUT?
STRING_OUTPUT receivedData$;
?
/*
? SOCKETS
? (Uncomment and define socket definitions as needed)
*/
// TCP_CLIENT
// TCP_SERVER
// UDP_SOCKET
?
?
/*
? Parameters
? (Uncomment and declare parameters as needed)
*/
// INTEGER_PARAMETER
// SIGNED_INTEGER_PARAMETER
// LONG_INTEGER_PARAMETER
// SIGNED_LONG_INTEGER_PARAMETER
// STRING_PARAMETER
?
/*
? Parameter Properties
? (Uncomment and declare parameter properties as needed)
*/
/*
#BEGIN_PARAMETER_PROPERTIES parameter_variable, parameter_variable, ...
? ?// propValidUnits = // unitString or unitDecimal|unitHex|unitPercent|unitCharacter|unitTime|unitTicks;
? ?// propDefaultUnit = // unitString, unitDecimal, unitHex, unitPercent, unitCharacter, unitTime or unitTicks;
? ?// propBounds = lower_bound , upper_bound;
? ?// propDefaultValue = ;? // or, propDefaultValue = "";
? ?// propList = // { "value" , "label" } , { "value" , "label" } , ... ;
? ?// propShortDescription = "status_bar_hint_text";
? ?// #BEGIN_PROP_FULL_DESCRIPTION? line_1...? line_2...? line_n? #END_PROP_FULL_DESCRIPTION
? ?// #BEGIN_PROP_NOTES line_1...? line_2...? line_n? #END_PROP_NOTES
#END_PARAMETER_PROPERTIES
*/
?
/*
? Structure Definitions
? (Uncomment and define structure definitions as needed)
? Note:? Be sure to initialize all declared STRING variables as needed
? ? ? ? ?For example, in Function Main: struct.myString = "";
*/
/*
STRUCTURE MyStruct1
{
};
?
MyStruct1 struct;
*/
?
/*
? Global Variables
? (Uncomment and declare global variables as needed)
? Note:? Be sure to initialize all declared STRING variables as needed
? ? ? ? ?For example, in Function Main: myString = "";
*/
// INTEGER
// LONG_INTEGER
// SIGNED_INTEGER
// SIGNED_LONG_INTEGER
// STRING
MyWebSocket ws;
/*
? Functions
? (Add any additional functions here)
? Note:? Functions must be physically placed before the location in
? ? ? ? ?the code that calls them.
*/
/*
Function MyFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
}
*/
?
/*
Integer_Function MyIntFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
?
? ? Return (0);
}
*/
?
/*
String_Function MyStrFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
?
? ? Return ("");
}
*/
?
/*
? Event Handlers
? (Uncomment and declare additional event handlers as needed)
*/
? ??
PUSH Connect
{
ws.Connect(Url$);
}
?
PUSH Disconnect?
{
? ? ws.DisconnectWebSocket();
}
?
CHANGE Message$
{
? ?ws.SingleSendAndReceive(Message$);?
}
??
Callback Function SocketStatusChange(integer Status)
{
ConnectedFb = Status;
}
EVENTHANDLER _onSerialValueChange(SerialChangeEventArgs e)
{
switch(e.msgType)
{
? ? case(0): { receivedData$ = e.stringToSplus; } //received serial data?
/* case(1): //mainPwr (v promenne e.shortToSplus)
{?
if(e.shortToSplus = 0) { mainPwrIsOn_fb = 0; mainPwrIsOff_fb = 1; }?
else { mainPwrIsOn_fb = 1; mainPwrIsOff_fb = 0; }
}
case(2): { mainBrightness_fb = e.shortToSplus; } //mainBrightness (v promenne e.shortToSplus)
case(3): { mainTransition_fb = e.shortToSplus; } //mainTransition (v promenne e.shortToSplus)
case(4): { mainBlend_fb = e.shortToSplus; } //mainBlend (bs) (v promenne e.shortToSplus)
case(5): { mainPreset_fb = e.shortToSplus; } //mainPreset (v promenne e.shortToSplus)
*/
}
}
? ?
?
?
?
/*
? Main()
? Uncomment and place one-time startup code here
? (This code will get called when the system starts up)
*/
?
Function Main()
{
WaitForInitializationComplete();
? RegisterDelegate(ws, UpdateSocketStatus, SocketStatusChange);
? ? RegisterEvent(SignalChangeEvents, onSerialValueChange, _onSerialValueChange);
}
?
?


 

¿ªÔÆÌåÓý

Thanks Jan!

On 5/14/2025 8:34 AM, Jan Hy?ha via groups.io wrote:

Hi Jay, this is my code I use for websocket communication, maybe for inspiration. Jan
?
using System;
using System.Text;
using Crestron.SimplSharp;// For Basic SIMPL# Classes
using Crestron.SimplSharp.CrestronWebSocketClient;
using Newtonsoft.Json;

namespace WebSocket
{
? ? public delegate void SendDigitalAnalogData(ushort Data);
? ? // ? ?public delegate void ReceivedDataEventHandler(string data);
? ? public delegate void ConnectionStatusChangedEventHandler(bool isConnected);
? ? public delegate void SerialChangedEventHandler(SerialChangeEventArgs e); ? ? ? ? ? ?//event posilani dat do S+
? ? public class MyWebSocket
? ? {
? ? ? ? // ? ? ? ?public event ReceivedDataEventHandler OnDataReceived;
? ? ? ? // ? ? ? ?public event ReceivedDataEventHandler OnReceivedValueChange;
? ? ? ? public event ConnectionStatusChangedEventHandler OnConnectionStatusChanged;
? ? ? ? private WebSocketClient myWSC = new WebSocketClient();
? ? ? ? public WebSocketClient.WEBSOCKET_RESULT_CODES rCode;
? ? ? ? public WebSocketClient.WEBSOCKET_PACKET_TYPES pType;
? ? ? ? public byte[] sendData;
? ? ? ? public byte[] receiveData;
? ? ? ? public int connected_i;
? ? ? ? WebSocketClient.WEBSOCKET_RESULT_CODES error;
? ? ? ? private CTimer heartbeatTimer;
? ? ? ? public SendDigitalAnalogData UpdateSocketStatus { get; set; }
? ? ? ? // ? ? public ReceivedDataEventHandler OnDataReceived { get; set; }
? ? ? ? public MyWebSocket()
? ? ? ? {
? ? ? ? ? ? // Inicializujte ?asova? pro odes¨ªl¨¢n¨ª heartbeat zpr¨¢v ka?d?ch 5 sekund
? ? ? ? ? ? heartbeatTimer = new CTimer(SendHeartbeat, null, 0, 5000);
? ? ? ? }
? ? ? ? public void Connect(String Url)
? ? ? ? {
? ? ? ? ? ? myWSC.URL = Url;
? ? ? ? ? ? CrestronConsole.PrintLine(myWSC.URL);
? ? ? ? ? ? error = myWSC.Connect();
? ? ? ? ? ? if (error == WebSocketClient.WEBSOCKET_RESULT_CODES.WEBSOCKET_CLIENT_SUCCESS)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UpdateSocketStatus(1);
? ? ? ? ? ? ? ? connected_i = 1;
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket connected");
? ? ? ? ? ? ? ? StartListening();
? ? ? ? ? ? ? ? if (OnConnectionStatusChanged != null) OnConnectionStatusChanged(true);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket could not connect to server. Connect return code: " + error.ToString());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void DisconnectWebSocket()
? ? ? ? {
? ? ? ? ? ? myWSC.Disconnect();
? ? ? ? ? ? connected_i = 0;
? ? ? ? ? ? UpdateSocketStatus(0);
? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Websocket disconnected. \r\n");
? ? ? ? ? ? if (OnConnectionStatusChanged != null) OnConnectionStatusChanged(false);
? ? ? ? }
? ? ? ? private void StartListening()
? ? ? ? {
? ? ? ? ? ? CrestronInvoke.BeginInvoke((Crestron.SimplSharp.CrestronSharpHelperDelegate)ListenForData);
? ? ? ? }
? ? ? ? private void ListenForData(object obj)
? ? ? ? {
? ? ? ? ? ? while (connected_i == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? myWSC.Receive(out receiveData, out pType);
? ? ? ? ? ? ? ? ? ? if (receiveData != null)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? string receivedString = Encoding.ASCII.GetString(receiveData, 0, receiveData.Length);
? ? ? ? ? ? ? ? ? ? ? ? // ? ? ? ? ?if (OnReceivedValueChange != null) OnReceivedValueChange(receivedString);
? ? ? ? ? ?// ? ? ? ? ? CrestronConsole.Print("Data received: " + receivedString + "\r\n");
? ? ? ? ? ? ? ? ? ? ? ? SignalChangeEvents.SerialValueChange(0, receivedString, 0); ? ? ? ? ? ? ? ? ? ? //udalost co posle prijata data do S+
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? CrestronConsole.Print("Error receiving data - Error Code:" + e + "\r\n");
? ? ? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void SingleSendAndReceive(String data)
? ? ? ? {
? // ? ? ? ?CrestronConsole.PrintLine("Sending data");
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sendData = Encoding.ASCII.GetBytes(data);
? ? ? ? ? ? ? ? rCode = myWSC.Send(sendData, (uint)sendData.Length, WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__TEXT_FRAME, WebSocketClient.WEBSOCKET_PACKET_SEGMENT_CONTROL.WEBSOCKET_CLIENT_PACKET_END);
? ? ? ? ? ? ? ? StartListening();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? UpdateSocketStatus(0);
? ? ? ? ? ? ? ? CrestronConsole.Print("WSClient Something went wrong - Error Code:" + e + "\r\n");
? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void ReconnectWebSocket()
? ? ? ? {
? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Attempting to reconnect...");
? ? ? ? ? ? while (connected_i == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Crestron.SimplSharp.CrestronEnvironment.Sleep(5000); // Wait 5 seconds before reconnecting
? ? ? ? ? ? ? ? Connect(myWSC.URL);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void SendHeartbeat(object obj)
? ? ? ? {
? ? ? ? ? ? if (connected_i == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string heartbeatMessage = "ping";
? ? ? ? ? ? ? ? sendData = Encoding.ASCII.GetBytes(heartbeatMessage);
? ? ? ? ? ? ? ? rCode = myWSC.Send(sendData, (uint)sendData.Length, WebSocketClient.WEBSOCKET_PACKET_TYPES.LWS_WS_OPCODE_07__PING, WebSocketClient.WEBSOCKET_PACKET_SEGMENT_CONTROL.WEBSOCKET_CLIENT_PACKET_END);
? ? ? ? ? ? ? ? //CrestronConsole.PrintLine("rCode na ping = " + rCode);
? ? ? ? ? ? ? ? // Pokud ping nen¨ª ¨²sp¨§?n?, odpojte a znovu p?ipojte websocket
? ? ? ? ? ? ? ? if (rCode != WebSocketClient.WEBSOCKET_RESULT_CODES.WEBSOCKET_CLIENT_SUCCESS)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? CrestronConsole.PrintLine("WSClient Ping failed, reconnecting...");
? ? ? ? ? ? ? ? ? ? DisconnectWebSocket();
? ? ? ? ? ? ? ? ? ? ReconnectWebSocket();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ?// ? ? ? ? ? CrestronConsole.PrintLine("WSClient Heartbeat sent: " + heartbeatMessage);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public class SerialChangeEventArgs : EventArgs
? ? {
? ? ? ? public short msgType { get; set; }
? ? ? ? public string stringToSplus { get; set; }
? ? ? ? public short shortToSplus { get; set; }
? ? ? ? public SerialChangeEventArgs()
? ? ? ? {
? ? ? ? }
? ? ? ? public SerialChangeEventArgs(short msgType, string stringToSplus, short shortToSplus)
? ? ? ? {
? ? ? ? ? ? this.msgType = msgType;
? ? ? ? ? ? this.stringToSplus = stringToSplus;
? ? ? ? ? ? this.shortToSplus = shortToSplus;
? ? ? ? }
? ? }
? ? public static class SignalChangeEvents
? ? {
? ? ? ? public static event SerialChangedEventHandler onSerialValueChange;
? ? ? ? public static void SerialValueChange(short msgType, string stringToSplus, short shortToSplus)
? ? ? ? {
? ? ? ? ? ? SignalChangeEvents.onSerialValueChange(new SerialChangeEventArgs(msgType, stringToSplus, shortToSplus));
? ? ? ? }
? ? }
}
?
?
?
/*
? SIMPL+ Module Information
? (Fill in comments below)
*/
/*
Dealer Name:
System Name:
System Number:
Programmer:
Comments:
*/
?
/*
? Compiler Directives
? (Uncomment and declare compiler directives as needed)
*/
// #ENABLE_DYNAMIC
// #SYMBOL_NAME ""
// #HINT ""
// #DEFINE_CONSTANT
// #CATEGORY ""?
// #PRINT_TO_TRACE
// #DIGITAL_EXPAND?
// #ANALOG_SERIAL_EXPAND?
// #OUTPUT_SHIFT?
// #HELP_PDF_FILE ""
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ENABLE_TRACE
// #ENCODING_ASCII
// #ENCODING_UTF16
// #ENCODING_INHERIT_FROM_PARENT
// #ENCODING_INHERIT_FROM_PROGRAM
?
#HELP_BEGIN
p?¨ªklad url:
?
?
?
#HELP_END
?
?
/*
? Include Libraries
? (Uncomment and include additional libraries as needed)
*/
//#INCLUDEPATH "C:\Crestron\Simpl\usrSIMPL#\WSClient\WSClient\bin\debug"
//#INCLUDEPATH "D:\Crestron\_Projekty\_testiky\WebSocketTest_v01\SIMPLSharp_WebSocketClient_Basic-master\WSClient\bin\Debug"
#INCLUDEPATH "D:\Crestron\_Projekty\_testiky\WebSocketTest_v01\SIMPLSharp_WebSocketClient_Basic-master\WSClient\bin\Release"
?
#USER_SIMPLSHARP_LIBRARY "WSClient"?
?
/*
? DIGITAL, ANALOG and SERIAL INPUTS and OUTPUTS
? (Uncomment and declare inputs and outputs as needed)
*/
DIGITAL_INPUT Connect,Disconnect;?
// ANALOG_INPUT?
STRING_INPUT Url$[256], Message$[65534];?
// BUFFER_INPUT?
?
DIGITAL_OUTPUT ConnectedFb;?
// ANALOG_OUTPUT?
STRING_OUTPUT receivedData$;
?
/*
? SOCKETS
? (Uncomment and define socket definitions as needed)
*/
// TCP_CLIENT
// TCP_SERVER
// UDP_SOCKET
?
?
/*
? Parameters
? (Uncomment and declare parameters as needed)
*/
// INTEGER_PARAMETER
// SIGNED_INTEGER_PARAMETER
// LONG_INTEGER_PARAMETER
// SIGNED_LONG_INTEGER_PARAMETER
// STRING_PARAMETER
?
/*
? Parameter Properties
? (Uncomment and declare parameter properties as needed)
*/
/*
#BEGIN_PARAMETER_PROPERTIES parameter_variable, parameter_variable, ...
? ?// propValidUnits = // unitString or unitDecimal|unitHex|unitPercent|unitCharacter|unitTime|unitTicks;
? ?// propDefaultUnit = // unitString, unitDecimal, unitHex, unitPercent, unitCharacter, unitTime or unitTicks;
? ?// propBounds = lower_bound , upper_bound;
? ?// propDefaultValue = ;? // or, propDefaultValue = "";
? ?// propList = // { "value" , "label" } , { "value" , "label" } , ... ;
? ?// propShortDescription = "status_bar_hint_text";
? ?// #BEGIN_PROP_FULL_DESCRIPTION? line_1...? line_2...? line_n? #END_PROP_FULL_DESCRIPTION
? ?// #BEGIN_PROP_NOTES line_1...? line_2...? line_n? #END_PROP_NOTES
#END_PARAMETER_PROPERTIES
*/
?
/*
? Structure Definitions
? (Uncomment and define structure definitions as needed)
? Note:? Be sure to initialize all declared STRING variables as needed
? ? ? ? ?For example, in Function Main: struct.myString = "";
*/
/*
STRUCTURE MyStruct1
{
};
?
MyStruct1 struct;
*/
?
/*
? Global Variables
? (Uncomment and declare global variables as needed)
? Note:? Be sure to initialize all declared STRING variables as needed
? ? ? ? ?For example, in Function Main: myString = "";
*/
// INTEGER
// LONG_INTEGER
// SIGNED_INTEGER
// SIGNED_LONG_INTEGER
// STRING
MyWebSocket ws;
/*
? Functions
? (Add any additional functions here)
? Note:? Functions must be physically placed before the location in
? ? ? ? ?the code that calls them.
*/
/*
Function MyFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
}
*/
?
/*
Integer_Function MyIntFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
?
? ? Return (0);
}
*/
?
/*
String_Function MyStrFunction1()
{
? ? // TODO:? Add local variable declarations here
?
? ? // TODO:? Add code here
?
? ? Return ("");
}
*/
?
/*
? Event Handlers
? (Uncomment and declare additional event handlers as needed)
*/
? ??
PUSH Connect
{
ws.Connect(Url$);
}
?
PUSH Disconnect?
{
? ? ws.DisconnectWebSocket();
}
?
CHANGE Message$
{
? ?ws.SingleSendAndReceive(Message$);?
}
??
Callback Function SocketStatusChange(integer Status)
{
ConnectedFb = Status;
}
EVENTHANDLER _onSerialValueChange(SerialChangeEventArgs e)
{
switch(e.msgType)
{
? ? case(0): { receivedData$ = e.stringToSplus; } //received serial data?
/* case(1): //mainPwr (v promenne e.shortToSplus)
{?
if(e.shortToSplus = 0) { mainPwrIsOn_fb = 0; mainPwrIsOff_fb = 1; }?
else { mainPwrIsOn_fb = 1; mainPwrIsOff_fb = 0; }
}
case(2): { mainBrightness_fb = e.shortToSplus; } //mainBrightness (v promenne e.shortToSplus)
case(3): { mainTransition_fb = e.shortToSplus; } //mainTransition (v promenne e.shortToSplus)
case(4): { mainBlend_fb = e.shortToSplus; } //mainBlend (bs) (v promenne e.shortToSplus)
case(5): { mainPreset_fb = e.shortToSplus; } //mainPreset (v promenne e.shortToSplus)
*/
}
}
? ?
?
?
?
/*
? Main()
? Uncomment and place one-time startup code here
? (This code will get called when the system starts up)
*/
?
Function Main()
{
WaitForInitializationComplete();
? RegisterDelegate(ws, UpdateSocketStatus, SocketStatusChange);
? ? RegisterEvent(SignalChangeEvents, onSerialValueChange, _onSerialValueChange);
}
?
?