From 562f07d8c7ffb04fe114ae25dbf3d43ebaad30ce Mon Sep 17 00:00:00 2001 From: Cristiano Santos Date: Wed, 16 Dec 2015 20:51:57 +0000 Subject: [PATCH] - Added code that shows the connect form again once the server is closed (so that the user can trade XAML pages on the server without needing to restart the app on the devices) --- .../Droid/Dependencies/WebSocketClient.cs | 11 +++++++++-- .../LiveXAML/Dependencies/IWebSocketClient.cs | 5 +++++ LiveXAMLApp/LiveXAML/LiveXAML.csproj | 1 + LiveXAMLApp/LiveXAML/TestPage.cs | 19 ++++++++++++++++--- .../iOS/Dependencies/WebSocketClient.cs | 11 +++++++++-- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/LiveXAMLApp/Droid/Dependencies/WebSocketClient.cs b/LiveXAMLApp/Droid/Dependencies/WebSocketClient.cs index 3d1bcc8..31a76ab 100644 --- a/LiveXAMLApp/Droid/Dependencies/WebSocketClient.cs +++ b/LiveXAMLApp/Droid/Dependencies/WebSocketClient.cs @@ -33,18 +33,25 @@ public override void Open (string wslocalhost){ }; websocket.Error += delegate(object sender, SuperSocket.ClientEngine.ErrorEventArgs e) { if (!connected && websocket!=null){ - connected = true; websocket.Close(); + connected = true; if (Error!=null){ Error(e.Exception); } } }; + websocket.Closed += delegate(object sender, EventArgs e) { + if (connected){ + if (Closed != null) { + Closed (); + } + } + }; timer = new Task (async delegate() { await Task.Delay(2000); if (!connected && websocket!=null){ - connected = true; websocket.Close(); + connected = true; if (Error!=null){ Error(new Exception("Connection failed")); } diff --git a/LiveXAMLApp/LiveXAML/Dependencies/IWebSocketClient.cs b/LiveXAMLApp/LiveXAML/Dependencies/IWebSocketClient.cs index a70d381..4960917 100644 --- a/LiveXAMLApp/LiveXAML/Dependencies/IWebSocketClient.cs +++ b/LiveXAMLApp/LiveXAML/Dependencies/IWebSocketClient.cs @@ -15,6 +15,11 @@ public Action Error { set; } + public Action Closed { + get; + set; + } + public Action MessageReceived { get; set; diff --git a/LiveXAMLApp/LiveXAML/LiveXAML.csproj b/LiveXAMLApp/LiveXAML/LiveXAML.csproj index 21b9441..8f0798b 100644 --- a/LiveXAMLApp/LiveXAML/LiveXAML.csproj +++ b/LiveXAMLApp/LiveXAML/LiveXAML.csproj @@ -67,6 +67,7 @@ + diff --git a/LiveXAMLApp/LiveXAML/TestPage.cs b/LiveXAMLApp/LiveXAML/TestPage.cs index d6f0fb5..ff3666a 100644 --- a/LiveXAMLApp/LiveXAML/TestPage.cs +++ b/LiveXAMLApp/LiveXAML/TestPage.cs @@ -12,6 +12,8 @@ namespace LiveXAML public class TestPage : ContentPage { IXamlDependency xamlDep; + string ipAddress="127.0.0.1"; + string port="9934"; bool receivedMessage=false; public TestPage () @@ -39,6 +41,15 @@ private async void ConnectServer(string address, int port){ }); Debug.WriteLine(obj); }; + client.Closed = delegate() { + Debug.WriteLine ("Closed"); + if (receivedMessage){ + receivedMessage=false; + Device.BeginInvokeOnMainThread(delegate { + Content=StartLayout(); + }); + } + }; client.MessageReceived = delegate(string message) { if (!receivedMessage){ receivedMessage=true; @@ -116,7 +127,7 @@ StackLayout StartLayout () }; var ipEntry = new Entry { HorizontalOptions=LayoutOptions.FillAndExpand, - Text="127.0.0.1" + Text=ipAddress }; var layout= new StackLayout{ HeightRequest=40, @@ -134,7 +145,7 @@ StackLayout StartLayout () }; var portEntry = new Entry { HorizontalOptions=LayoutOptions.FillAndExpand, - Text="9934" + Text=port }; layout= new StackLayout{ HeightRequest=40, @@ -151,7 +162,9 @@ StackLayout StartLayout () BackgroundColor=Color.Gray }; connectButton.Clicked += delegate(object sender, EventArgs e) { - ConnectServer(ipEntry.Text,int.Parse(portEntry.Text)); + ipAddress=ipEntry.Text; + port=portEntry.Text; + ConnectServer(ipAddress,int.Parse(port)); }; layout= new StackLayout{ HeightRequest=40, diff --git a/LiveXAMLApp/iOS/Dependencies/WebSocketClient.cs b/LiveXAMLApp/iOS/Dependencies/WebSocketClient.cs index b252827..632d399 100644 --- a/LiveXAMLApp/iOS/Dependencies/WebSocketClient.cs +++ b/LiveXAMLApp/iOS/Dependencies/WebSocketClient.cs @@ -33,18 +33,25 @@ public override void Open (string wslocalhost){ }; websocket.Error += delegate(object sender, SuperSocket.ClientEngine.ErrorEventArgs e) { if (!connected && websocket!=null){ - connected = true; websocket.Close(); + connected = true; if (Error!=null){ Error(e.Exception); } } }; + websocket.Closed += delegate(object sender, EventArgs e) { + if (connected){ + if (Closed != null) { + Closed (); + } + } + }; timer = new Task (async delegate() { await Task.Delay(2000); if (!connected && websocket!=null){ - connected = true; websocket.Close(); + connected = true; if (Error!=null){ Error(new Exception("Connection failed")); }