This commit is contained in:
SCM6WE
2021-09-21 10:12:29 +02:00
parent d42acbc90e
commit 697a7ad506
9 changed files with 188 additions and 45 deletions

View File

@@ -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;
}
}