/// <summary> /// Verbindet mit dem Server und meldet sich mit den Nutzerdaten an. /// </summary> /// <param name="Username"></param> /// <param name="Password"></param> /// <returns>Gibt an ob die Verbindung und der Login erfolgreich waren.</returns> public bool Connect(string Username, string Password) { try { tcpClient = new TcpClient(settings.IpAddress, settings.Port); streamRW = new StreamRW(tcpClient.GetStream()); Message message = new Message() { content = new Content(ContentType.Login, Username, Password), sender = Username, sendTime = DateTime.Now }; Send(message); username = Username; if (streamRW.ReadLine() == "Login successfull") { return(true); } else { streamRW.Close(); tcpClient.Close(); return(false); } } catch (Exception e) { Log.WriteLine("[Server][{0}] {1}", DateTime.Now, e.InnerException); MessageBox.Show("Server nicht erreichtbar"); return(false); } }
/// <summary> /// Lädt die ChatHistory vom Server und zeigt sie auf der Form an. /// </summary> public void LoadChat() { Message sendMessage = new Message() { content = new Content(ContentType.RequestChat) }; streamRW.WriteLine(sendMessage.ToString()); List <Message> chatHistory = Serializer.Deserialize <List <Message> >(streamRW.ReadLine()); if (chatHistory != null) { foreach (Message message in chatHistory) { System.Windows.HorizontalAlignment horizontallignment; if (message.sender == username) { horizontallignment = System.Windows.HorizontalAlignment.Right; } else { horizontallignment = System.Windows.HorizontalAlignment.Left; } RaiseMessageReceivedEvent(horizontallignment, message.content.parameter[0], message.sender, message.sendTime); } } dTimer = new DispatcherTimer(); dTimer.Interval = new TimeSpan(0, 0, 1); dTimer.Tick += DTimer_Tick; dTimer.Start(); }
/// <summary> /// Versucht, sich beim Server zu registrieren. /// </summary> /// <param name="username"></param> /// <param name="password"></param> public bool Register(string username, string password) { if (tcpClient == null || !IsConnected) { settings = SettingsDatabase.Load(); tcpClient = new TcpClient(settings.IpAddress, settings.Port); streamRW = new StreamRW(tcpClient.GetStream()); Message message = new Message() { content = new Content(ContentType.Register, username, password), sender = null, sendTime = DateTime.Now }; Send(message); string buffer = streamRW.ReadLine(); if (buffer == "Register succesfull") { Log.WriteLine("[Client][{0}] {1} registered.", DateTime.Now, username); } else if (buffer != "") { MessageBox.Show(buffer); return(false); } Disconnect("Registration finished"); return(true); } return(false); }
/// <summary> /// Beginnt mit der Überwachung des Streams und läuft solange der TcpClient connected ist. /// </summary> public void Start() { while (tcpClient.Connected) { string Message = streamRW.ReadLine(); if (Message != "") { ProcessMessage(Message); } } }
/// <summary> /// Beginnt mit der Überwachung des Streams und läuft solange der TcpClient connected ist. /// </summary> public void Start() { // Solange der Client mit dem Server verbunden ist, wird jede ankommende Nachricht verarbeitet. while (tcpClient.Connected) { string Message = streamRW.ReadLine(); if (Message != null && Message != "") { ProcessMessage(Message); } } }
/// <summary> /// Verbindet mit dem Server und meldet sich mit den Nutzerdaten an. /// </summary> /// <param name="Username"></param> /// <param name="Password"></param> /// <returns>Gibt an ob die Verbindung und der Login erfolgreich waren.</returns> public bool Connect(string Username, string Password) { tcpClient = new TcpClient("87.172.204.102", 1337); streamRW = new StreamRW(tcpClient.GetStream()); Message message = new Message() { content = new Command(CommandType.Login, Username, Password), sender = Username, sendTime = DateTime.Now }; Send(message); username = Username; return((streamRW.ReadLine() == "Login successfull") ? true : false); }