private IEnumerator RegisterSubscriptionHandlerAfterDelay( ChannelSubscription subscription, MessageRouter messageRouter ) { // skip a frame to make sure the router is fully built // before handling any messages yield return(null); // make sure the subscription is still active // (we haven't been killed in the mean time) if (!subscriptions.Contains(subscription)) { yield break; } // register the handler Manager.SubscriptionRouter.HandleSubscription( subscription, messageRouter.RouteMessage ); // register hooks if (!hooksRegistered) { Manager.Tunnel.OnConnectionLost += OnConnectionLost; Manager.Tunnel.OnConnectionRegained += OnConnectionRegained; hooksRegistered = true; } }
/// <summary> /// Creates a new subscription of this session to a channel /// </summary> /// <param name="channel"></param> /// <returns></returns> public ChannelSubscription CreateSubscription(SpecificChannel channel) { string sessionId = sessionIdRepository.SessionId; string url = new Uri(serverUri, "subscribe").ToString(); var subscription = new ChannelSubscription( channel.ChannelName, sessionId ); var serializedSubscription = Serializer.ToJson <ChannelSubscription>( subscription, SerializationContext.ServerToClient ); http.PendingRequest().Post(url, new JsonObject { ["environmentId"] = environmentId, ["broadcastingKey"] = broadcastingKey, ["channel"] = channel.ChannelName, ["sessionId"] = sessionId, ["subscription"] = serializedSubscription }); return(subscription); }
/// <summary> /// Ends a subscription and returns true if it was /// the last subscriptions for this channel, thus we should /// unsubscribe from the channel /// </summary> /// <param name="subscription"></param> /// <returns></returns> private bool EndSubscription(ChannelSubscription subscription) { bool removed1 = activeSubscriptions.Remove(subscription); bool removed2 = pendingSubscriptions.Remove(subscription); // the subscription has already been removed long ago if (!removed1 && !removed2) { return(false); } if (activeSubscriptions.Keys .Any(s => s.ChannelName == subscription.ChannelName)) { return(false); } if (pendingSubscriptions.Keys .Any(s => s.ChannelName == subscription.ChannelName)) { return(false); } return(true); }
private void CheckPendingSubscriptionExpiration( ChannelSubscription subscription ) { if (!pendingSubscriptions.ContainsKey(subscription)) { throw new ArgumentException( "Given subscription is not pending." ); } PendingSubscription pendingSub = pendingSubscriptions[subscription]; // still young enough if ((pendingSub.createdAt - DateTime.UtcNow).TotalMinutes < 5.0) { return; } // too old, remove Debug.LogWarning( "[Unisave] Removing expired pending subscription: " + Serializer.ToJson(subscription).ToString(true) ); EndSubscriptions(new[] { subscription }); CheckTunnelNeededness(); }
public void HandleSubscription( ChannelSubscription subscription, Action <BroadcastingMessage> handler ) { if (subscription == null) { throw new ArgumentNullException(nameof(subscription)); } if (handler == null) { throw new ArgumentNullException(nameof(handler)); } if (activeSubscriptions.ContainsKey(subscription)) { throw new InvalidOperationException( "This subscription is already being handled" ); } activeSubscriptions[subscription] = handler; if (pendingSubscriptions.ContainsKey(subscription)) { var pendingSub = pendingSubscriptions[subscription]; pendingSubscriptions.Remove(subscription); while (pendingSub.pendingMessages.Count > 0) { InvokeHandlerSafely( handler, pendingSub.pendingMessages.Dequeue() ); } } tunnel.IsNeeded(); }
/// <summary> /// Receive messages from a channel subscription /// </summary> /// <param name="subscription"></param> /// <returns></returns> protected MessageRouterBuilder FromSubscription( ChannelSubscription subscription ) { if (subscriptions.Contains(subscription)) { throw new InvalidOperationException( "You cannot handle a subscription twice" ); } subscriptions.Add(subscription); var messageRouter = new MessageRouter(); StartCoroutine( RegisterSubscriptionHandlerAfterDelay( subscription, messageRouter ) ); return(new MessageRouterBuilder(messageRouter)); }
protected bool Equals(ChannelSubscription other) { return(SubscriptionId == other.SubscriptionId); }