private ChatService() { _messages = Observable.Create <ChatMessage>(observer => { try { if (_client == null) { _clientCallback = new ChatServiceClientCallback(observer); _client = new ChatServiceClient(new InstanceContext(_clientCallback)); _client.ChannelFactory.Closed += (s, a) => { observer.OnCompleted(); }; _client.ChannelFactory.Faulted += (s, a) => { observer.OnCompleted(); }; if (_login != null) { _token = _client.Connect(_login); } } } catch (Exception e) { observer.OnError(e); } return(() => { _client.Close(); _client = null; }); }); }
public void Connect(string user, string host) { User = user; if (!host.Contains("://")) { host = string.Format("http://{0}", host); } Host = host; // Validate host Uri uri; UrlExtractor.TryCreate(Host, out uri, validHostUriSchemes); if (uri == null) { ConnectionFailed(this, DateTime.Now, string.Format("Invalid format for host: {0}", Host)); return; } Host = uri.ToString(); try { Binding binding; if (uri.Scheme.Equals(Uri.UriSchemeNetPipe)) { var namedPipeBinding = new NetNamedPipeBinding(); namedPipeBinding.Security.Mode = NetNamedPipeSecurityMode.None; binding = namedPipeBinding; } else if (uri.Scheme.Equals(Uri.UriSchemeNetTcp)) { var tcpBinding = new NetTcpBinding(); tcpBinding.Security.Mode = SecurityMode.None; binding = tcpBinding; } else { var httpBinding = new WSDualHttpBinding(); httpBinding.Security.Mode = WSDualHttpSecurityMode.None; binding = httpBinding; } // Append service name // Note: URI creation appends any missing "/" to the host, so it's safe to just append if (!Host.EndsWith(serviceName) && !Host.EndsWith(string.Format("{0}/", serviceName))) { Host = string.Format("{0}{1}", Host, serviceName); } // Create the endpoint address EndpointAddress endpointAddress = new EndpointAddress(Host); client = new ChatServiceClient(new InstanceContext(this), binding, endpointAddress); client.Open(); client.Connect(user); } catch (Exception exception) { TriggerConnectionFailed(exception); } }
void ConnectUser() { if (!isConnected) { client = new ChatServiceClient(new System.ServiceModel.InstanceContext(this)); ID = client.Connect(tbUserName.Text); tbUserName.IsEnabled = false; btnConDiscon.Content = "Disconnect"; isConnected = true; } }
private void ConnectButt_Click(object sender, EventArgs e) { ChatCallback callback = new ChatCallback(); proxy = new ChatServiceClient(new InstanceContext(callback)); callback.Change += callback_Change; if (!proxy.Connect(nicktext.Text)) { MessageBox.Show("Imie już zarezerwowane"); } else { chatBox.Enabled = true; Message.Enabled = true; Sender.Enabled = true; nicktext.Enabled = false; } KeyPreview = true; }
private void Connect(string username, string hostname) { try { const string endpointConfigurationName = "netTcpBinding_IChatService"; var remoteAdress = String.Format("net.tcp://{0}/InkChat", hostname); _chatClient = new ChatServiceClient(new InstanceContext(this), endpointConfigurationName, remoteAdress); //_chatClient.ClientCredential //_chatClient.ClientCredentials.Windows.ClientCredential.UserName = "******";//用户名 //_chatClient.ClientCredentials.Windows.ClientCredential.Password = "";//密码 //_chatClient.ClientCredentials.Windows.ClientCredential.Domain = "10.0.0.2"; _chatClient.Open(); _chatClient.Connect(username); } catch (Exception e) { _dispatcher.Invoke(new Action(() => AppendText(String.Format("Couldn't establish connection to server. {0} {1}", e.Message, e.InnerException != null ? e.InnerException.Message : ""), Colors.Red))); _dispatcher.Invoke(new Action(() => EnableChat(false))); _dispatcher.Invoke(new Action(() => EnableConnect(true))); _dispatcher.Invoke(new Action(() => EnableDisconnect(false))); } }