SendMessage is now Json

This commit is contained in:
Patrick Hellebrand
2021-09-22 14:57:36 +02:00
parent d25063bd87
commit 3b2b75df53
3 changed files with 21 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ using System.Net;
using SocketIOSharp.Server;
using SocketIOSharp.Server.Client;
using PolyChat.Models;
using System.Text.Json;
namespace PolyChat
{
@@ -50,20 +51,24 @@ namespace PolyChat
{
Debug.WriteLine("--- Client connected! ---");
// setup event listeners
socket.On("initial", (JToken[] data) =>
socket.On("initial", async (JToken[] data) =>
{
Debug.WriteLine("--- initial packet received ---");
string ForeignIp = data.ToString();
//Todo deserialize inital packet and extract ip address
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(Data)));
UIController.OnIncomingConnection(ForeignIp);
});
});
}
public void SendMessage(string ip, string message)
public void SendMessage(string ip, string type, string content)
{
Debug.WriteLine("--- Controller.SendMessage ---");
Connections[ip].SendMessage(message);
Debug.WriteLine($"{type} -> {ip} content: {content}");
string json = $"{{ type: {type}, content: {content} }}";
Debug.WriteLine($"json: {json}");
Connections[ip].SendMessage(json);
}
private void OnMessage(JToken[] data)
@@ -72,6 +77,7 @@ namespace PolyChat
if (data != null && data.Length > 0 && data[0] != null)
{
Debug.WriteLine("Message: " + data[0]);
Debug.WriteLine($"DATA: {data[0].ToString()}");
}
else Debug.WriteLine("Undefined: " + data);
}

View File

@@ -5,6 +5,7 @@ using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@@ -51,7 +52,7 @@ namespace PolyChat
public void OnSendMessage(object sender = null, RoutedEventArgs e = null)
{
selectedPartner.AddMessage(new ChatMessage(username, "message" , inputSend.Text));
Controller.SendMessage(selectedPartner.Code, inputSend.Text);
Controller.SendMessage(selectedPartner.Code, "message", inputSend.Text);
// clear input
inputSend.Text = "";
}
@@ -89,12 +90,16 @@ namespace PolyChat
/// Adds a new ChatPartner to the UI with default Name.
/// </summary>
/// <param name="ip">IP Adress, gets shown as Util.IP > Code</param>
public void OnIncomingConnection(string ip)
public async void OnIncomingConnection(string ip)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Partners.Add(new ChatPartner(
"Connecting...",
ip
));
updateNoChatsPlaceholder();
});
}
/// <summary>
/// Adds an message to the UI, based on .sender if known

View File

@@ -35,7 +35,7 @@ namespace PolyChat.Models
Socket = socket;
Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect);
Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data));
Socket.On("message", (Action<JToken[]>)onMessage);
Socket.On("message", (Action<JToken[]>) onMessage);
//we are connected if we got here, inital packet was already received
Connected = true;