Exemple #1
0
 }
                        System.Diagnostics.Debug.WriteLine(" -> Not found error, attempting to reinitialize");
                        //  disconnect
                        //System.Diagnostics.Debug.WriteLine(" -> -> uninitialize");
                        //Uninitialize();
                        //  reconnect
                        System.Diagnostics.Debug.WriteLine(" -> -> reinitialize");
                        Initialize(_UserId, _AuthKey, false, _PushData);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(string.Format("*** ChatService.Initialize - Exception: {0}", ex));
                    }
                }

                if (status.Category == PNStatusCategory.PNConnectedCategory)
                {
                    System.Diagnostics.Debug.WriteLine(" -> Connected");
                    // this is expected for a subscribe, this means there is no error or issue whatsoever
                    // reset fail count
                    _FailCount = 0;
                }
                else if (status.Category == PNStatusCategory.PNReconnectedCategory)
                {
                    System.Diagnostics.Debug.WriteLine(" -> Reconnected");
                    // this usually occurs if subscribe temporarily fails but reconnects. This means
                    // there was an error but there is no longer any issue
                }
                else if (status.Category == PNStatusCategory.PNDisconnectedCategory)
                {
                    System.Diagnostics.Debug.WriteLine(" -> Disconnected");
                    // this is the expected category for an unsubscribe. This means there
                    // was no error in unsubscribing from everything
                }
                else if (status.Category == PNStatusCategory.PNUnexpectedDisconnectCategory)
                {
                    System.Diagnostics.Debug.WriteLine(" -> Unexpected disconnected");
                    // this is usually an issue with the internet connection, this is an error, handle appropriately
                }
                else if (status.Category == PNStatusCategory.PNAccessDeniedCategory)
                {
                    System.Diagnostics.Debug.WriteLine(" -> Access Denied");
                    // this means that PAM does allow this client to subscribe to this
                    // channel and channel group configuration. This is another explicit error
                }
            }
        }

        #endregion Callbacks

        #region Operations

        public void GetHistory(long timeStamp)
        {
            try
            {
                if (!Initialized || string.IsNullOrEmpty(_UserId)) return;
                _Pubnub.History()
                    .Channel(_UserId)
                    .Start(timeStamp)
                    .Count(20)
                    .Async(new PNHistoryResultExt((result, status) =>
                    {
                        if ((result.Messages == null) || (result.Messages.Count == 0)) return;
                        foreach (var message in result.Messages)
                        {
                            //  let listeners know
                            MessageReceived?.Invoke(this, new MessageEventArgs(message.Entry.ToString()));
                        }
                    }));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("*** ChatService.GetHistory - Exception: {0}", ex));
            }
        }

        public void Publish(string to, string message, object state)
        {
            try
            {
                if (!Initialized || string.IsNullOrEmpty(_UserId)) throw new ArgumentException("Cannot publish before initialize.");

                //  publish message
                _Pubnub.Publish()
                    .Channel(to)
                    .Message(message)
                    .Async(new PNPublishResultExt((result, status) =>
                    {
                        if (message == null) return;
                        //  get the message
                        PublishComplete?.Invoke(this, new MessageEventArgs(message)
                        {
                            State = state,
                            Success = !status.Error,
                            Timestamp = new DateTime(result.Timetoken)
                        });
                    }));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("*** ChatService.Publish - Exception: {0}", ex));
            }
        }

        #endregion Operations

        #region Event Triggers

        private void RaiseChannelJoined(string channel, string uuid)
        {
            ChannelJoined?.Invoke(this, new PresenceEventArgs(channel, uuid));
        }

        private void RaiseChannelLeft(string channel, string uuid)
        {
            ChannelLeft?.Invoke(this, new PresenceEventArgs(channel, uuid));
        }

        private void RaiseChannelTimeout(string channel, string uuid)
        {
            ChannelTimeout?.Invoke(this, new PresenceEventArgs(channel, uuid));
        }

        private void RaiseChannelState(string channel, string uuid, ChatState state)
        {
            ChannelState?.Invoke(this, new PresenceEventArgs(channel, uuid, state));
        }

        #endregion Event Triggers

        #region Cleanup

        public void Dispose()
        {
            if (_Disposed) return;
            //  TODO: check this - it looks dodgy
            SubscribeCallbackExt listenerSubscribeCallack = new SubscribeCallbackExt((pubnubObj, message) => { }, (pubnubObj, presence) => { }, (pubnubObj, status) => { });
            _Pubnub.AddListener(listenerSubscribeCallack);
            // some time later
            _Pubnub.RemoveListener(listenerSubscribeCallack);
            _Pubnub.Destroy();
            _Disposed = true;
        }

        #endregion Cleanup
    }
}


Exemple #2
0
 private void RaiseChannelTimeout(string channel, string uuid)
 {
     ChannelTimeout?.Invoke(this, new PresenceEventArgs(channel, uuid));
 }