private void User_MessageReceived(Message message) { if (message is Introduction) { SetUserName setUserName = SetUserName; Dispatcher.Invoke(setUserName, (message as Introduction).Name); Dispatcher.Invoke(Init); } else if (message is TextMessage) { AddTextMessage addTextMessageDele = AddTextMessage; Dispatcher.Invoke(addTextMessageDele, message as TextMessage, userName); } else if (message is FileMessage) { AddFileMessage addFileMessageDele = AddFileMessage; Dispatcher.Invoke(addFileMessageDele, message as FileMessage, userName); } }
private bool TestCommand(SetUserName evt, out IEvent e) { e = null; if (!evt.Normalized) { if (user != null && !string.Equals(user.UserName, evt.UserName)) { e = new UserNameChanged(evt.UserName, evt.Normalized); } } else { if (user != null && !string.Equals(user.NormalizedUserName, evt.UserName)) { e = new UserNameChanged(evt.UserName, evt.Normalized); } } //add some tests? return(true); }
private void btnCoupleUp_Click(object sender, EventArgs e) { PrintGameBoard(); string IP = textBoxIPadress.Text; string username = textBox1.Text; var userImageIndex = comboBox1.SelectedIndex; myClient = new Client(this); Thread clientThread = new Thread(myClient.Start); clientThread.Start(IP); clientThread.Join(); NetworkStream n = myClient.client.GetStream(); SetUserName message = new SetUserName(username); SetUserImage msg = new SetUserImage(userImageIndex); string json = JsonConvert.SerializeObject(msg); BinaryWriter w = new BinaryWriter(n); w.Write(json); w.Flush(); label1.Visible = false; label2.Visible = false; textBoxIPadress.Visible = false; textBox1.Visible = false; btnCoupleUp.Visible = false; comboBox1.Visible = false; btnUp.Visible = false; btnDown.Visible = false; btnLeft.Visible = false; btnRight.Visible = false; }
public void Run() { try { string message = ""; while (tcpclient.Connected) { NetworkStream n = tcpclient.GetStream(); // Slipp Patrik /*if ((IPAddress)tcpclient.Client.RemoteEndPoint() == "127.0.0.1") * { * tcpclient.Close(); * }*/ message = new BinaryReader(n).ReadString(); Message msg = JsonConvert.DeserializeObject <Message>(message); switch (msg.type) { case "Move": Move myMove = JsonConvert.DeserializeObject <Move>(message); RefreshGameBoard result = myServer.MoveLogic(thisPlayer, myMove); string json = JsonConvert.SerializeObject(result); myServer.Broadcast(this, json); break; case "SetUserName": SetUserName username = JsonConvert.DeserializeObject <SetUserName>(message); thisPlayer = new Player(5, 5, 4 /*username.username*/); myServer.players.Add(thisPlayer); RefreshGameBoard currentGameBoard = new RefreshGameBoard(myServer.players); string currentGameBoardJson = JsonConvert.SerializeObject(currentGameBoard); myServer.Broadcast(this, currentGameBoardJson); break; case "SetUserImage": SetUserImage userImage = JsonConvert.DeserializeObject <SetUserImage>(message); thisPlayer = new Player(5, 5, userImage.userImageIndex /*username.username*/); myServer.players.Add(thisPlayer); RefreshGameBoard refreshedGameBoard = new RefreshGameBoard(myServer.players); string refreshedGameBoardJson = JsonConvert.SerializeObject(refreshedGameBoard); myServer.Broadcast(this, refreshedGameBoardJson); break; default: // ++ i failed attemps vid 5 failures addera ip till shitlist. break; } Debug.WriteLine(message); } } catch (Exception ex) { Debug.WriteLine("Här är vårt message: " + ex.Message); } finally { myServer.players.Remove(thisPlayer); myServer.clients.Remove(this); //myServer.DisconnectClient(this); tcpclient.Close(); RefreshGameBoard refreshedGameBoard = new RefreshGameBoard(myServer.players); string refreshedGameBoardJson = JsonConvert.SerializeObject(refreshedGameBoard); myServer.Broadcast(this, refreshedGameBoardJson); } }