add retry logic on disconnect, silently delete chat if disconnected after connect was succesfull
This commit is contained in:
@@ -37,7 +37,7 @@ namespace PolyChat
|
|||||||
public void Connect(string ip)
|
public void Connect(string ip)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Controller.Connect ---");
|
Debug.WriteLine("--- Controller.Connect ---");
|
||||||
Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data)));
|
Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Serve()
|
private void Serve()
|
||||||
@@ -59,7 +59,7 @@ namespace PolyChat
|
|||||||
Debug.WriteLine("--- initial packet received ---");
|
Debug.WriteLine("--- initial packet received ---");
|
||||||
string ForeignIp = data[0].ToString();
|
string ForeignIp = data[0].ToString();
|
||||||
//Todo deserialize inital packet and extract ip address
|
//Todo deserialize inital packet and extract ip address
|
||||||
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data)));
|
Connections.Add(ForeignIp, new Connection(socket, Data => OnMessage(ForeignIp, Data), CloseChat));
|
||||||
UIController.OnIncomingConnection(ForeignIp);
|
UIController.OnIncomingConnection(ForeignIp);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -89,6 +89,15 @@ namespace PolyChat
|
|||||||
else Debug.WriteLine("Undefined: " + data);
|
else Debug.WriteLine("Undefined: " + data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CloseChat(string IP, bool wasConnected = true)
|
||||||
|
{
|
||||||
|
Connections[IP].Close();
|
||||||
|
Connections.Remove(IP);
|
||||||
|
UIController.OnChatPartnerDeleted(IP);
|
||||||
|
if(!wasConnected)
|
||||||
|
UIController.ShowConnectionError(IP, $"Connection to {IP} failed...");
|
||||||
|
}
|
||||||
|
|
||||||
public string getIP()
|
public string getIP()
|
||||||
{
|
{
|
||||||
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
|
||||||
|
|||||||
@@ -36,15 +36,22 @@ namespace PolyChat
|
|||||||
updateSendButtonEnabled();
|
updateSendButtonEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void ShowConnectionError(string message)
|
public async void ShowConnectionError(string code, string message)
|
||||||
{
|
{
|
||||||
ConnectionFailedDialog dialog = new ConnectionFailedDialog(message);
|
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
|
||||||
var result = await dialog.ShowAsync();
|
|
||||||
if (result == ContentDialogResult.Primary)
|
|
||||||
{
|
{
|
||||||
//retry
|
ConnectionFailedDialog dialog = new ConnectionFailedDialog(message);
|
||||||
}
|
var result = await dialog.ShowAsync();
|
||||||
// else abort -> del chat
|
if (result == ContentDialogResult.Primary)
|
||||||
|
{
|
||||||
|
Controller.Connect(code);
|
||||||
|
Partners.Add(new ChatPartner(
|
||||||
|
"Connecting...",
|
||||||
|
code
|
||||||
|
));
|
||||||
|
updateNoChatsPlaceholder();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// EVENTS
|
// EVENTS
|
||||||
@@ -113,10 +120,22 @@ namespace PolyChat
|
|||||||
|
|
||||||
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
|
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
|
||||||
{
|
{
|
||||||
|
Controller.CloseChat(selectedPartner.Code);
|
||||||
Partners.Remove(selectedPartner);
|
Partners.Remove(selectedPartner);
|
||||||
updateNoChatsPlaceholder();
|
updateNoChatsPlaceholder();
|
||||||
updateNoChatSelected();
|
updateNoChatSelected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async void OnChatPartnerDeleted(string code)
|
||||||
|
{
|
||||||
|
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
|
||||||
|
{
|
||||||
|
Partners.Remove(Partners.First(p => p.Code == code));
|
||||||
|
selectedPartner = null;
|
||||||
|
updateNoChatsPlaceholder();
|
||||||
|
updateNoChatSelected();
|
||||||
|
});
|
||||||
|
}
|
||||||
public void OnChatPartnerSelected(object sender, RoutedEventArgs e)
|
public void OnChatPartnerSelected(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
string code = ((RadioButton)sender).Tag.ToString();
|
string code = ((RadioButton)sender).Tag.ToString();
|
||||||
|
|||||||
@@ -15,11 +15,13 @@ namespace PolyChat.Models
|
|||||||
private SocketIOSocket Socket;
|
private SocketIOSocket Socket;
|
||||||
private bool Connected = false;
|
private bool Connected = false;
|
||||||
private readonly string IP;
|
private readonly string IP;
|
||||||
|
private Action<string, bool> DeleteConnection;
|
||||||
|
|
||||||
public Connection(string ip, ushort port, Action<JToken[]> onMessage)
|
public Connection(string ip, ushort port, Action<JToken[]> onMessage, Action<string, bool> onClose)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("! CONNECTING TO SERVER !");
|
Debug.WriteLine("! CONNECTING TO SERVER !");
|
||||||
IP = ip;
|
IP = ip;
|
||||||
|
DeleteConnection = onClose;
|
||||||
// establish connection
|
// establish connection
|
||||||
Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port));
|
Client = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, port));
|
||||||
Client.Connect();
|
Client.Connect();
|
||||||
@@ -30,9 +32,10 @@ namespace PolyChat.Models
|
|||||||
Client.On("message", (Action<JToken[]>) onMessage);
|
Client.On("message", (Action<JToken[]>) onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Connection(SocketIOSocket socket, Action<JToken[]> onMessage)
|
public Connection(SocketIOSocket socket, Action<JToken[]> onMessage, Action<string, bool> onClose)
|
||||||
{
|
{
|
||||||
Socket = socket;
|
Socket = socket;
|
||||||
|
DeleteConnection = onClose;
|
||||||
Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect);
|
Socket.On(SocketIOEvent.DISCONNECT, OnDisconnect);
|
||||||
Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data));
|
Socket.On(SocketIOEvent.ERROR, (JToken[] Data) => OnError(Data));
|
||||||
Socket.On("message", (Action<JToken[]>) onMessage);
|
Socket.On("message", (Action<JToken[]>) onMessage);
|
||||||
@@ -62,8 +65,9 @@ namespace PolyChat.Models
|
|||||||
private void OnDisconnect()
|
private void OnDisconnect()
|
||||||
{
|
{
|
||||||
Debug.WriteLine("--- Disconnected! ---");
|
Debug.WriteLine("--- Disconnected! ---");
|
||||||
|
Debug.WriteLine($"--- Deleting Connection with IP: {IP}---");
|
||||||
|
DeleteConnection(IP, IsConnected());
|
||||||
Connected = false;
|
Connected = false;
|
||||||
Close();
|
|
||||||
}
|
}
|
||||||
private void OnError(JToken[] data)
|
private void OnError(JToken[] data)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user