예제 #1
0
        public void CallAPI <ReplyType>(string api, object request, Action <ReplyType> onSuccess, OnErrorReply onError)
        {
            string messageNum = (MessageSequence++).ToString();
            string json       = JsonConvert.SerializeObject(request, Formatting.None, JsonSettings);

            string message = api + ":" + messageNum + ":" + json;

            MessageReplyClosure callback = delegate(bool isError, string replyJson)
            {
                if (isError)
                {
                    ErrorReply err = JsonConvert.DeserializeObject <ErrorReply>(replyJson, JsonSettings);
                    SV.OnServerError(err);
                    if (onError != null)
                    {
                        onError(err);
                    }
                }
                else
                {
                    ReplyType reply = JsonConvert.DeserializeObject <ReplyType>(replyJson, JsonSettings);
                    if (onSuccess != null)
                    {
                        onSuccess(reply);
                    }
                }
            };

            ReplyCallbacks.Add(messageNum, callback);

            ServerSocket.Send(message);
        }
		public static ErrorReply makeClientErrorCode(int code, string details)
		{
			ErrorReply reply = new ErrorReply();

			reply.errorCode = code;
			reply.errorMessage = "There was a connection error";
			reply.errorDetails = details;

			switch(code)
			{
			case -1:
				reply.errorMessage = "Connection closed";
				break;
			case -2:
				reply.errorMessage = "Network error";
				break;
			}

			return reply;
		}
        public static ErrorReply makeClientErrorCode(int code, string details)
        {
            ErrorReply reply = new ErrorReply();

            reply.errorCode    = code;
            reply.errorMessage = "There was a connection error";
            reply.errorDetails = details;

            switch (code)
            {
            case -1:
                reply.errorMessage = "Connection closed";
                break;

            case -2:
                reply.errorMessage = "Network error";
                break;
            }

            return(reply);
        }
예제 #4
0
        public void Init(OnErrorReply onConnected)
        {
            GetSerializerSettings();

            ServervilleWSComponent wsComp = ServervilleWSComponent.Get();

            wsComp.UpdateEvent += Update;

            string url = SV.ServerURL + "/websocket";

            ServerSocket = new WebSocket(url);

            ReplyCallbacks = new Dictionary <string, MessageReplyClosure>();

            ServerSocket.OnOpen += (object sender, EventArgs e) =>
            {
                if (onConnected != null)
                {
                    onConnected(null);
                }
            };

            ServerSocket.OnClose += OnWSClosed;

            ServerSocket.OnMessage += OnWSMessage;

            ServerSocket.OnError += (object sender, ErrorEventArgs e) =>
            {
                Debug.Log("Connection error: " + e.ToString());
                if (onConnected != null)
                {
                    onConnected(ErrorReply.makeClientErrorCode(-2, e.Message));
                }
            };

            ServerSocket.Connect();
        }
        public IEnumerator PostJSON(ServervilleClient sv, string url, string request, Type replyType, string sessionId, Action <object> onSuccess, OnErrorReply onError)
        {
            using (UnityWebRequest www = new UnityWebRequest(url))
            {
                www.method = UnityWebRequest.kHttpVerbPOST;

                UploadHandlerRaw upload = new UploadHandlerRaw(Encoding.UTF8.GetBytes(request));
                upload.contentType = "application/json";
                www.uploadHandler  = upload;
                www.disposeUploadHandlerOnDispose = true;

                DownloadHandlerBuffer buffer = new DownloadHandlerBuffer();
                www.downloadHandler = buffer;
                www.disposeDownloadHandlerOnDispose = true;

                if (sessionId != null)
                {
                    www.SetRequestHeader("Authorization", sessionId);
                }

                if (sv.LogMessages)
                {
                    Debug.Log("HTTP<- " + request);
                }

                yield return(www.Send());

                if (www.isError)
                {
                    ErrorReply err = ErrorReply.makeClientErrorCode(-2, www.error);
                    if (onError != null)
                    {
                        onError(err);
                    }

                    yield break;
                }

                if (sv.LogMessages)
                {
                    Debug.Log("HTTP-> " + buffer.text);
                }

                if (www.responseCode >= 200 && www.responseCode < 400)
                {
                    object reply = JsonConvert.DeserializeObject(buffer.text, replyType, ServervilleHttp.JsonSettings);
                    if (onSuccess != null)
                    {
                        onSuccess(reply);
                    }
                }
                else
                {
                    ErrorReply err = JsonConvert.DeserializeObject <ErrorReply>(buffer.text, ServervilleHttp.JsonSettings);
                    sv.OnServerError(err);
                    if (onError != null)
                    {
                        onError(err);
                    }
                }

                if (www.GetResponseHeader("X-Notifications") != null)
                {
                    // Pending notifications from the server!
                    GetNotifications(sv);
                }
            }
        }