del Controller and ClientHandler / msg send to ui
This commit is contained in:
@@ -16,18 +16,18 @@ namespace PolyChat.Models
|
||||
private Boolean connected = true;
|
||||
private String ipSelf;
|
||||
|
||||
public Client(SocketIOClient connection, String ip)
|
||||
public Client(SocketIOClient connection, String ip, MainPage uiController)
|
||||
{
|
||||
this.ipSelf = ip;
|
||||
this.connection_client = connection;
|
||||
InitEventHandlers(this, connection);
|
||||
InitEventHandlers(this, connection, uiController);
|
||||
}
|
||||
|
||||
public Client(SocketIOSocket connection, String ip)
|
||||
public Client(SocketIOSocket connection, String ip, MainPage uiController)
|
||||
{
|
||||
this.ipSelf = ip;
|
||||
this.connection_server = connection;
|
||||
InitEventHandlers(this, connection);
|
||||
InitEventHandlers(this, connection, uiController);
|
||||
}
|
||||
|
||||
//Sending
|
||||
@@ -47,7 +47,7 @@ namespace PolyChat.Models
|
||||
{
|
||||
Debug.WriteLine($"connected is {connected}");
|
||||
//create msg
|
||||
Message msg = new Message(chatMessage, false, Controller.ip);
|
||||
Message msg = new Message(chatMessage, false, ipSelf);
|
||||
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
@@ -73,48 +73,30 @@ namespace PolyChat.Models
|
||||
}
|
||||
}).Start();
|
||||
}
|
||||
/*
|
||||
/// <summary>
|
||||
/// Sends Message with new name
|
||||
/// </summary>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="nameChange"></param>
|
||||
/// <param name="timestamp"></param>
|
||||
public void sendNameChange(SendCode code, String nameChange)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
//create msg
|
||||
Message msg = new Message( Controller.ip);
|
||||
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
|
||||
//send msg
|
||||
connection_client.Emit(code.ToString(), petJson);
|
||||
}).Start();
|
||||
}
|
||||
*/
|
||||
|
||||
//==================================================================================
|
||||
//EventHandeling
|
||||
//===================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of client to server communiation
|
||||
/// handles all events of client
|
||||
/// </summary>
|
||||
/// <param name="client">self</param>
|
||||
/// <param name="connection"></param>
|
||||
private static void InitEventHandlers(Client client, SocketIOClient connection)
|
||||
private static void InitEventHandlers(Client client, SocketIOClient connection, MainPage uiController)
|
||||
{
|
||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||
{
|
||||
Message pet = new Message(Data[0]);
|
||||
Message msg = new Message(Data[0]);
|
||||
uiController.OnIncomingMessage(msg);
|
||||
|
||||
//TODO: send message to GUI
|
||||
});
|
||||
|
||||
connection.On(SendCode.Command.ToString(), (Data) =>
|
||||
{
|
||||
Console.WriteLine("Command recieved!" + Data[0]);
|
||||
|
||||
});
|
||||
|
||||
connection.On(SocketIOEvent.CONNECTION, () =>
|
||||
@@ -124,15 +106,16 @@ namespace PolyChat.Models
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of server to client communiation
|
||||
/// handles all events of server
|
||||
/// </summary>
|
||||
/// <param name="client">self</param>
|
||||
/// <param name="connection"></param>
|
||||
private static void InitEventHandlers(Client client, SocketIOSocket connection)
|
||||
private static void InitEventHandlers(Client client, SocketIOSocket connection, MainPage uiController)
|
||||
{
|
||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||
{
|
||||
Message pet = new Message(Data[0]);
|
||||
Message msg = new Message(Data[0]);
|
||||
uiController.OnIncomingMessage(msg);
|
||||
//TODO: send message to GUI
|
||||
});
|
||||
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SocketIOSharp.Common;
|
||||
using SocketIOSharp.Server;
|
||||
using SocketIOSharp.Client;
|
||||
using SocketIOSharp.Server.Client;
|
||||
using EngineIOSharp.Common.Enum;
|
||||
using Json.Net;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
class ClientHandler
|
||||
{
|
||||
public List<Client> clients = new List<Client>();
|
||||
public ClientHandler()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// connects new clients and saves them in list
|
||||
/// </summary>
|
||||
/// <param name="clientCode">ip adress of parter</param>
|
||||
public void connectNewClient(String clientCode)
|
||||
{
|
||||
//Todo: convert code into ip
|
||||
|
||||
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, clientCode, 8050));
|
||||
connection.Connect();
|
||||
clients.Add(new Client(connection, clientCode));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns client that fits to ip adress
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
public Client getClient(String ip)
|
||||
{
|
||||
foreach (Client cl in clients)
|
||||
{
|
||||
if (cl.getIP().Equals(ip))
|
||||
{
|
||||
return cl;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,11 +27,7 @@ namespace PolyChat.Models
|
||||
this.uiController = uiController;
|
||||
this.Port = Port;
|
||||
ownIP = getIP();
|
||||
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||
Server.OnConnection((socket) => connectNewClient(socket));
|
||||
Server.Start();
|
||||
Debug.WriteLine($"Your ip is: {ownIP}");
|
||||
Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
||||
startServer();
|
||||
}
|
||||
|
||||
//EXTERNAL METHODS
|
||||
@@ -45,7 +41,7 @@ namespace PolyChat.Models
|
||||
{
|
||||
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050));
|
||||
connection.Connect();
|
||||
clients.Add(new Client(connection, ip));
|
||||
clients.Add(new Client(connection, ip, uiController));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,8 +52,9 @@ namespace PolyChat.Models
|
||||
{
|
||||
socket.On(SendCode.Initial.ToString(), (JToken[] Data) =>
|
||||
{
|
||||
Debug.WriteLine("Client connected!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
Message m = new Message(Data[0]);
|
||||
clients.Add(new Client(socket,m.Ip));
|
||||
clients.Add(new Client(socket,m.Ip, uiController));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,24 +87,23 @@ namespace PolyChat.Models
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// changes name of self and sends new name to all chats
|
||||
/// </summary>
|
||||
/// <param name="newName"></param>
|
||||
public void changeName(String newName)
|
||||
public MainPage getUIController()
|
||||
{
|
||||
this.ownName = newName;
|
||||
foreach(Client cl in clients)
|
||||
{
|
||||
cl.sendNameChange(SendCode.NameChange, newName);
|
||||
}
|
||||
return this.uiController;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
//=========================================================================================================================================================================================
|
||||
//INTERNAL METHODS
|
||||
//=========================================================================================================================================================================================
|
||||
private void startServer()
|
||||
{
|
||||
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||
Server.OnConnection((socket) => connectNewClient(socket));
|
||||
Server.Start();
|
||||
Debug.WriteLine($"Your ip is: {ownIP}");
|
||||
Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns client that fit to ip address
|
||||
|
||||
Reference in New Issue
Block a user