5 Commits

Author SHA1 Message Date
Dogukan
a4e45b1a6b [MISC] tried to send the message to the current lobby, didn't work. 2020-10-21 20:43:28 +02:00
Dogukan
74f8e868f6 [ADDITION] servercommunication towards the client.
Still in progress not functional because of a null exception when receiving the broadcast message.
2020-10-21 17:47:48 +02:00
Dogukan
381c142eaa [ADDITION] Added databinding to the chatbox function.
Tried to broadcast the message, doesn't work yet.
2020-10-20 23:54:29 +02:00
Dogukan
4d161391b1 Merge remote-tracking branch 'origin/setupBranch' into setupBranch 2020-10-20 19:53:35 +02:00
Dogukan
7dbdcc8bcc [ADDITION] fixed the chatbox size 2020-10-20 19:53:28 +02:00
11 changed files with 106 additions and 31 deletions

View File

@@ -65,12 +65,11 @@ namespace Client
} }
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null); stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
} }
private void handleData(byte[] message) private void handleData(byte[] message)
{ {
byte id = message[0]; byte id = message[4];
byte[] payload = new byte[message.Length - 1]; byte[] payload = new byte[message.Length - 1];
Array.Copy(message, 1, payload, 0, message.Length - 1); Array.Copy(message, 1, payload, 0, message.Length - 1);
switch (id) switch (id)
@@ -83,17 +82,21 @@ namespace Client
(string, string) combo = JSONConvert.GetUsernameAndMessage(payload); (string, string) combo = JSONConvert.GetUsernameAndMessage(payload);
string textUsername = combo.Item1; string textUsername = combo.Item1;
string textMsg = combo.Item2; string textMsg = combo.Item2;
//TODO display username and message in chat window
//TODO display username and message in chat window
Debug.WriteLine("[CLIENT] INCOMING MESSAGE!");
Debug.WriteLine("[CLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
break; break;
case JSONConvert.LOBBY: case JSONConvert.LOBBY:
// lobby data // lobby data
//TODO fill lobby with the data received //TODO fill lobby with the data received
break; break;
case JSONConvert.CANVAS: case JSONConvert.CANVAS:
// canvas data // canvas data
break; break;
default: default:
Debug.WriteLine("[CLIENT] Received weird identifier: " + id); Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
break; break;

View File

@@ -30,6 +30,7 @@ namespace Client
private User _user; private User _user;
private Client _client; private Client _client;
private Lobby _lobby; private Lobby _lobby;
private string _message;
private ClientData() private ClientData()
{ {
@@ -55,5 +56,17 @@ namespace Client
set { _lobby = value; } set { _lobby = value; }
} }
public String Message
{
get
{
return _message;
}
set
{
_message = value;
}
}
} }
} }

View File

@@ -1,10 +1,71 @@
using System.Collections.ObjectModel; using GalaSoft.MvvmLight.Command;
using SharedClientServer;
using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;
namespace Client.ViewModels namespace Client.ViewModels
{ {
class ViewModelGame : INotifyPropertyChanged class ViewModelGame : INotifyPropertyChanged
{ {
ClientData data = ClientData.Instance;
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
private dynamic _payload;
private string _username;
private string _message;
public string Message
{
get
{
return _message;
}
set
{
_message = value;
}
}
public ICommand OnKeyDown { get; set; }
public ViewModelGame()
{
if (_payload == null)
{
_message = "";
}
else
{
//_message = data.Message;
//_username = data.User.Username;
//Messages.Add($"{data.User.Username}: {Message}");
}
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
}
private void ChatBox_KeyDown()
{
//if enter then clear textbox and send message.
if (Message != string.Empty) AddMessage(Message);
Message = string.Empty;
}
internal void AddMessage(string message)
{
Messages.Add($"{data.User.Username}: {message}");
_payload = new
{
username = data.User.Username,
message = message
};
//Broadcast the message after adding it to the list!
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
}
} }
} }

View File

@@ -48,8 +48,13 @@
</Border> </Border>
<Grid Grid.Column="2" Grid.Row="1"> <Grid Grid.Column="2" Grid.Row="1">
<TextBox Name="SentMessage" IsReadOnly="True"/> <ListBox Name ="TextBox" ItemsSource="{Binding Path=Messages}" Margin="0,0,0,69"/>
<TextBox x:Name="ChatBox" Keyboard.KeyDown="ChatBox_KeyDown" Width="200" Height="50" ToolTip="Message goes here" ToolTipService.IsEnabled="True" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<TextBox Name="ChatBox" Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,465,0,0">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding OnKeyDown}"/>
</TextBox.InputBindings>
</TextBox>
</Grid> </Grid>
</Grid> </Grid>

View File

@@ -18,7 +18,7 @@ namespace Client.Views
/// </summary> /// </summary>
public partial class GameWindow : Window public partial class GameWindow : Window
{ {
ClientData data = ClientData.Instance;
public GameWindow() public GameWindow()
{ {
DataContext = new ViewModelGame(); DataContext = new ViewModelGame();
@@ -82,7 +82,6 @@ namespace Client.Views
// var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement; // var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
// TEST.Children.Add(deepCopy); // TEST.Children.Add(deepCopy);
//} //}
} }
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e) private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
@@ -95,23 +94,14 @@ namespace Client.Views
color = colorSelected; color = colorSelected;
} }
private void ChatBox_KeyDown(object sender, KeyEventArgs e) ///*
{ // * Writes the current client's message to the chatbox.
//if enter then clear textbox and send message. // */
if (e.Key.Equals(Key.Enter)) //private void WriteToChat(string message)
{ //{
WriteToChat(ChatBox.Text); // string user = data.User.Username;
ChatBox.Clear(); // SentMessage.AppendText($"{user}: {message}\n");
} // data.User.Message = message;
} //}
/*
* Writes the current client's message to the chatbox.
*/
private void WriteToChat(string message)
{
string user = data.User.Username;
SentMessage.AppendText($"{user}: {message}\n");
}
} }
} }

View File

@@ -32,7 +32,6 @@ namespace Client
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
{ {
Lobby lobbySelected = LobbyList.SelectedItem as Lobby; Lobby lobbySelected = LobbyList.SelectedItem as Lobby;
if(lobbySelected != null) if(lobbySelected != null)
{ {

View File

@@ -1,4 +1,5 @@
 
using Newtonsoft.Json.Linq;
using SharedClientServer; using SharedClientServer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -16,6 +17,7 @@ namespace Server.Models
private byte[] totalBuffer = new byte[1024]; private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0; private int totalBufferReceived = 0;
public User User { get; set; } public User User { get; set; }
private ServerCommunication serverCom = ServerCommunication.INSTANCE;
/// <summary> /// <summary>
@@ -116,7 +118,11 @@ namespace Server.Models
string textUsername = combo.Item1; string textUsername = combo.Item1;
string textMsg = combo.Item2; string textMsg = combo.Item2;
Debug.WriteLine("[SERVERCLIENT] User name: {0}\t User message: {1}", textUsername, textMsg);
// todo handle sending to all except this user the username and message to display in chat // todo handle sending to all except this user the username and message to display in chat
serverCom.SendToLobby(User.Lobby,payload);
Debug.WriteLine("Payload has been sent!");
break; break;
case JSONConvert.LOBBY: case JSONConvert.LOBBY:

View File

@@ -1,5 +1,4 @@
 using Client;
using Client;
using SharedClientServer; using SharedClientServer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@@ -18,6 +18,5 @@ namespace SharedClientServer
Array.Copy(stringAsBytes, 0, res, 1, stringAsBytes.Length); Array.Copy(stringAsBytes, 0, res, 1, stringAsBytes.Length);
return res; return res;
} }
} }
} }

View File

@@ -49,7 +49,6 @@ namespace SharedClientServer
} }
/// <summary> /// <summary>
/// constructs a message that can be sent to the clients or server /// constructs a message that can be sent to the clients or server
/// </summary> /// </summary>

View File

@@ -9,6 +9,7 @@ namespace SharedClientServer
private string _username; private string _username;
private int _score; private int _score;
private bool _host; private bool _host;
private string _message;
public User(string username, int score, bool host) public User(string username, int score, bool host)
{ {