Start a new topic

This is getting INSANE - PART II

This is the DuoCode version (using JSON of course) but the point is the signature

and what they do respective to the platform they are running on is the same:


This same wrapper code around AjaxClient.CallMethod will work on ALL my .net platforms, Windows, Max, IOS, Android... And now C# in the Browser:


 GreetingService svc = new GreetingService("http://localhost:4444/ajax/gateway");

                svc.greetServer("John Doe Smith", new MethodCallback<string>(

 (result) => {

                        Console.WriteLine("Got a Good Result: " + result);

 }, (error) => {

                        Console.WriteLine("Got Error: " + error.Message);

 }

 ));




Really make a call to DuoCode Class Library (works too... i just need to do a bit more parsing of the result... but the actual payload goes out to the server and i get a response back with my data... cleaning up code now... just was excited to show what can done with DuoCode)


using System;

using DuoCode.Dom;

using DuoCode.Runtime;


namespace Ajax.Gateway

{

 public class MethodCallback<T>

 {

        public Action<T> OnSuccess { get; private set; }

        public Action<Exception> OnFailure { get; private set; }

        //

        public MethodCallback(Action<T> success, Action<Exception> failure = null)

 {

 OnSuccess = success;

 OnFailure = failure;

 }

 }


 public class AjaxClient

 {

 public const string RpcFormContentType = "application/x-www-form-urlencoded";

        public const string RpcFlashContentType = "application/x-shockwave-flash";

        public const string RpcBinaryContentType = "application/octet-stream";

        public const string RpcScriptContentType = "application/javascript";

        public const string RpcPostContentType = "application/x-ajax-request";

        public const string RpcNativeContentType = "application/x-ajax-native";

        public const string RpcJsonContentType = "application/json";

        public const string RpcXmlContentType = "application/xml";

        public const string RpcHtmlContentType = "text/html";

        public const string RpcTextContentType = "text/plain";

        //

        public const string RpcPostRequestType = "POST";

        public const string RpcGetRequestType = "GET";

        public const string RpcOptionsRequestType = "OPTIONS";

        //

        public const string RpcNullType = "{null}";

        public const string RpcVoidType = "{void}";

        //

        public const string RpcXmlResponseType = "application/x-ajax-xml";

        public const string RpcJsonResponseType = "application/x-ajax-json";

        public const string RpcNullResponseType = "application/x-ajax-null";

        public const string RpcVoidResponseType = "application/x-ajax-void";

        public const string RpcStringResponseType = "application/x-ajax-string";

        public const string RpcBooleanResponseType = "application/x-ajax-boolean";

        public const string RpcDecimalResponseType = "application/x-ajax-decimal";

        public const string RpcDoubleResponseType = "application/x-ajax-double";

        public const string RpcSingleResponseType = "application/x-ajax-single";

        public const string RpcInt16ResponseType = "application/x-ajax-int16";

        public const string RpcInt32ResponseType = "application/x-ajax-int32";

        public const string RpcInt64ResponseType = "application/x-ajax-int64";

        public const string RpcIntPtrResponseType = "application/x-ajax-intptr";

        public const string RpcUInt16ResponseType = "application/x-ajax-uint16";

        public const string RpcUInt32ResponseType = "application/x-ajax-uint32";

        public const string RpcUInt64ResponseType = "application/x-ajax-uint64";

        public const string RpcUIntPtrResponseType = "application/x-ajax-uintptr";

        public const string RpcBase64ResponseType = "application/x-ajax-base64";

        public const string RpcCharResponseType = "application/x-ajax-char";

        public const string RpcByteResponseType = "application/x-ajax-byte";

        public const string RpcSByteResponseType = "application/x-ajax-sbyte";

        public const string RpcDateTimeResponseType = "application/x-ajax-datetime";

        public const string RpcInteropResponseType = "application/x-ajax-interop";


        public static string FormatRequest(string methodName, object[]paramValues) {

 //var rpcRequest = new ajax_request_token();

 //rpcRequest.method = methodName;

 //if (paramValues != null && ajax_is_array(paramValues)) {

 //    rpcRequest.params = paramValues;

            //}

 //rpcRequest.id = (this.requestId != null) ? this.requestId : "0";

 //return JSON.stringify(rpcRequest);

 return "{\"jsonrpc\":\"2.0\",\"method\":\"Ajax.Gateway.GreetServer\",\"params\":[\"Mackey Kinard\"],\"id\":\"0\"}";

 }


        public static void CallMethod<T>(string url, string method, object[] parameters = null, MethodCallback<T> callback = null)

 {

            string data = FormatRequest(method, parameters);

 XMLHttpRequest xhr = new XMLHttpRequest();

            xhr.open("POST", url, true);

            xhr.setRequestHeader("Content-Type", RpcPostContentType);

            //

            xhr.onerror = delegate(ErrorEvent err) {

                System.Console.WriteLine("XHR - ON ERROR: " + err.message); // TODO: Handle Error Here

                return null;

 };

            //

            xhr.onreadystatechange = delegate(Event evt) {

                if (xhr.readyState == 4) {

                    if (xhr.status == 200) {

                        try {

                            object nil = null;

 T httpResponse = (T)nil;

                            string response_data = (string)xhr.response;


                            System.Console.WriteLine(response_data);

                            Ajax.Gateway.Webix.Webix.Message(response_data);


                            string returnType = xhr.getResponseHeader("Content-Type");

                            if (returnType == RpcVoidResponseType || returnType == RpcNullResponseType) {

 httpResponse = (T)nil;

                            } else if (returnType == RpcJsonResponseType) {

                                httpResponse = JSON.parse(xhr.response);

                            } else if (returnType == RpcInteropResponseType) {

                                httpResponse = JSON.parse(xhr.response);

                            } else if (returnType == RpcStringResponseType || returnType == RpcBase64ResponseType) {

 httpResponse = xhr.response;

                            } else if (returnType == RpcBooleanResponseType) {

 httpResponse = (T)xhr.response;

                            } else if (returnType == RpcInt16ResponseType || returnType == RpcInt32ResponseType || returnType == RpcInt64ResponseType || returnType == RpcIntPtrResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcUInt16ResponseType || returnType == RpcUInt32ResponseType || returnType == RpcUInt64ResponseType || returnType == RpcUIntPtrResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcDoubleResponseType || returnType == RpcDecimalResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcSingleResponseType) {

 //httpResponse = parseFloat(xhr.response);

                            } else if (returnType == RpcByteResponseType || returnType == RpcSByteResponseType) {

 //httpResponse = parseInt(xhr.response);

                            } else if (returnType == RpcDateTimeResponseType) {

 //httpResponse = new Date(xhr.response);

                            } else if (returnType == RpcCharResponseType) {

 httpResponse = xhr.response;

                            } else if (returnType == RpcXmlResponseType) {

                                //if (window.DOMParser) {

 //    var parser = new DOMParser();

 //    httpResponse = parser.parseFromString(xhr.response, "text/xml");

                                //}

                                //else // Internet Explorer

                                //{

 //    httpResponse = new ActiveXObject("Microsoft.XMLDOM");

 //    httpResponse.async = false;

 //    httpResponse.loadXML(xhr.response);

                                //}

                            } else if (returnType == RpcNativeContentType) {

 throw new Exception("Native response content types not supported");

                            } else {

                                throw new Exception("Invalid response content type encountered: " + returnType);

 }

                            //

                            if (callback != null && callback.OnFailure != null) {

 callback.OnSuccess(httpResponse);

 }

                        } catch (Exception err) {

                            if (callback != null && callback.OnFailure != null) {

 callback.OnFailure(err);

 }

 }

                    } else {

                        if (callback != null && callback.OnFailure != null) {

                            string status = xhr.status.ToString(), status_text = xhr.statusText;

                            callback.OnFailure(new Exception(status + " - " + status_text));

 }

 }

 }

                return null;

 };

 xhr.send(data);

 }

 }

}



1 Comment

Excellent!


It would be awesome if you could write a blog post, describing your experiences (and successes!) with DuoCode!

We'd be happy to feature it on our blog too!

Login or Signup to post a comment