/** * Subscribe - Private Interface * * @param String channel name. * @param Procedure function callback * @param String timetoken. */ private void _subscribe(string channel, object timetoken, Action<object> usercallback, bool reconnect) { //Exit if the channel is unsubscribed if (!_channelSubscription.ContainsKey(channel)) { if (appSwitch.TraceInfo) { Trace.WriteLine(string.Format("DateTime {0}, Due to Unsubscribe, further subscription was stopped for channel {1}", DateTime.Now.ToString(), channel.ToString())); } return; } //Check internet connection ClientNetworkStatus.checkInternetStatus(_pubnetSystemActive, updateInternetStatus); Thread.Sleep(2000); if (!_pubnetInternetStatus && _pubnetSystemActive) { if (appSwitch.TraceInfo) { Trace.WriteLine(string.Format("DateTime {0}, Subscribe - No internet connection for {1}", DateTime.Now.ToString(), channel)); } ReconnectState netState = new ReconnectState(); netState.channel = channel; netState.type = ResponseType.Subscribe; netState.callback = usercallback; netState.timetoken = timetoken; reconnectNetwork(netState); return; } // Begin recursive subscribe try { // Build URL List<string> url = new List<string>(); url.Add("subscribe"); url.Add(this.SUBSCRIBE_KEY); url.Add(channel); url.Add("0"); url.Add(timetoken.ToString()); // Wait for message _urlRequest(url, ResponseType.Subscribe, usercallback, reconnect); } catch(Exception ex) { if (appSwitch.TraceError) { Trace.WriteLine(string.Format("DateTime {0} method:_subscribe \n channel={1} \n timetoken={2} \n Exception Details={3}",DateTime.Now.ToString(), channel, timetoken.ToString(), ex.ToString())); } //TODO: Check if we need sleep time System.Threading.Thread.Sleep(1000); this._subscribe(channel, timetoken,usercallback,false); } }
private void reconnectNetwork(ReconnectState netState) { System.Threading.Timer timer = new Timer(new TimerCallback(reconnectNetworkCallback),netState,0,PUBNUB_NETWORK_CHECK_CALLBACK_INTERVAL_IN_SEC * 1000); _channelReconnectTimer.AddOrUpdate(netState.channel, timer, (key, oldState) => timer); }