diff --git a/PolyChat/Connection.cs b/PolyChat/Connection.cs index f32592c..1fe0366 100644 --- a/PolyChat/Connection.cs +++ b/PolyChat/Connection.cs @@ -4,8 +4,8 @@ using EngineIOSharp.Common.Enum; using Newtonsoft.Json.Linq; using SocketIOSharp.Client; using SocketIOSharp.Common; -using SocketIOSharp.Server; using SocketIOSharp.Server.Client; +using PolyChat.Models; namespace PolyChat { @@ -14,10 +14,12 @@ namespace PolyChat private SocketIOClient Client; private SocketIOSocket Socket; private bool Connected = false; + private readonly string IP; public Connection(string ip, ushort port, Action onMessage) { Debug.WriteLine("! CONNECTING TO SERVER !"); + IP = ip; // establish connection Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port)); Client.Connect(); @@ -28,37 +30,15 @@ namespace PolyChat Client.On("message", (Action) onMessage); } - public Connection(ushort port, Action onMessage) + public Connection(SocketIOSocket socket, 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)); - }); + Socket = socket; + Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect); + Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data)); + Socket.On("message", (Action)onMessage); + + //we are connected if we got here, inital packet was already received + Connected = true; } public void SendMessage(string message) { @@ -74,6 +54,8 @@ namespace PolyChat private void OnConnect() { + Debug.WriteLine("--- Sending initial packet to server ---"); + Client.Emit("initial", IP); Debug.WriteLine("--- Connection successfull ---"); Connected = true; } @@ -81,6 +63,7 @@ namespace PolyChat { Debug.WriteLine("--- Disconnected! ---"); Connected = false; + Close(); } private void OnError(JToken[] data) { @@ -90,10 +73,17 @@ namespace PolyChat else Debug.WriteLine("Unkown Error"); Debug.WriteLine("---"); + Close(); } // Getters + public void Close() + { + if (Client != null) Client.Close(); + if (Socket != null) Socket.Close(); + } + public bool IsConnected() { return Connected; diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index d377df8..b973258 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -2,6 +2,8 @@ using System.Diagnostics; using Newtonsoft.Json.Linq; using System.Net; +using SocketIOSharp.Server; +using SocketIOSharp.Server.Client; namespace PolyChat { @@ -30,13 +32,31 @@ namespace PolyChat public void Connect(string ip) { Debug.WriteLine("--- Controller.Connect ---"); - Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(Data))); + new Connection(ip, PORT, Data => OnMessage(Data)); } private void Serve() { - Debug.WriteLine("--- Controller.Serve ---"); - Connections.Add("unknownIP", new Connection(PORT, Data => OnMessage(Data))); + 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) => + { + Debug.WriteLine("--- Client connected! ---"); + // setup event listeners + socket.On("initial", (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))); + }); + }); } public void SendMessage(string ip, string message)