Esempio n. 1
0
        private ChatService()
        {
            _messages = Observable.Create <ChatMessage>(observer =>
            {
                try
                {
                    if (_client == null)
                    {
                        _client = new ChatServiceClient("PollingHttpBinding_IChatServer");
                        _client.PushMessageReceived    += (s, a) => observer.OnNext(a.message);
                        _client.ConnectCompleted       += (s, a) => _token = a.Result;
                        _client.ChannelFactory.Closed  += (s, a) => observer.OnCompleted();
                        _client.ChannelFactory.Faulted += (s, a) => observer.OnCompleted();

                        if (_login != null)
                        {
                            _client.ConnectAsync(_login);
                        }
                    }
                }
                catch (Exception e)
                {
                    observer.OnError(e);
                }

                return(() =>
                {
                    _client.CloseAsync();
                    _client = null;
                });
            });
        }