public void ProcessRequest(HttpContext context) { // retrieve params var clientId = context.Request.QueryString["clientId"]; var longPollingDuration = int.Parse(context.Request.QueryString["longPollingDuration"]); var imgIdx = int.Parse(context.Request.QueryString["imgIdx"]); // if needed // stream image(s) data within the response for the given duration // the connection will be automatically reseted by the client when the request ends var startTime = DateTime.Now; var remainingTime = longPollingDuration; var handler = new RemoteSessionLongPollingHandler(context, clientId); try { handler.Open(); // keep the http context open as long as the http client is connected and for the given duration while (context.Response.IsClientConnected && remainingTime > 0) { Thread.Sleep(1000); remainingTime = longPollingDuration - Convert.ToInt32((DateTime.Now - startTime).TotalMilliseconds); } } catch (Exception exc) { // rethrown } finally { handler.Close(); } }
public void ProcessRequest(HttpContext context) { var handler = new RemoteSessionLongPollingHandler(context); // retrieve params var longPollingDuration = int.Parse(context.Request.QueryString["longPollingDuration"]); var imgIdx = int.Parse(context.Request.QueryString["imgIdx"]); // if needed // stream image(s) data within the response for the given duration // the connection will be automatically reseted by the client when the request ends var startTime = DateTime.Now; var remainingTime = longPollingDuration; try { // keep the http context open as long as the http client is connected and for the given duration while (context.Response.IsClientConnected && remainingTime > 0) { Thread.Sleep(1000); remainingTime = longPollingDuration - Convert.ToInt32((DateTime.Now - startTime).TotalMilliseconds); } } catch (Exception exc) { Trace.TraceError("long polling error for http session {0} ({1})", context.Session.SessionID, exc); } Trace.TraceInformation("long polling closed for http session {0}", context.Session.SessionID); }
public RemoteSessionLongPollingHandler(HttpContext context) { _context = context; Session = context.Session; try { if (_context.Session[HttpSessionStateVariables.RemoteSession.ToString()] == null) { throw new NullReferenceException(); } // retrieve the remote session for the given http session _remoteSession = (RemoteSession)_context.Session[HttpSessionStateVariables.RemoteSession.ToString()]; // register the handler against the remote session manager lock (((ICollection)_remoteSession.Manager.LongPollings).SyncRoot) { // search for a previously registered handler RemoteSessionLongPollingHandler oldLongPolling = null; foreach (var longPolling in _remoteSession.Manager.LongPollings) { if (longPolling.Session.SessionID == _context.Session.SessionID) { oldLongPolling = longPolling; break; } } // unregister the previous handler, if any if (oldLongPolling != null) { _remoteSession.Manager.LongPollings.Remove(oldLongPolling); } // register this handler _remoteSession.Manager.LongPollings.Add(this); } // the handler is ready to push data Send("<script>parent.lpInitConnection();</script>"); } catch (Exception exc) { Trace.TraceError("Failed to retrieve the remote session for the http session {0}, ({1})", _context.Session.SessionID, exc); } }