merge
This commit is contained in:
@@ -16,6 +16,7 @@ namespace PolyChat
|
||||
private MainPage UIController;
|
||||
|
||||
private ClientHandler clientHandler;
|
||||
/*
|
||||
public Controller(MainPage uiController)
|
||||
{
|
||||
UIController = uiController;
|
||||
@@ -30,9 +31,9 @@ namespace PolyChat
|
||||
|
||||
public void sendMessage(String ip, String name, String msg)
|
||||
{
|
||||
clientHandler.getClient(ip).sendMessage(SendCode.Message, name, msg, DateTime.Now);
|
||||
clientHandler.getClient(ip).sendMessage(SendCode.Message, msg, DateTime.Now);
|
||||
}
|
||||
|
||||
*/
|
||||
/// <summary>
|
||||
/// prints out ip. on server side automatticaly finds 10.... ip (which is the correct one)
|
||||
/// </summary>
|
||||
|
||||
@@ -16,13 +16,13 @@ namespace PolyChat
|
||||
/// </summary>
|
||||
public sealed partial class MainPage : Page
|
||||
{
|
||||
private Controller Controller;
|
||||
private NetworkingController networkingController;
|
||||
private ObservableCollection<ChatPartner> Partners;
|
||||
private ChatPartner selectedPartner;
|
||||
public MainPage()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
Controller = new Controller(this);
|
||||
networkingController = new NetworkingController(this);
|
||||
|
||||
Partners = new ObservableCollection<ChatPartner>();
|
||||
}
|
||||
@@ -38,11 +38,10 @@ namespace PolyChat
|
||||
public void OnSendMessage(object sender = null, RoutedEventArgs e = null)
|
||||
{
|
||||
selectedPartner.AddMessage(new ChatMessage(
|
||||
DateTime.Now,
|
||||
inputSend.Text,
|
||||
false
|
||||
));
|
||||
Controller.sendMessage(selectedPartner.Code, inputUsername.Text, inputSend.Text);
|
||||
networkingController.sendMessage(selectedPartner.Code, inputSend.Text);
|
||||
// clear input
|
||||
inputSend.Text = "";
|
||||
}
|
||||
@@ -54,7 +53,7 @@ namespace PolyChat
|
||||
if (result == ContentDialogResult.Primary)
|
||||
{
|
||||
string ip = dialog.getText();
|
||||
Controller.Connect(ip);
|
||||
networkingController.connectNewClient(ip);
|
||||
Partners.Add(new ChatPartner(
|
||||
"NO NAME",
|
||||
ip
|
||||
@@ -66,7 +65,6 @@ namespace PolyChat
|
||||
{
|
||||
ChatPartner sendingPartner = Partners.First(p => p.Code == message.Ip);
|
||||
sendingPartner.AddMessage(new ChatMessage(
|
||||
message.Timestamp,
|
||||
message.Msg,
|
||||
true,
|
||||
message.Sender
|
||||
|
||||
@@ -11,16 +11,18 @@ namespace PolyChat.Models
|
||||
public readonly bool Foreign;
|
||||
public readonly string StringTimeStamp;
|
||||
|
||||
public ChatMessage(DateTime Timestamp, string Msg, bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1")
|
||||
public ChatMessage(string Msg = "", bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1")
|
||||
{
|
||||
this.Sender = Sender;
|
||||
this.Timestamp = Timestamp;
|
||||
this.Timestamp = DateTime.Now;
|
||||
StringTimeStamp = Timestamp.ToString();
|
||||
this.Msg = Msg;
|
||||
this.Foreign = Foreign;
|
||||
this.Ip = Ip;
|
||||
}
|
||||
|
||||
|
||||
|
||||
override
|
||||
public string ToString()
|
||||
{
|
||||
|
||||
@@ -10,12 +10,7 @@ using SocketIOSharp.Server.Client;
|
||||
using EngineIOSharp.Common.Enum;
|
||||
using Json.Net;
|
||||
using System.Net;
|
||||
using SocketIOSharp.Client;
|
||||
using SocketIOSharp.Common;
|
||||
using SocketIOSharp.Common.Packet;
|
||||
using System;
|
||||
using System.Net;
|
||||
using EngineIOSharp.Common.Enum;
|
||||
using System.Threading;
|
||||
|
||||
namespace PolyChat.Models
|
||||
@@ -23,18 +18,18 @@ namespace PolyChat.Models
|
||||
class Client
|
||||
{
|
||||
private SocketIOClient connection;
|
||||
public Boolean isConnected = false;
|
||||
private List<ChatMessage> msgStack = new List<ChatMessage>();
|
||||
private Boolean active = true;
|
||||
private String ip;
|
||||
private Boolean connected = false;
|
||||
private String ipSelf;
|
||||
|
||||
public Client(SocketIOClient connection, String ip)
|
||||
{
|
||||
this.ip = ip;
|
||||
this.ipSelf = ip;
|
||||
this.connection = connection;
|
||||
InitEventHandlers(this, connection);
|
||||
}
|
||||
|
||||
//Sending
|
||||
//===================================================================================
|
||||
/// <summary>
|
||||
/// converts String message into json file and sends it to the server.
|
||||
/// </summary>
|
||||
@@ -44,12 +39,12 @@ namespace PolyChat.Models
|
||||
/// <param name="sender">Sender of Message</param>
|
||||
/// <param name="chatMessage">the accual text the user wants to send</param>
|
||||
/// <param name="timestamp">current time</param>
|
||||
public void sendMessage(SendCode code, String sender, String chatMessage, DateTime timestamp)
|
||||
public void sendMessage(SendCode code, String chatMessage)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
//create msg
|
||||
ChatMessage msg = new ChatMessage(timestamp, chatMessage, false, sender, Controller.ip);
|
||||
ChatMessage msg = new ChatMessage(chatMessage, false, Controller.ip);
|
||||
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
@@ -59,16 +54,31 @@ namespace PolyChat.Models
|
||||
}).Start();
|
||||
}
|
||||
|
||||
/*
|
||||
private void recieveMessage(String msg)
|
||||
/// <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)
|
||||
{
|
||||
// deserialize json string
|
||||
MSG pet = JsonNet.Deserialize<MSG>(msg);
|
||||
new Thread(() =>
|
||||
{
|
||||
//create msg
|
||||
ChatMessage msg = new ChatMessage( Controller.ip);
|
||||
|
||||
//TODO: send message to GUI
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
|
||||
//send msg
|
||||
connection.Emit(code.ToString(), petJson);
|
||||
}).Start();
|
||||
}
|
||||
*/
|
||||
|
||||
//==================================================================================
|
||||
//EventHandeling
|
||||
//===================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of client server communiation
|
||||
/// </summary>
|
||||
@@ -85,25 +95,24 @@ namespace PolyChat.Models
|
||||
{
|
||||
Console.WriteLine("Command recieved!" + Data[0]);
|
||||
});
|
||||
connection.On(SendCode.test1.ToString(), (Data) =>
|
||||
{
|
||||
Console.WriteLine("test1 recieved!" + Data[0]);
|
||||
});
|
||||
connection.On(SendCode.test2.ToString(), (Data) =>
|
||||
{
|
||||
Console.WriteLine("test2 recieved!" + Data[0]);
|
||||
});
|
||||
|
||||
connection.On(SocketIOEvent.CONNECTION, () =>
|
||||
{
|
||||
Console.WriteLine("Connected!");
|
||||
client.isConnected = true;
|
||||
client.connected = true;
|
||||
});
|
||||
}
|
||||
//==================================================================================
|
||||
//Getter and Setter
|
||||
//==================================================================================
|
||||
|
||||
public String getIP()
|
||||
{
|
||||
return this.ip;
|
||||
return this.ipSelf;
|
||||
}
|
||||
|
||||
public Boolean isConnected()
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
29
PolyChat/Models/MSG.cs
Normal file
29
PolyChat/Models/MSG.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// dumy class for json converter
|
||||
/// </summary>
|
||||
public class MSG
|
||||
{
|
||||
public String sender = "unknown";
|
||||
public DateTime timestamp = new DateTime(2000, 01, 01);
|
||||
public String msg = "empty";
|
||||
public IPAddress ip = new IPAddress(new byte[] { 49,48,46,49,46,50,49,49,46,50,54 });
|
||||
|
||||
|
||||
public MSG(IPAddress ip, String msg, DateTime timestamp)
|
||||
{
|
||||
this.sender = sender;
|
||||
this.ip = ip;
|
||||
this.timestamp = timestamp;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
103
PolyChat/Models/NetworkingController.cs
Normal file
103
PolyChat/Models/NetworkingController.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SocketIOSharp.Client;
|
||||
using EngineIOSharp.Common.Enum;
|
||||
using System.Net;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
class NetworkingController
|
||||
{
|
||||
public List<Client> clients = new List<Client>();
|
||||
private String ownName = "";
|
||||
private IPAddress ownIP;
|
||||
MainPage uiController;
|
||||
|
||||
public NetworkingController (MainPage uiController)
|
||||
{
|
||||
this.uiController = uiController;
|
||||
this.ownIP = getIP();
|
||||
}
|
||||
|
||||
//EXTERNAL METHODS
|
||||
//=================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// connects self to server with given ip
|
||||
/// </summary>
|
||||
/// <param name="ip"> server to connect to </param>
|
||||
public void connectNewClient(String ip)
|
||||
{
|
||||
SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050));
|
||||
connection.Connect();
|
||||
clients.Add(new Client(connection, ip));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// sends Message to given ip
|
||||
/// </summary>
|
||||
/// <param name="ip"> partner to send to </param>
|
||||
/// <param name="msg"> to send </param>
|
||||
public void sendMessage(String ip, String msg)
|
||||
{
|
||||
this.getClient(ip).sendMessage(SendCode.Message, msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// changes name of self and sends new name to all chats
|
||||
/// </summary>
|
||||
/// <param name="newName"></param>
|
||||
public void changeName(String newName)
|
||||
{
|
||||
this.ownName = newName;
|
||||
foreach(Client cl in clients)
|
||||
{
|
||||
cl.sendNameChange(SendCode.NameChange, newName);
|
||||
}
|
||||
}
|
||||
|
||||
//=================================================================================
|
||||
//INTERNAL METHODS
|
||||
//=================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// returns client that fits to ip adress
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
/// <returns></returns>
|
||||
private Client getClient(String ip)
|
||||
{
|
||||
foreach (Client cl in clients)
|
||||
{
|
||||
if (cl.getIP().Equals(ip))
|
||||
{
|
||||
return cl;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private IPAddress 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];
|
||||
//get ip as byte array
|
||||
/*
|
||||
byte[] ba = System.Text.Encoding.ASCII.GetBytes(addrList[i].ToString());
|
||||
foreach (var item in ba)
|
||||
{
|
||||
Console.Write(item.ToString() + ",");
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
{
|
||||
Message,
|
||||
Command,
|
||||
test1,
|
||||
test2
|
||||
NameChange,
|
||||
Initial
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace PolyChat.Models
|
||||
{
|
||||
private Controller p;
|
||||
private readonly ushort Port;
|
||||
private SocketIOServer Server;
|
||||
private SocketIOServer server;
|
||||
private List<SocketIOSocket> Sockets = new List<SocketIOSocket>();
|
||||
|
||||
/// <summary>
|
||||
@@ -25,9 +25,9 @@ namespace PolyChat.Models
|
||||
{
|
||||
this.Port = Port;
|
||||
this.p = p;
|
||||
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||
Server.OnConnection((socket) => OnConnect(socket));
|
||||
Server.Start();
|
||||
server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||
server.OnConnection((socket) => OnConnect(socket));
|
||||
server.Start();
|
||||
Console.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
||||
}
|
||||
|
||||
|
||||
@@ -124,6 +124,7 @@
|
||||
</Compile>
|
||||
<Compile Include="Models\ChatMessage.cs" />
|
||||
<Compile Include="Models\ChatPartner.cs" />
|
||||
<Compile Include="Models\NetworkingController.cs" />
|
||||
<Compile Include="Models\Client.cs" />
|
||||
<Compile Include="Models\ClientHandler.cs" />
|
||||
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
|
||||
|
||||
Reference in New Issue
Block a user