Minor updates to SseClient class

This commit is contained in:
Neil Dorin
2017-08-14 10:08:03 -06:00
parent 208e236308
commit 44c9bcb788
5 changed files with 37 additions and 21 deletions

Binary file not shown.

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Net;
using Crestron.SimplSharp.Net.Http; using Crestron.SimplSharp.Net.Http;
namespace PepperDash.Core namespace PepperDash.Core
@@ -35,6 +36,8 @@ namespace PepperDash.Core
HttpClient Client; HttpClient Client;
HttpClientRequest Request; HttpClientRequest Request;
Connection SseConnection;
public GenericHttpSseClient(string key, string name) public GenericHttpSseClient(string key, string name)
{ {
@@ -75,25 +78,40 @@ namespace PepperDash.Core
// In order to get a handle on the response stream, we have to get // In order to get a handle on the response stream, we have to get
// the request stream first. Boo // the request stream first. Boo
Client.BeginGetRequestStream(GetRequestStreamCallback, Request, null); Client.BeginGetRequestStream(GetRequestStreamCallback, Request, null);
CrestronConsole.PrintLine("Request made!"); //CrestronConsole.PrintLine("Request made!");
} }
catch (Exception e) catch (Exception e)
{ {
ErrorLog.Notice("Exception occured in AsyncWebPostHttps(): " + e.ToString()); ErrorLog.Notice("Exception occured in InitiateConnection(): " + e);
} }
}); });
} }
public void CloseConnection(string s) public void CloseConnection(string s)
{
try
{ {
if (Client != null) if (Client != null)
{ {
if (SseConnection != null)
{
// Gracefully dispose of any in use resources??
SseConnection.Disconnect();
SseConnection.Close(true);
Client.Abort(); Client.Abort();
}
IsConnected = false; IsConnected = false;
Debug.Console(1, this, "Client Disconnected"); Debug.Console(1, this, "Client Disconnected");
} }
} }
catch (Exception e)
{
Debug.Console(1, this, "Error Closing SSE Connection: {0}", e);
}
}
private void GetRequestStreamCallback(HttpClientRequest request, HTTP_CALLBACK_ERROR error, object status) private void GetRequestStreamCallback(HttpClientRequest request, HTTP_CALLBACK_ERROR error, object status)
{ {
@@ -130,14 +148,10 @@ namespace PepperDash.Core
// This closes up the GetResponseStream async // This closes up the GetResponseStream async
var response = request.ThisClient.EndGetResponseStream(request); var response = request.ThisClient.EndGetResponseStream(request);
response.DataConnection.OnBytesReceived += new EventHandler(DataConnection_OnBytesReceived); SseConnection = response.DataConnection;
IsConnected = true;
Debug.Console(1, this, "Client Disconnected"); Debug.Console(1, this, "Client Disconnected");
#warning Need to explore using a heartbeat to verify connection to the server
Stream streamResponse = response.ContentStream; Stream streamResponse = response.ContentStream;
// Object containing various states to be passed back to async callback below // Object containing various states to be passed back to async callback below
RequestState asyncState = new RequestState(); RequestState asyncState = new RequestState();
@@ -163,11 +177,6 @@ namespace PepperDash.Core
} }
} }
void DataConnection_OnBytesReceived(object sender, EventArgs e)
{
Debug.Console(1, this, "DataConnection OnBytesReceived Fired");
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@@ -205,6 +214,8 @@ namespace PepperDash.Core
return; return;
} }
try
{
Crestron.SimplSharp.CrestronIO.IAsyncResult asynchronousResult; Crestron.SimplSharp.CrestronIO.IAsyncResult asynchronousResult;
do do
{ {
@@ -213,6 +224,11 @@ namespace PepperDash.Core
} }
while (asynchronousResult.CompletedSynchronously && !requestState.Done); while (asynchronousResult.CompletedSynchronously && !requestState.Done);
} }
catch (Exception e)
{
Debug.Console(1, this, "Exception: {0}", e);
}
}
else else
{ {
requestState.Done = true; requestState.Done = true;