From 01a48c336d26034977fe003bb0d10c05a7eb0ca2 Mon Sep 17 00:00:00 2001 From: Patrick Hellebrand Date: Wed, 22 Sep 2021 12:58:05 +0200 Subject: [PATCH] New Connection Class replacing Client, Controller Class replaceing networkController, no json support yet --- PolyChat/Connection.cs | 102 ++++++++++++++++++++++++++++++++++ PolyChat/Controller.cs | 73 ++++++++++++++++++++++++ PolyChat/MainPage.xaml.cs | 43 +++++++++----- PolyChat/Package.appxmanifest | 6 +- PolyChat/PolyChat.csproj | 5 ++ 5 files changed, 213 insertions(+), 16 deletions(-) create mode 100644 PolyChat/Connection.cs create mode 100644 PolyChat/Controller.cs diff --git a/PolyChat/Connection.cs b/PolyChat/Connection.cs new file mode 100644 index 0000000..f32592c --- /dev/null +++ b/PolyChat/Connection.cs @@ -0,0 +1,102 @@ +using System; +using System.Diagnostics; +using EngineIOSharp.Common.Enum; +using Newtonsoft.Json.Linq; +using SocketIOSharp.Client; +using SocketIOSharp.Common; +using SocketIOSharp.Server; +using SocketIOSharp.Server.Client; + +namespace PolyChat +{ + public class Connection + { + private SocketIOClient Client; + private SocketIOSocket Socket; + private bool Connected = false; + + public Connection(string ip, ushort port, Action onMessage) + { + Debug.WriteLine("! CONNECTING TO SERVER !"); + // establish connection + Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port)); + Client.Connect(); + // setup event listeners + Client.On(SocketIOEvent.CONNECTION, OnConnect); + Client.On(SocketIOEvent.DISCONNECT, OnDisconnect); + Client.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); + Client.On("message", (Action) onMessage); + } + + public Connection(ushort port, Action onMessage) + { + Debug.WriteLine("! SERVER STARTING !"); + SocketIOServer server = new SocketIOServer(new SocketIOServerOption( + port + )); + server.Start(); + Debug.WriteLine("Port " + server.Option.Port); + Debug.WriteLine("Path " + server.Option.Path); + // listen for connection + server.OnConnection((SocketIOSocket socket) => + { + Console.WriteLine("--- Client connected! ---"); + Socket = socket; + Connected = true; + // setup event listeners + Socket.On("input", (JToken[] data) => + { + Debug.WriteLine("--- Incoming input ---"); + onMessage(data); + socket.Emit("echo", data); + }); + Socket.On("message", (JToken[] data) => + { + Debug.WriteLine("--- Incoming message ---"); + onMessage(data); + socket.Emit("echo", data); + }); + Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect); + Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); + }); + } + public void SendMessage(string message) + { + Debug.WriteLine("--- Sending message ---"); + Debug.WriteLine($"Connected {Connected}"); + Debug.WriteLine($"Client {Client}"); + Debug.WriteLine($"Socket {Socket}"); + if (Socket != null) Socket.Emit("message", message); + else if (Client != null) Client.Emit("message", message); + } + + // Event Methods + + private void OnConnect() + { + Debug.WriteLine("--- Connection successfull ---"); + Connected = true; + } + private void OnDisconnect() + { + Debug.WriteLine("--- Disconnected! ---"); + Connected = false; + } + private void OnError(JToken[] data) + { + Debug.WriteLine("--- Error: ---"); + if (data != null && data.Length > 0 && data[0] != null) + Debug.WriteLine(data[0]); + else + Debug.WriteLine("Unkown Error"); + Debug.WriteLine("---"); + } + + // Getters + + public bool IsConnected() + { + return Connected; + } + } +} diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs new file mode 100644 index 0000000..a26cdbd --- /dev/null +++ b/PolyChat/Controller.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.Diagnostics; +using Newtonsoft.Json.Linq; +using System.Net; + +namespace PolyChat +{ + class Controller + { + // Constants + private const ushort PORT = 8050; + // Controller + private readonly MainPage UIController; + // Props + private Dictionary Connections = new Dictionary(); + private string OwnName = ""; + private string OwnIP; + + /// + /// Initializes Controller with UI access + /// + /// UWP UI Controller + public Controller(MainPage uiController) + { + UIController = uiController; + OwnIP = getIP(); + Serve(); + } + + public void Connect(string ip) + { + Debug.WriteLine("--- Controller.Connect ---"); + Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(Data))); + } + + private void Serve() + { + Debug.WriteLine("--- Controller.Serve ---"); + Connections.Add("unknownIP", new Connection(PORT, Data => OnMessage(Data))); + } + + public void SendMessage(string ip, string message) + { + Debug.WriteLine("--- Controller.SendMessage ---"); + Connections[ip].SendMessage(message); + } + + private void OnMessage(JToken[] data) + { + Debug.WriteLine("--- Controller.OnMessage ---"); + if (data != null && data.Length > 0 && data[0] != null) + { + Debug.WriteLine("Message: " + data[0]); + } + else Debug.WriteLine("Undefined: " + data); + } + + public string getIP() + { + IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); + IPAddress[] addrList = ipEntry.AddressList; + + for (short i = 0; i < addrList.Length; i++) + { + if (addrList[i].ToString().Substring(0, 3).Equals("10.")) + { + return addrList[i].ToString(); + } + } + return null; + } + } +} diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 42f1ca2..ba84679 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -17,7 +17,7 @@ namespace PolyChat /// public sealed partial class MainPage : Page { - private NetworkingController networkingController; + private Controller Controller; private ObservableCollection Partners; private ChatPartner selectedPartner = null; private string username; @@ -25,9 +25,9 @@ namespace PolyChat { this.InitializeComponent(); // init controller - networkingController = new NetworkingController(this); + Controller = new Controller(this); // ui variables - ipAddress.Text = IP.GetCodeFromIP(networkingController.getIP().ToString()); + ipAddress.Text = IP.GetCodeFromIP(Controller.getIP()); Partners = new ObservableCollection(); updateNoChatsPlaceholder(); updateNoUsernamePlaceholder(); @@ -48,19 +48,10 @@ namespace PolyChat // EVENTS - public void OnChatPartnerSelected(object sender, RoutedEventArgs e) - { - string code = ((RadioButton)sender).Tag.ToString(); - selectedPartner = Partners.First(p => p.Code == code); - listViewMessages.ItemsSource = selectedPartner.Messages; - selectedPartnerName.Text = selectedPartner.Name; - updateNoChatSelected(); - } - public void OnSendMessage(object sender = null, RoutedEventArgs e = null) { selectedPartner.AddMessage(new Message(inputSend.Text,false)); - networkingController.sendMessage(selectedPartner.Code, inputSend.Text); + Controller.SendMessage(selectedPartner.Code, inputSend.Text); // clear input inputSend.Text = ""; } @@ -72,7 +63,7 @@ namespace PolyChat if (result == ContentDialogResult.Primary) { string ip = IP.GetIPfromCode(dialog.getValue()); - networkingController.connectNewClient(ip); + Controller.Connect(ip); Partners.Add(new ChatPartner( "Connecting...", ip @@ -94,6 +85,21 @@ namespace PolyChat updateNoUsernamePlaceholder(); } + /// + /// Adds a new ChatPartner to the UI with default Name. + /// + /// IP Adress, gets shown as Util.IP > Code + public void OnIncomingConnection(string ip) + { + Partners.Add(new ChatPartner( + "Connecting...", + ip + )); + } + /// + /// Adds an message to the UI, based on .sender if known + /// + /// ChatMessage public void OnIncomingMessage(Message message) { ChatPartner sendingPartner = Partners.First(p => p.Code == message.Ip); @@ -108,6 +114,15 @@ namespace PolyChat { Partners.Remove(selectedPartner); updateNoChatsPlaceholder(); + updateNoChatSelected(); + } + public void OnChatPartnerSelected(object sender, RoutedEventArgs e) + { + string code = ((RadioButton)sender).Tag.ToString(); + selectedPartner = Partners.First(p => p.Code == code); + listViewMessages.ItemsSource = selectedPartner.Messages; + selectedPartnerName.Text = selectedPartner.Name; + updateNoChatSelected(); } private void OnKeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) diff --git a/PolyChat/Package.appxmanifest b/PolyChat/Package.appxmanifest index d4a461b..06228f6 100644 --- a/PolyChat/Package.appxmanifest +++ b/PolyChat/Package.appxmanifest @@ -4,7 +4,8 @@ xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" - IgnorableNamespaces="uap mp"> + xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" + IgnorableNamespaces="uap mp uap3"> - + + \ No newline at end of file diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 3410aba..5b6c905 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -119,6 +119,8 @@ App.xaml + + MainPage.xaml @@ -185,6 +187,9 @@ 6.2.12 + + 0.9.13 + 2.0.3