private async void Process(object sender, SocketMessageReceivedEventArgs e)
 {
     string methodName;
     string methodNamespace;
     IDictionary<string, string> methodParams = new Dictionary<string, string>();
     IDictionary<string, dynamic> requestObject = null;
     try
     {
         requestObject = JsonConvert.DeserializeObject<IDictionary<string, dynamic>>(e.Message);
     }
     catch (JsonException)
     {
         this.SendMessage(JsonConvert.SerializeObject(JSResponse.GetErrorResponse("invalid json")));
         return;
     }
     if (requestObject.ContainsKey("method") && requestObject.ContainsKey("namespace"))
     {
         methodName = (string)requestObject["method"];
         methodNamespace = (string)requestObject["namespace"];
     }
     else
     {
         this.SendMessage(JsonConvert.SerializeObject(JSResponse.GetErrorResponse("missing method or namespace")));
         return;
     }
     if (requestObject.ContainsKey("params"))
     {
         methodParams = requestObject["params"].ToObject<IDictionary<string, string>>();
     }
     var request = new JSRequest(methodNamespace, methodName, methodParams);
     this.SendMessage(await this.ProcessRequest(request));
 }
Exemple #2
0
        protected override async Task Process(HttpListenerContext context)
        {
            var writer = new StreamWriter(context.Response.OutputStream);
            context.AddAccessControlHeaders();
            string getRequest = context.Request.Url.AbsolutePath.Remove(0,1); //Remove first slash
            string getUri = context.Request.Url.AbsoluteUri;
            int index = getUri.IndexOf("?", StringComparison.Ordinal);
            IDictionary<string, string> dictParams = new Dictionary<string, string>();

            if (getUri.Contains("?post"))
            {
                try
                {
                    using (var reader = new StreamReader(context.Request.InputStream)){
                        IDictionary<string, string> _dictParams = JsonConvert.DeserializeObject<IDictionary<string, string>>(reader.ReadToEnd());
                        dictParams = _dictParams ?? dictParams;
                    }
                }
                catch (JsonException)
                {
                    writer.WriteLine(JsonConvert.SerializeObject(JSResponse.GetErrorResponse("missing methodname or namespace")));
                    writer.Flush();
                    context.Response.OutputStream.Close();
                }
            }
            else
            {
                if (index > 0)
                {
                    string rawParams = getUri.Substring(index).Remove(0, 1);
                    var nvcParams = HttpUtility.ParseQueryString(rawParams);
                    dictParams = nvcParams.AllKeys.ToDictionary(o => o, o => nvcParams[o]);
                }
            }
            
            if(!(getRequest.Split('/').Count() >= 2))
            {
                writer.WriteLine(JsonConvert.SerializeObject(JSResponse.GetErrorResponse("missing methodname or namespace")));
                writer.Flush();
                context.Response.OutputStream.Close();
            }
            var request = new JSRequest(getRequest.Split('/')[0], getRequest.Split('/')[1], dictParams);

            if (request.MethodParameters.ContainsKey("jsoncallback"))
            {
                context.Response.AppendHeader("Content-Type", "application/javascript");
            }
            else
            {
                context.Response.AppendHeader("Content-Type", "application/json");
            }
            
            writer.WriteLine(await this.ProcessRequest(request));
            writer.Flush();
            context.Response.OutputStream.Close();
        }
Exemple #3
0
 private async Task<string> ProcessRequest(JSRequest args)
 {
     return await this.coreInstance.Get<IAjaxManager>().CallMethodAsync(args);
 }