[FIX] fixed server client message structure

This commit is contained in:
Sem van der Hoeven
2020-10-20 17:43:28 +02:00
parent 1a539efdd1
commit 33574a7b23
8 changed files with 42 additions and 18 deletions

View File

@@ -16,11 +16,11 @@ namespace Client
private int totalBufferReceived = 0;
public int Port = 5555;
public bool Connected = false;
//TODO send login packet to server with ClientServerUtil.createpayload(0x01,dynamic json with username)
public string Username { get; }
private string username;
public Client()
public Client(string username)
{
this.username = username;
this.tcpClient = new TcpClient();
Debug.WriteLine("Starting connect to server");
tcpClient.BeginConnect("localhost", Port, new AsyncCallback(OnConnect), null);
@@ -31,6 +31,7 @@ namespace Client
Debug.Write("finished connecting to server");
this.tcpClient.EndConnect(ar);
this.stream = tcpClient.GetStream();
SendMessage(JSONConvert.ConstructUsernameMessage(username));
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
}
@@ -100,11 +101,13 @@ namespace Client
public void SendMessage(byte[] message)
{
Debug.WriteLine("[CLIENT] sending message " + Encoding.ASCII.GetString(message));
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWriteComplete), null);
}
private void OnWriteComplete(IAsyncResult ar)
{
Debug.WriteLine("[CLIENT] finished writing");
stream.EndWrite(ar);
}
}

View File

@@ -18,7 +18,7 @@ namespace Client
_model = new Model();
ButtonCommand = new RelayCommand(() =>
{
Client client = new Client();
}, true);
_lobbies = new List<Lobby>();

View File

@@ -26,8 +26,8 @@ namespace Client.Views
private void Button_EnterUsername(object sender, RoutedEventArgs e)
{
User user = new User(usernameTextbox.Text, 0, false);
Client client = new Client();
User user = new User(usernameTextbox.Text);
Client client = new Client(user.Username);
data.User = user;
data.Client = client;

View File

@@ -47,7 +47,7 @@
</ComboBox>
<Label Grid.Row="3" Name="testLabel" FontSize="15" VerticalAlignment="Center"/>
<Label Content="place username here" Grid.Column="1" HorizontalAlignment="Center" Margin="0,12,0,0" VerticalAlignment="Top" Grid.Row="1"/>
<Label Name="usernameLabel" Content="place username here" Grid.Column="1" HorizontalAlignment="Center" Margin="0,12,0,0" VerticalAlignment="Top" Grid.Row="1"/>
</Grid>

View File

@@ -27,7 +27,7 @@ namespace Client
this.DataContext = new ViewModel();
InitializeComponent();
usernameTextbox.Text = data.User.Username;
usernameLabel.Content = data.User.Username;
}
private void Button_Click(object sender, RoutedEventArgs e)