/// <summary>
        /// Broadcasts an error message to the client who caused the error
        /// </summary>
        /// <param name="ErrorMessage">Details of the error</param>
        /// <param name="AContext">The user's connection context</param>
        private static void SendError(string ErrorMessage, UserContext AContext)
        {
            Response r = new Response();

            r = new Response();
            r.Type = ResponseType.Error;
            r.Data = new { Message = ErrorMessage };

            AContext.Send(JsonConvert.SerializeObject(r));
        }
Exemplo n.º 2
0
        private void OnReceive(UserContext context)
        {
            //TODO :: Logging?
            var client = context.Data as Client;
            if (client != null)
            {
                Request[] requests = client.GetRequests();
                if (requests != null)
                {
                    if (requests.Length > 0)
                    {
                        foreach (Request request in requests)
                        {
                            request.From = client;

                            if (TryForward(request))
                            {
                                //Only requests that are forwarded need to be tracked.
                                client.Requests.TryAdd(request.Id, request);
                            }
                            else
                            {
                                client.Error(request, Message.ServerUnavailable);
                            }
                        }
                    }
                }
                else
                {
                    client.Error(null, Message.RequestMalformed);
                }
            }
            else
            {
                context.Send(String.Empty, true); //Close
            }
        }
        /// <summary>
        /// Event fired when a data is received from the Alchemy Websockets server instance.
        /// Parses data as JSON and calls the appropriate message or sends an error message.
        /// </summary>
        /// <param name="AContext">The user's connection context</param>
        public static void OnReceive(UserContext AContext)
        {
            Console.WriteLine("Received Data From :" + AContext.ClientAddress.ToString());

            try
            {
                string json = AContext.DataFrame.ToString();

                // <3 dynamics
                dynamic obj = JsonConvert.DeserializeObject(json);

                if ((int)obj.Type == (int)CommandType.Register)
                {
                    Register(obj.Name.Value, AContext);
                }
                else if ((int)obj.Type == (int)CommandType.Message)
                {
                    ChatMessage(obj.Message.Value, AContext);
                }
                else if ((int)obj.Type == (int)CommandType.NameChange)
                {
                    NameChange(obj.Name.Value, AContext);
                }
            }
            catch (Exception e) // Bad JSON! For shame.
            {
                Response r = new Response();
                r.Type = ResponseType.Error;
                r.Data = new { Message = e.Message };

                AContext.Send(JsonConvert.SerializeObject(r));
            }
        }