Add project files.
This commit is contained in:
23
PolyChat/Models/ChatMessage.cs
Normal file
23
PolyChat/Models/ChatMessage.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
public class ChatMessage
|
||||
{
|
||||
public string Text;
|
||||
public string Date;
|
||||
public bool Foreign;
|
||||
|
||||
public ChatMessage(string text, string date, bool foreign)
|
||||
{
|
||||
Text = text;
|
||||
Date = date;
|
||||
Foreign = foreign;
|
||||
}
|
||||
|
||||
override
|
||||
public string ToString()
|
||||
{
|
||||
string prefix = Foreign ? "Other" : "Me";
|
||||
return $"{prefix}: Text";
|
||||
}
|
||||
}
|
||||
}
|
||||
28
PolyChat/Models/ChatPartner.cs
Normal file
28
PolyChat/Models/ChatPartner.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using SocketIOSharp.Server.Client;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
public class ChatPartner
|
||||
{
|
||||
public string Name;
|
||||
public string Code;
|
||||
public ObservableCollection<ChatMessage> Messages;
|
||||
private SocketIOSocket socketIOSocket;
|
||||
|
||||
public ChatPartner(string name, string code, ObservableCollection<ChatMessage> messages = null)
|
||||
{
|
||||
Name = name;
|
||||
Code = code;
|
||||
if (messages == null) Messages = new ObservableCollection<ChatMessage>();
|
||||
else Messages = messages;
|
||||
}
|
||||
|
||||
public SocketIOSocket SocketIOSocket { get => socketIOSocket; set => socketIOSocket = value; }
|
||||
|
||||
public void AddMessage(ChatMessage message)
|
||||
{
|
||||
Messages.Add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
110
PolyChat/Models/Client.cs
Normal file
110
PolyChat/Models/Client.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
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;
|
||||
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
|
||||
{
|
||||
class Client
|
||||
{
|
||||
private SocketIOClient connection;
|
||||
public Boolean isConnected = false;
|
||||
private List<MSG> msgStack = new List<MSG>();
|
||||
private Boolean active = true;
|
||||
private String ip;
|
||||
|
||||
public Client(SocketIOClient connection, String ip)
|
||||
{
|
||||
this.ip = ip;
|
||||
this.connection = connection;
|
||||
InitEventHandlers(this, connection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// converts String message into json file and sends it to the server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// gets called by gui if someone wants to send Message
|
||||
/// </remarks>
|
||||
/// <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)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
//create msg
|
||||
MSG msg = new MSG(sender, Controller.ip, chatMessage, timestamp);
|
||||
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
|
||||
//send msg
|
||||
connection.Emit(code.ToString(), petJson);
|
||||
}).Start();
|
||||
}
|
||||
|
||||
/*
|
||||
private void recieveMessage(String msg)
|
||||
{
|
||||
// deserialize json string
|
||||
MSG pet = JsonNet.Deserialize<MSG>(msg);
|
||||
|
||||
//TODO: send message to GUI
|
||||
}
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of client server communiation
|
||||
/// </summary>
|
||||
/// <param name="client">self</param>
|
||||
/// <param name="connection"></param>
|
||||
private static void InitEventHandlers(Client client, SocketIOClient connection)
|
||||
{
|
||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||
{
|
||||
MSG pet = JsonNet.Deserialize<MSG>(BitConverter.ToString(Data[0].ToObject<byte[]>()));
|
||||
//TODO: send message to GUI
|
||||
});
|
||||
connection.On(SendCode.Command.ToString(), (Data) =>
|
||||
{
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
public String getIP()
|
||||
{
|
||||
return this.ip;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
53
PolyChat/Models/ClientHandler.cs
Normal file
53
PolyChat/Models/ClientHandler.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
13
PolyChat/Models/Exceptions/ConnectionFailedException.cs
Normal file
13
PolyChat/Models/Exceptions/ConnectionFailedException.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PolyChat.Models.Exceptions
|
||||
{
|
||||
public class ConnectionFailedException : Exception
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
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(String sender, IPAddress ip, String msg, DateTime timestamp)
|
||||
{
|
||||
this.sender = sender;
|
||||
this.ip = ip;
|
||||
this.timestamp = timestamp;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
PolyChat/Models/SendCode.cs
Normal file
10
PolyChat/Models/SendCode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
enum SendCode
|
||||
{
|
||||
Message,
|
||||
Command,
|
||||
test1,
|
||||
test2
|
||||
}
|
||||
}
|
||||
53
PolyChat/Models/Socket.cs
Normal file
53
PolyChat/Models/Socket.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Json.Net;
|
||||
using SocketIOSharp.Common;
|
||||
using SocketIOSharp.Server;
|
||||
using SocketIOSharp.Server.Client;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
class Socket
|
||||
{
|
||||
private Controller p;
|
||||
private readonly ushort Port;
|
||||
private SocketIOServer Server;
|
||||
private List<SocketIOSocket> Sockets = new List<SocketIOSocket>();
|
||||
|
||||
/// <summary>
|
||||
/// creates server on specified port
|
||||
/// </summary>
|
||||
/// <param name="Port"></param>
|
||||
public Socket(Controller p, ushort Port = 8050)
|
||||
{
|
||||
this.Port = Port;
|
||||
this.p = p;
|
||||
Server = new SocketIOServer(new SocketIOServerOption(Port));
|
||||
Server.OnConnection((socket) => OnConnect(socket));
|
||||
Server.Start();
|
||||
Console.WriteLine($"Server started, binding to port {Port}, waiting for connection...");
|
||||
}
|
||||
|
||||
private void OnConnect(SocketIOSocket socket)
|
||||
{
|
||||
Console.WriteLine($"{socket.GetHashCode()} connected to the server");
|
||||
Sockets.Add(socket);
|
||||
socket.On(SocketIOEvent.DISCONNECT, () => OnDisconnect(socket));
|
||||
socket.On("Message", (Data) => p.OnMessageCallback(socket, Data));
|
||||
}
|
||||
|
||||
private void OnDisconnect(SocketIOSocket socket)
|
||||
{
|
||||
Sockets.Remove(socket);
|
||||
}
|
||||
|
||||
public bool SendMessage(SocketIOSocket socket)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user