SmartConnector Forum
Schneider Electric support forum about SmartConnector applications for integration of other building management systems (BMS) into EcoStruxure Building Operation.
Link copied. Please paste this link to share this article on your social media post.
Hi,
I did a websocket search in the forum but nothing came up.
I need to listen to the websocket broadcasting from an API. Erwin-Vd-Zwart had helped before and sent me the code sample I wrote below. It is very useful for my standard updates. I have no problem with it. But I need an uninterrupted use to listen to websocket. If it is in the loop, the socket will be closed and opened. I'll paste my code below. Is there anyone who can help with this?
Erwins Code;
#region Execute_Subclass - Override
protected override IEnumerable<Prompt> Execute_Subclass()
{
for (;;)
{
CheckCancellationToken();
// Perform here your class to do the actual request
Task.Delay(FeedbackInterval * 1000).Wait();
}
}
#endregion
}
My Code;
#region Execute_Subclass - Override
protected override IEnumerable<Prompt> Execute_Subclass()
{
for(; ; )
{
using (WebSocket ws = new WebSocket("ws://192.168.1.106:5555/event"))
{
ws.Connect();
ws.OnMessage += (sender, e) =>
{
DataAdapter.ModifyValueItemValue("deviceName_1003", "success", EwsValueStateEnum.Good); //for testing, not important
};
}
//Task.Delay(5000).Wait();
}
}
#endregion
Link copied. Please paste this link to share this article on your social media post.
Hi,
You can do that by adding a task like "connect" and then execute a connect.wait(), the for(;;) loop will now wait until the task 'connect' is completed, inside the task connect i added a loop to listen to the websocket messages and this will happen until the connection is lost, in this scenario the task never completes and keeps looping anmd listening to the messages until the connection is lost, then the task completes and loop will be executed again initiating a new connect etcetera..
See this sample where all is working as you want.
BR,
Erwin
Link copied. Please paste this link to share this article on your social media post.
Hi,
You can do that by adding a task like "connect" and then execute a connect.wait(), the for(;;) loop will now wait until the task 'connect' is completed, inside the task connect i added a loop to listen to the websocket messages and this will happen until the connection is lost, in this scenario the task never completes and keeps looping anmd listening to the messages until the connection is lost, then the task completes and loop will be executed again initiating a new connect etcetera..
See this sample where all is working as you want.
BR,
Erwin
Link copied. Please paste this link to share this article on your social media post.
Link copied. Please paste this link to share this article on your social media post.
On line 84 you have the string 'content' and that might be converted to JSON (i assume your WS is publishing JSON) and from there you can execute actions to write to the EWS server.
Please test intensive as the code is still rudimentery and not used in production yet.
BR,
Erwin
Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.