public CommandDispatcher(UserContext context)
        {
            context.SetOnDisconnect(OnDisconnect);
            context.SetOnReceive(OnReceive);
            context.SetOnConnected(OnConnected);

            this.Context = context;
        }
        private void OnReceiveCore(UserContext context, DataFrame frame)
        {
            var memoryStream = new MemoryStream();
            memoryStream.Write(frame.Payload, 0, frame.Payload.Length);
            memoryStream.Position = 0;

            var obj = JsonSerializer.Parse<JukeboxMessage>(memoryStream);

            JukeboxMessage dispatcherObject = (JukeboxMessage)obj;
            if (dispatcherObject.Type == "response")
                HandleResponse(dispatcherObject);
            else if (dispatcherObject.Type == "request")
                HandleRequest(dispatcherObject);
            else if (dispatcherObject.Type == "error")
                HandleError(dispatcherObject);
        }
        private void SendKeepAlive(UserContext context)
        {
            ts.TraceEvent(TraceEventType.Verbose, 0, "Sending keep alive");

            try
            {
                Invoke("keep-alive", null);
                ts.TraceEvent(TraceEventType.Information, 0, "Ok");
            }
            catch (Exception e)
            {
                ts.TraceEvent(TraceEventType.Error, 0, e.Message);
            }
        }
 private void OnReceive(UserContext context, DataFrame frame)
 {
     try
     {
         OnReceiveCore(context, frame);
     }
     catch (Exception e)
     {
         ts.TraceEvent(TraceEventType.Error, 0, e.Message);
     }
 }
 private void OnDisconnect(UserContext c)
 {
     this.Context = null;
     this.keepAliveTimer.Dispose();
     this.keepAliveTimer = null;
 }
 private void OnConnected(UserContext context)
 {
     this.keepAliveTimer = new Timer(PollServer, null, new TimeSpan(0, 0, 5), new TimeSpan(0, 0, 30));
 }
        private void OnConnect(UserContext context)
        {
            var dispatcher = new CommandDispatcher(context);

            var id = context.RequestPath;
            Debug.Assert(id.Length > 0);
            id = id.Substring(1); // Strip first slash

            var jukebox = EnsureJukebox(id);
            if (jukebox.IsAttached)
            {
                dispatcher.Context.SetOnConnected((o) =>
                {
                    dispatcher.Close(Constants.AlreadyConnectedError);
                });
                return;
            }

            ts.TraceEvent(TraceEventType.Information, 0, "Jukebox connected [id = '{0}']", id);
            context.Data = jukebox;

            jukebox.Start(dispatcher);
        }
 private void OnDisconnect(UserContext context)
 {
     var jukebox = context.Data as Jukebox;
     if (jukebox != null)
         ts.TraceEvent(TraceEventType.Information, 0, "Jukebox disconnected [id = '{0}']", jukebox.Identifier);
 }