Compare commits
5 Commits
c00e4c6a01
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e75385a69b | ||
|
|
f5030c55c5 | ||
|
|
2718864b5c | ||
|
|
61cbd7fe74 | ||
|
|
08325b1c65 |
156
PolyChat/Models/Client.cs
Normal file
156
PolyChat/Models/Client.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using SocketIOSharp.Common;
|
||||
using SocketIOSharp.Client;
|
||||
using SocketIOSharp.Server.Client;
|
||||
using Json.Net;
|
||||
using System.Threading;
|
||||
using PolyChat.Models.Exceptions;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
class Client
|
||||
{
|
||||
private SocketIOClient connection_client = null;
|
||||
private SocketIOSocket connection_server = null;
|
||||
private Boolean connected = true;
|
||||
private String ipSelf;
|
||||
|
||||
public Client(SocketIOClient connection, String ip, MainPage uiController)
|
||||
{
|
||||
this.ipSelf = ip;
|
||||
this.connection_client = connection;
|
||||
InitEventHandlers(this, connection, uiController);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when socket accepts client
|
||||
/// </summary>
|
||||
/// <param name="connection"></param>
|
||||
/// <param name="ip"></param>
|
||||
/// <param name="uiController"></param>
|
||||
public Client(SocketIOSocket connection, String ip, MainPage uiController)
|
||||
{
|
||||
Debug.WriteLine("New Client Saved!!!!(Clinent[34])");
|
||||
this.ipSelf = ip;
|
||||
this.connection_server = connection;
|
||||
InitEventHandlers(this, connection, uiController);
|
||||
}
|
||||
|
||||
//Sending
|
||||
//===================================================================================
|
||||
/// <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 chatMessage)
|
||||
{
|
||||
new Thread(() =>
|
||||
{
|
||||
Debug.WriteLine($"connected is {connected}");
|
||||
//create msg
|
||||
Message msg = new Message(chatMessage, false, ipSelf);
|
||||
|
||||
//convert msg
|
||||
String petJson = JsonNet.Serialize(msg);
|
||||
|
||||
//wait if not connected and send msg
|
||||
int i=0;
|
||||
int sleeptimer = 2000;
|
||||
while(!this.connected)
|
||||
{
|
||||
Thread.Sleep(sleeptimer);
|
||||
i++;
|
||||
if(i>=10)
|
||||
{
|
||||
throw new MessageTimedOutException(i*sleeptimer);
|
||||
}
|
||||
}
|
||||
if (connection_client != null)
|
||||
{
|
||||
connection_client.Emit(code.ToString(), petJson);
|
||||
}else if (connection_server != null)
|
||||
{
|
||||
connection_server.Emit(code.ToString(), petJson);
|
||||
}
|
||||
}).Start();
|
||||
}
|
||||
|
||||
//==================================================================================
|
||||
//EventHandeling
|
||||
//===================================================================================
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of client
|
||||
/// </summary>
|
||||
/// <param name="client">self</param>
|
||||
/// <param name="connection"></param>
|
||||
private static void InitEventHandlers(Client client, SocketIOClient connection, MainPage uiController)
|
||||
{
|
||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||
{
|
||||
//Message msg = new Message(Data[0]);
|
||||
Debug.WriteLine("Normal Message Recieved!!!!");
|
||||
Message msg = JsonNet.Deserialize<Message>(Data[0].ToString());
|
||||
uiController.OnIncomingMessage(msg);
|
||||
|
||||
//TODO: send message to GUI
|
||||
});
|
||||
|
||||
connection.On(SendCode.Command.ToString(), (Data) =>
|
||||
{
|
||||
Console.WriteLine("Command recieved!" + Data[0]);
|
||||
|
||||
});
|
||||
|
||||
connection.On(SocketIOEvent.CONNECTION, () =>
|
||||
{
|
||||
|
||||
client.connected = true;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// handles all events of server
|
||||
/// </summary>
|
||||
/// <param name="client">self</param>
|
||||
/// <param name="connection"></param>
|
||||
private static void InitEventHandlers(Client client, SocketIOSocket connection, MainPage uiController)
|
||||
{
|
||||
connection.On(SendCode.Message.ToString(), (Data) =>
|
||||
{
|
||||
Debug.WriteLine("Normal Message Recieved!!!!");
|
||||
Message msg = JsonNet.Deserialize<Message>(Data[0].ToString());
|
||||
//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]);
|
||||
});
|
||||
|
||||
client.connected = true;
|
||||
}
|
||||
//==================================================================================
|
||||
//Getter and Setter
|
||||
//==================================================================================
|
||||
|
||||
public String getIP()
|
||||
{
|
||||
return this.ipSelf;
|
||||
}
|
||||
|
||||
public Boolean isConnected()
|
||||
{
|
||||
return this.connected;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
62
PolyChat/Models/Message.cs
Normal file
62
PolyChat/Models/Message.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Json.Net;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
public class Message
|
||||
{
|
||||
public readonly string Sender;
|
||||
public readonly DateTime Timestamp = new DateTime(1970, 01, 01);
|
||||
public readonly string Msg = "empty";
|
||||
public readonly string Ip;
|
||||
public readonly bool Foreign;
|
||||
public readonly string StringTimeStamp;
|
||||
|
||||
/// <summary>
|
||||
/// create new Message object from parameters
|
||||
/// </summary>
|
||||
/// <param name="Msg"></param>
|
||||
/// <param name="Foreign"></param>
|
||||
/// <param name="Sender"></param>
|
||||
/// <param name="Ip"></param>
|
||||
public Message(string Msg = "", bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1")
|
||||
{
|
||||
this.Sender = Sender;
|
||||
this.Timestamp = DateTime.Now;
|
||||
StringTimeStamp = Timestamp.ToString();
|
||||
this.Msg = Msg;
|
||||
this.Foreign = Foreign;
|
||||
this.Ip = Ip;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// create new Message object from JToken (json)
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public Message(JToken data)
|
||||
{
|
||||
|
||||
Message m = (Message) data.ToObject<Message>();
|
||||
Sender = m.Sender;
|
||||
Timestamp = m.Timestamp;
|
||||
StringTimeStamp = Timestamp.ToString();
|
||||
Msg = m.Msg;
|
||||
Ip = m.Ip;
|
||||
Foreign = m.Foreign;
|
||||
}
|
||||
|
||||
public Message()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
override
|
||||
public string ToString()
|
||||
{
|
||||
string prefix = Foreign ? "Other" : "Me";
|
||||
return $"{prefix}: {Msg}({Sender})";
|
||||
}
|
||||
}
|
||||
}
|
||||
127
PolyChat/Models/NetworkingController.cs
Normal file
127
PolyChat/Models/NetworkingController.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using SocketIOSharp.Client;
|
||||
using EngineIOSharp.Common.Enum;
|
||||
using System.Net;
|
||||
using PolyChat.Models.Exceptions;
|
||||
|
||||
//dependencies for server functionality
|
||||
using SocketIOSharp.Server;
|
||||
using SocketIOSharp.Server.Client;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Threading;
|
||||
using Json.Net;
|
||||
|
||||
namespace PolyChat.Models
|
||||
{
|
||||
class NetworkingController
|
||||
{
|
||||
public List<Client> clients = new List<Client>();
|
||||
private String ownName = "";
|
||||
private IPAddress ownIP;
|
||||
private readonly ushort Port;
|
||||
private SocketIOServer Server;
|
||||
private readonly MainPage uiController;
|
||||
|
||||
public NetworkingController (MainPage uiController, ushort Port = 8050)
|
||||
{
|
||||
this.uiController = uiController;
|
||||
this.Port = Port;
|
||||
ownIP = getIP();
|
||||
startServer();
|
||||
}
|
||||
|
||||
//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, uiController));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// handle incomming connection
|
||||
/// </summary>
|
||||
/// <param name="ip"> server to connect to </param>
|
||||
private void connectNewClient(SocketIOSocket socket)
|
||||
{
|
||||
socket.On(SendCode.Initial.ToString(), (JToken[] Data) =>
|
||||
{
|
||||
Debug.WriteLine("Client connected and Initial Message Recieved!!!!!!!!!!!!!!!!!");
|
||||
Message msg = JsonNet.Deserialize<Message>(Data[0].ToString());
|
||||
clients.Add(new Client(socket,msg.Ip, uiController));
|
||||
});
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// returns own ip adress
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public 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];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public MainPage getUIController()
|
||||
{
|
||||
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
|
||||
/// </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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,11 @@
|
||||
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
|
||||
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
|
||||
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
|
||||
IgnorableNamespaces="uap mp uap3">
|
||||
xmlns:uap6="http://schemas.microsoft.com/appx/manifest/uap/windows10/6"
|
||||
xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
|
||||
xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
|
||||
xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2"
|
||||
IgnorableNamespaces="uap mp uap3 uap6 iot uap4 uap2">
|
||||
|
||||
<Identity
|
||||
Name="ee2ef4f2-e61b-497a-8f0e-9fa7c90234b1"
|
||||
@@ -45,7 +49,42 @@
|
||||
</Applications>
|
||||
|
||||
<Capabilities>
|
||||
<Capability Name="internetClient" />
|
||||
<Capability Name="internetClientServer"/>
|
||||
<Capability Name="allJoyn"/>
|
||||
<uap:Capability Name="appointments"/>
|
||||
<uap3:Capability Name="backgroundMediaPlayback"/>
|
||||
<uap:Capability Name="blockedChatMessages"/>
|
||||
<uap:Capability Name="chat"/>
|
||||
<Capability Name="codeGeneration"/>
|
||||
<uap:Capability Name="contacts"/>
|
||||
<uap:Capability Name="enterpriseAuthentication"/>
|
||||
<uap6:Capability Name="graphicsCapture"/>
|
||||
<iot:Capability Name="lowLevelDevices"/>
|
||||
<uap:Capability Name="musicLibrary"/>
|
||||
<uap:Capability Name="objects3D"/>
|
||||
<uap4:Capability Name="offlineMapsManagement"/>
|
||||
<uap:Capability Name="phoneCall"/>
|
||||
<uap2:Capability Name="phoneCallHistoryPublic"/>
|
||||
<uap:Capability Name="picturesLibrary"/>
|
||||
<Capability Name="privateNetworkClientServer"/>
|
||||
<uap3:Capability Name="remoteSystem"/>
|
||||
<uap:Capability Name="removableStorage"/>
|
||||
<uap:Capability Name="sharedUserCertificates"/>
|
||||
<uap2:Capability Name="spatialPerception"/>
|
||||
<iot:Capability Name="systemManagement"/>
|
||||
<uap:Capability Name="userAccountInformation"/>
|
||||
<uap3:Capability Name="userNotificationListener"/>
|
||||
<uap:Capability Name="videosLibrary"/>
|
||||
<uap:Capability Name="voipCall"/>
|
||||
<uap4:Capability Name="userDataTasks"/>
|
||||
<DeviceCapability Name="bluetooth"/>
|
||||
<DeviceCapability Name="gazeInput"/>
|
||||
<DeviceCapability Name="lowLevel"/>
|
||||
<DeviceCapability Name="microphone"/>
|
||||
<DeviceCapability Name="location"/>
|
||||
<DeviceCapability Name="pointOfService"/>
|
||||
<DeviceCapability Name="proximity"/>
|
||||
<DeviceCapability Name="webcam"/>
|
||||
</Capabilities>
|
||||
</Package>
|
||||
</Package>
|
||||
|
||||
20
README.md
Normal file
20
README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Polychat
|
||||

|
||||
serverless chat app written in c# using [`SocketIOSharp`](https://github.com/uhm0311/SocketIOSharp) & `UWP`
|
||||
|
||||
### Features:
|
||||
- chat is not dependent on server, every client can be a server
|
||||
- chatpartner discovery over ip address
|
||||
- set username to be shown to chatpartners
|
||||
- save chat history to local disk (encrypted)
|
||||
- dark mode ;)
|
||||
|
||||
### Implentation:
|
||||
- network connection via the library [`SocketIOSharp`](https://github.com/uhm0311/SocketIOSharp)
|
||||
- graphical user interface uses `UWP` (Universal Windows App)
|
||||
- messages are sent via json
|
||||
- chat history gets saved as json after encryption
|
||||
|
||||
### Screenshots:
|
||||

|
||||

|
||||
Reference in New Issue
Block a user