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; }); }); }
public void Connect(string userName, string serverIp) { try { localClient = new Client(); localClient.Name = userName; var context = new InstanceContext(this); chatClient = new ChatServiceClient(context); //As the address in the configuration file is set to localhost //we want to change it so we can call a service in internal //network, or over internet var servicePath = chatClient.Endpoint.ListenUri.AbsolutePath; var serviceListenPort = chatClient.Endpoint.Address.Uri.Port.ToString(); chatClient.Endpoint.Address = new EndpointAddress($"net.tcp://{serverIp}:{serviceListenPort}{servicePath}"); chatClient.Open(); chatClient.InnerDuplexChannel.Faulted += (sender, args) => HandleProxy(); chatClient.InnerDuplexChannel.Opened += (sender, args) => HandleProxy(); chatClient.InnerDuplexChannel.Closed += (sender, args) => HandleProxy(); chatClient.ConnectAsync(localClient); Connected = true; } catch (Exception exception) { ServiceMessage.AppendLine(exception.Message); Connected = false; } }
public void Connect(string login) { _login = login; if (_client == null) { return; } _client.ConnectAsync(login); }