/// <summary> /// Show new message on the scene /// </summary> /// <param name="message">Message data</param> public void Show(MessageData message) { if (message is null) { throw new ArgumentNullException("message"); } OnMessagePushed?.Invoke(message); }
public void PushMesage(string message, string topicName = "") { var ea = new MessagePushingEventArgs(message, topicName); OnMessagePushing?.Invoke(this, ea); if (ea.Cancel) { return; } List <string> itemsToRemove = new List <string>(); if (string.IsNullOrWhiteSpace(topicName)) { foreach (var session in _sessions) { try { if (session.Key != session.Value.Connection.Id) { Debugger.Break(); } session.Value.Response.WriteAsync($"data: {message}\n\n"); session.Value.Response.Body.Flush(); } catch (ObjectDisposedException) { itemsToRemove.Add(session.Key); } } } else { if (_topics.TryGetValue(topicName, out ConcurrentDictionary <string, HttpContext> followers)) { List <string> itemsToRemovex = new List <string>(); foreach (var follower in followers) { try { follower.Value.Response.WriteAsync($"data: {message}\n\n"); follower.Value.Response.Body.Flush(); } catch (ObjectDisposedException) { itemsToRemove.Add(follower.Value.Connection.Id); itemsToRemovex.Add(follower.Key); } } itemsToRemove.ForEach(ids => { followers.Remove(ids, out HttpContext context); }); } } itemsToRemove.ForEach(ids => { _sessions.Remove(ids, out HttpContext context); if (context != null) { context.Abort(); } }); OnMessagePushed?.Invoke(this, new MessagePushedEventArgs(message, topicName)); }