Пример #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);
        }
Пример #2
0
        private void HandleStringMessage(string messageStr)
        {
            try
            {
                int split1 = messageStr.IndexOf(':');
                if (split1 < 1)
                {
                    Debug.Log("Incorrectly formatted message");
                    return;
                }

                string messageType = messageStr.Substring(0, split1);
                if (messageType == "N")
                {
                    // Server push message
                    int split2 = messageStr.IndexOf(':', split1 + 1);
                    if (split2 < 0)
                    {
                        Debug.Log("Incorrectly formatted message");
                        return;
                    }

                    string notificationType = messageStr.Substring(split1 + 1, split2 - (split1 + 1));
                    string notificationJson = messageStr.Substring(split2 + 1);

                    SV.OnServerNotification(notificationType, notificationJson);
                }
                else if (messageType == "E" || messageType == "R")
                {
                    // Reply
                    int split2 = messageStr.IndexOf(':', split1 + 1);
                    if (split2 < 0)
                    {
                        Debug.Log("Incorrectly formatted message");
                        return;
                    }

                    string messageNum = messageStr.Substring(split1 + 1, split2 - (split1 + 1));

                    string messageJson = messageStr.Substring(split2 + 1);

                    bool isError = false;
                    if (messageType == "E")
                    {
                        isError = true;
                    }

                    MessageReplyClosure callback = ReplyCallbacks[messageNum];
                    ReplyCallbacks.Remove(messageNum);

                    callback(isError, messageJson);
                }
            }
            catch (Exception exc)
            {
                // Error handling message
                Debug.Log("Error handling message: " + exc.ToString());
            }
        }