You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
[WebSocketRoute("/ws")]publicclassMyWebSocket:WebSocketConnection{privatestaticDictionary<Guid,MyWebSocket>connections=newDictionary<Guid,MyWebSocket>();publicGuidUniqueId{get;}publicMyWebSocket(){UniqueId=Guid.NewGuid();}publicoverridevoidOnOpen(){connections.Add(UniqueId,this);System.Diagnostics.Debug.WriteLine("New websocket connected {0}, now have {1} in list",UniqueId,connections.Count);}publicoverridevoidOnClose(WebSocketCloseStatus?closeStatus,stringcloseStatusDescription){Close(WebSocketCloseStatus.NormalClosure,"");connections.Remove(UniqueId);System.Diagnostics.Debug.WriteLine("Websocket {0} closed, now have {1} in list",UniqueId,connections.Count);}publicoverridevoidOnReceiveError(Exceptionerror){Close(WebSocketCloseStatus.ProtocolError,error.Message);connections.Remove(UniqueId);System.Diagnostics.Debug.WriteLine("Websocket {0} errored with {1}, now have {2} in list",UniqueId,error.Message,connections.Count);}publicoverrideasyncTaskOnMessageReceived(ArraySegment<byte>message,WebSocketMessageTypetype){if(type==WebSocketMessageType.Text){vartxt=System.Text.Encoding.UTF8.GetString(message.Array,message.Offset,message.Count);varoutput=System.Text.Encoding.UTF8.GetBytes("Received: "+txt);awaitSendText(output,true);}}publicstaticvoidBroadcast(stringtext,paramsobject[]args){vartxtBuffer=System.Text.Encoding.UTF8.GetBytes(string.Format(text,args));foreach(varconninconnections.Values){conn.SendText(txtBuffer,true);}}}
so having that static method I can call MyWebSocket.Broadcast("some event {0}", "0101"); from anywhere...
When using this code (Framework 4.7.2) , my websocket client says that it is disconnected by the server just after open.
The problem seems to come from connections.Add(UniqueId, this); . Any idea why ?
@mika76 you have to put this code on a background thread and not within the MyWebSocket class
`public static class NotificationDispatcher
{
private static ConcurrentQueue<byte[]> ActiveQueue { get; set; } = new ConcurrentQueue<byte[]>();
private static Thread RunningThread;
private static Dictionary<Guid, NotificationsRoute> _clients = new Dictionary<Guid, NotificationsRoute>();
public static void ClientAdd(NotificationsRoute route) => _clients.Add(route.UniqueId, route);
public static void ClientRemove(Guid guid) => _clients.Remove(guid);
public static void QueueMessage(byte[] message) => ActiveQueue.Enqueue(message);
public static void StartDispatch(CancellationToken cancellationToken)
{
ActiveQueue = new ConcurrentQueue<byte[]>();
RunningThread = new Thread(() =>
{
DateTime lastHeartbeat = DateTime.Now.AddDays(-1);
while (!cancellationToken.IsCancellationRequested)
{
try
{
if (ActiveQueue.TryDequeue(out byte[] dispatchMessage) && dispatchMessage.Length > 0)
foreach (var conn in _clients.Values)
{
_ = conn.SendText(dispatchMessage, true);
}
}
catch (Exception ex)
{
Logger.Log("[Dispatcher Thread Error]" + ex.ToString(), LogLevel.Error);
}
//Sample
if (lastHeartbeat < DateTime.Now.AddSeconds(-10))
{
ActiveQueue.Enqueue($"Heartbeat here at: {lastHeartbeat:yyyy-MM-dd HH:mm:ss}".JsonEncodeSocketMessage());
lastHeartbeat = DateTime.Now;
}
Thread.Sleep(1000);
}
});
RunningThread.Start();
}
public static void StopDispatch()
{
try { RunningThread?.Abort(); _clients = null; } catch { }
}
}`
I am sending a Heartbeat message to all connected clients to see if all clients are getting the message and works great
Activity
mika76 commentedon Sep 18, 2018
This is my solution to the problem...
so having that static method I can call
MyWebSocket.Broadcast("some event {0}", "0101");
from anywhere...oschwab commentedon Dec 18, 2018
Hello,
When using this code (Framework 4.7.2) , my websocket client says that it is disconnected by the server just after open.
The problem seems to come from
connections.Add(UniqueId, this);
. Any idea why ?swagfin commentedon Jan 12, 2023
@mika76 you have to put this code on a background thread and not within the MyWebSocket class
`public static class NotificationDispatcher
{
private static ConcurrentQueue<byte[]> ActiveQueue { get; set; } = new ConcurrentQueue<byte[]>();
I am sending a Heartbeat message to all connected clients to see if all clients are getting the message and works great