From 3b2b75df537a9070c942d155b2507036708e98d7 Mon Sep 17 00:00:00 2001 From: Patrick Hellebrand Date: Wed, 22 Sep 2021 14:57:36 +0200 Subject: [PATCH] SendMessage is now Json --- PolyChat/Controller.cs | 12 +++++++++--- PolyChat/MainPage.xaml.cs | 17 +++++++++++------ PolyChat/Models/Connection.cs | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 48dd274..33d29dc 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -5,6 +5,7 @@ using System.Net; using SocketIOSharp.Server; using SocketIOSharp.Server.Client; using PolyChat.Models; +using System.Text.Json; namespace PolyChat { @@ -50,20 +51,24 @@ namespace PolyChat { Debug.WriteLine("--- Client connected! ---"); // setup event listeners - socket.On("initial", (JToken[] data) => + socket.On("initial", async (JToken[] data) => { Debug.WriteLine("--- initial packet received ---"); string ForeignIp = data.ToString(); //Todo deserialize inital packet and extract ip address Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(Data))); + UIController.OnIncomingConnection(ForeignIp); }); }); } - public void SendMessage(string ip, string message) + public void SendMessage(string ip, string type, string content) { Debug.WriteLine("--- Controller.SendMessage ---"); - Connections[ip].SendMessage(message); + Debug.WriteLine($"{type} -> {ip} content: {content}"); + string json = $"{{ type: {type}, content: {content} }}"; + Debug.WriteLine($"json: {json}"); + Connections[ip].SendMessage(json); } private void OnMessage(JToken[] data) @@ -72,6 +77,7 @@ namespace PolyChat if (data != null && data.Length > 0 && data[0] != null) { Debug.WriteLine("Message: " + data[0]); + Debug.WriteLine($"DATA: {data[0].ToString()}"); } else Debug.WriteLine("Undefined: " + data); } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 67f6688..8f51424 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -5,6 +5,7 @@ using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; +using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -51,7 +52,7 @@ namespace PolyChat public void OnSendMessage(object sender = null, RoutedEventArgs e = null) { selectedPartner.AddMessage(new ChatMessage(username, "message" , inputSend.Text)); - Controller.SendMessage(selectedPartner.Code, inputSend.Text); + Controller.SendMessage(selectedPartner.Code, "message", inputSend.Text); // clear input inputSend.Text = ""; } @@ -89,12 +90,16 @@ namespace PolyChat /// Adds a new ChatPartner to the UI with default Name. /// /// IP Adress, gets shown as Util.IP > Code - public void OnIncomingConnection(string ip) + public async void OnIncomingConnection(string ip) { - Partners.Add(new ChatPartner( - "Connecting...", - ip - )); + await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => + { + Partners.Add(new ChatPartner( + "Connecting...", + ip + )); + updateNoChatsPlaceholder(); + }); } /// /// Adds an message to the UI, based on .sender if known diff --git a/PolyChat/Models/Connection.cs b/PolyChat/Models/Connection.cs index f214739..6700330 100644 --- a/PolyChat/Models/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -35,7 +35,7 @@ namespace PolyChat.Models Socket = socket; Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect); Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); - Socket.On("message", (Action)onMessage); + Socket.On("message", (Action) onMessage); //we are connected if we got here, inital packet was already received Connected = true;