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