Skip to content

Commit

Permalink
- Added code that shows the connect form again once the server is clo…
Browse files Browse the repository at this point in the history
…sed (so that the user can trade XAML pages on the server without needing to restart the app on the devices)
  • Loading branch information
reyalpsirc committed Dec 16, 2015
1 parent 9f4c89c commit 562f07d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
11 changes: 9 additions & 2 deletions LiveXAMLApp/Droid/Dependencies/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
5 changes: 5 additions & 0 deletions LiveXAMLApp/LiveXAML/Dependencies/IWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public Action<Exception> Error {
set;
}

public Action Closed {
get;
set;
}

public Action<string> MessageReceived {
get;
set;
Expand Down
1 change: 1 addition & 0 deletions LiveXAMLApp/LiveXAML/LiveXAML.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="test.xaml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Dependencies\" />
Expand Down
19 changes: 16 additions & 3 deletions LiveXAMLApp/LiveXAML/TestPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -134,7 +145,7 @@ StackLayout StartLayout ()
};
var portEntry = new Entry {
HorizontalOptions=LayoutOptions.FillAndExpand,
Text="9934"
Text=port
};
layout= new StackLayout{
HeightRequest=40,
Expand All @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions LiveXAMLApp/iOS/Dependencies/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down

0 comments on commit 562f07d

Please sign in to comment.