/// <summary> /// Handles multicast push updates request from a multicast view model. /// </summary> private void VMInstance_RequestMulticastPushUpdates(object sender, MulticastPushUpdatesEventArgs e) { var vmInfo = _activeVMs.FirstOrDefault(kvp => kvp.Value.Instance == sender).Value; if (vmInfo == null) { return; } if (e.PushData) { var vmInstance = vmInfo.Instance; lock (vmInstance) { var changedProperties = vmInstance.AcceptChangedProperties(); if (changedProperties != null && changedProperties.Count > 0) { Send(vmInfo, new GroupSend { GroupName = vmInfo.GroupName, ConnectionIds = e.ConnectionIds, ExcludedConnectionIds = new List <string> { e.ExcludedConnectionId }, Data = vmInstance.Serialize(changedProperties) }); } e.PushData = false; } } else if (e.ExcludedConnectionId != vmInfo.ConnectionId) { e.ConnectionIds.Add(vmInfo.ConnectionId); } }
/// <summary> /// Pushes changed properties to all associated clients, excluding the given connection. /// </summary> /// <param name="excludedConnectionId">Connection to exclude.</param> internal void PushUpdatesExcept(string excludedConnectionId) { var delegates = RequestMulticastPushUpdates?.GetInvocationList().ToList(); if (delegates != null && delegates.Count > 0) { var eventArgs = new MulticastPushUpdatesEventArgs { ExcludedConnectionId = excludedConnectionId }; // First invocation cycle is to get the participating connection Ids from the VMControllers when a group name isn't used. if (string.IsNullOrEmpty(GroupName)) { RequestMulticastPushUpdates?.Invoke(this, eventArgs); } // Invoke the first available event handler to do the actual data push. // If successful, the handler will set the PushData back to false. eventArgs.PushData = true; foreach (var d in delegates) { d.DynamicInvoke(this, eventArgs); if (!eventArgs.PushData) { break; } } } }
/// <summary> /// Handles multicast push updates request from a multicast view model. /// </summary> private void VMInstance_RequestMulticastPushUpdates(object sender, MulticastPushUpdatesEventArgs e) { var vmInfo = _activeVMs.FirstOrDefault(kvp => kvp.Value.Instance == sender).Value; if (vmInfo == null) { return; } if (e.PushData) { var vmInstance = vmInfo.Instance; lock (vmInstance) { var changedProperties = vmInstance.AcceptChangedProperties(); if (changedProperties != null && changedProperties.Count > 0) { var vmData = vmInstance.Serialize(changedProperties); ResponseVMFilter.Invoke(vmInfo.Id, vmInstance, vmData, filteredData => { if (!string.IsNullOrEmpty(e.GroupName)) { var message = new GroupSend { GroupName = e.GroupName, ConnectionIds = e.ConnectionIds, ExcludedConnectionId = e.ExcludedConnectionId, Data = (string)filteredData }; _vmResponse(MULTICAST + nameof(GroupSend), vmInfo.Id, JsonConvert.SerializeObject(message)); } else { foreach (var connectionId in e.ConnectionIds) { _vmResponse(connectionId, vmInfo.Id, (string)filteredData); } } }); } e.PushData = false; } } else if (e.ExcludedConnectionId != vmInfo.ConnectionId) { e.ConnectionIds.Add(vmInfo.ConnectionId); } }