Merged
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
using System.Diagnostics;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Net;
|
||||
using SocketIOSharp.Server;
|
||||
using SocketIOSharp.Server.Client;
|
||||
using PolyChat.Models;
|
||||
|
||||
namespace PolyChat
|
||||
@@ -31,13 +33,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)
|
||||
|
||||
@@ -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.Models
|
||||
{
|
||||
@@ -14,10 +14,12 @@ namespace PolyChat.Models
|
||||
private SocketIOClient Client;
|
||||
private SocketIOSocket Socket;
|
||||
private bool Connected = false;
|
||||
private readonly string IP;
|
||||
|
||||
public Connection(string ip, ushort port, Action<JToken[]> 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.Models
|
||||
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;
|
||||
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.On("message", (Action<JToken[]>)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.Models
|
||||
|
||||
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.Models
|
||||
{
|
||||
Debug.WriteLine("--- Disconnected! ---");
|
||||
Connected = false;
|
||||
Close();
|
||||
}
|
||||
private void OnError(JToken[] data)
|
||||
{
|
||||
@@ -90,10 +73,17 @@ namespace PolyChat.Models
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user