New Connection Class replacing Client, Controller Class replaceing networkController, no json support yet

This commit is contained in:
Patrick Hellebrand
2021-09-22 12:58:05 +02:00
parent 46cc5b1d10
commit 01a48c336d
5 changed files with 213 additions and 16 deletions

102
PolyChat/Connection.cs Normal file
View File

@@ -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<JToken[]> 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<JToken[]>) onMessage);
}
public Connection(ushort port, Action<JToken[]> 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;
}
}
}

73
PolyChat/Controller.cs Normal file
View File

@@ -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<string, Connection> Connections = new Dictionary<string, Connection>();
private string OwnName = "";
private string OwnIP;
/// <summary>
/// Initializes Controller with UI access
/// </summary>
/// <param name="uiController">UWP UI Controller</param>
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;
}
}
}

View File

@@ -17,7 +17,7 @@ namespace PolyChat
/// </summary>
public sealed partial class MainPage : Page
{
private NetworkingController networkingController;
private Controller Controller;
private ObservableCollection<ChatPartner> 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<ChatPartner>();
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();
}
/// <summary>
/// Adds a new ChatPartner to the UI with default Name.
/// </summary>
/// <param name="ip">IP Adress, gets shown as Util.IP > Code</param>
public void OnIncomingConnection(string ip)
{
Partners.Add(new ChatPartner(
"Connecting...",
ip
));
}
/// <summary>
/// Adds an message to the UI, based on .sender if known
/// </summary>
/// <param name="message">ChatMessage</param>
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)

View File

@@ -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">
<Identity
Name="ee2ef4f2-e61b-497a-8f0e-9fa7c90234b1"
@@ -44,6 +45,7 @@
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<Capability Name="internetClientServer"/>
<Capability Name="privateNetworkClientServer"/>
</Capabilities>
</Package>

View File

@@ -119,6 +119,8 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Connection.cs" />
<Compile Include="Controller.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
@@ -185,6 +187,9 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.12</Version>
</PackageReference>
<PackageReference Include="SocketIoClientDotNet">
<Version>0.9.13</Version>
</PackageReference>
<PackageReference Include="SocketIOSharp">
<Version>2.0.3</Version>
</PackageReference>