/// <summary> /// Enqueues the event message. /// </summary> /// <param name="eventCode">Event code.</param> /// <param name="parameters">Parameters.</param> public static void EnqueueEventMessage(PlayEventCode eventCode, params object[] parameters) { EnqueueEventMessage(new PlayEventMessage() { MethodName = eventCode.ToString(), MethodParameters = parameters }); }
internal static void RunSocketCommand(PlayCommand command, PlayEventCode eventCode = PlayEventCode.None, Action <PlayCommand, PlayResponse> done = null) { var encoded = command.SokcetEncode; Play.Log("socket=>" + encoded); Play.RoomConnection.Send(encoded); var repsonseListener = new SocketResponseListener() { Command = command, Done = done, EventCode = eventCode }; Play.SubscribeNoticeReceived(repsonseListener); //Action<string> onMessage = null; //LogCommand(command, null, CommandType.WebSocket); //onMessage = (messgae) => //{ // var messageJson = Json.Parse(messgae) as IDictionary<string, object>; // if (messageJson.Keys.Contains("i")) // { // if (command.Body["i"].ToString() == messageJson["i"].ToString()) // { // RoomConnection.OnMessage -= onMessage; // var response = new PlayResponse(messageJson); // LogCommand(null, response, CommandType.WebSocket); // if (response.IsSuccessful) // { // if (done != null) // { // done(command, response); // } // } // if (eventCode != PlayEventCode.None) // { // var next = PlayStateMachine.Next(eventCode, response); // if (response.IsSuccessful) // { // Play.InvokeEvent(next); // } // else // { // Play.InvokeEvent(next, response.ErrorCode, response.ErrorReason); // } // } // } // } //}; //RoomConnection.OnMessage += onMessage; }
public static PlayEventCode Next(PlayEventCode eventCode, PlayResponse response) { if (response.StatusCode > 201 || response.StatusCode < 200) { return(Failed(eventCode)); } else { return(Successed(eventCode)); } }
internal static void RunHttpCommand(PlayCommand command, PlayEventCode eventCode = PlayEventCode.None, Action <PlayCommand, PlayResponse> done = null) { if (eventCode != PlayEventCode.None) { Play.InvokeEvent(eventCode); } var httpRequest = command.HttpEncode; Play.ExecuteHttp(httpRequest, (tuple => { int statusCode = (int)HttpStatusCode.BadRequest; IDictionary <string, object> body = new Dictionary <string, object>(); try { body = Json.Parse(tuple.Item2) as IDictionary <string, object>; statusCode = (int)tuple.Item1; } catch { } var response = new PlayResponse() { Body = body as IDictionary <string, object>, StatusCode = (int)tuple.Item1 }; if (response.IsSuccessful) { LogCommand(command, response); if (done != null) { done(command, response); } } else { LogCommand(command, response, printer: ErrorLogger); } if (eventCode != PlayEventCode.None) { var next = PlayStateMachine.Next(eventCode, response); if (response.IsSuccessful) { Play.InvokeEvent(next); } else { Play.InvokeEvent(next, response.ErrorCode, response.ErrorReason); } } })); }
/// <summary> /// /// </summary> /// <param name="eventCode"></param> /// <param name="parameters"></param> public static void InvokeEvent(PlayEventCode eventCode, params object[] parameters) { #if UNITY EnqueueEventMessage(eventCode, parameters); #else Play.EventBehaviours.When(kv => kv.Key == eventCode.ToString()).Every(kv => { kv.Value.Every(b => { var callbackMethod = Play.Find <PlayEventAttribute>(b, kv.Key); if (callbackMethod != null) { callbackMethod.Invoke(b, parameters.Length > 0 ? parameters : null); } }); }); #endif }
/// <summary> /// Dequeues the event message. /// </summary> /// <param name="eventCode">Event code.</param> public static void DequeueEventMessage(PlayEventCode eventCode) { }
public static PlayEventCode Failed(PlayEventCode eventCode) { return((PlayEventCode)((int)(eventCode + 1) * -1)); }
public static PlayEventCode Successed(PlayEventCode eventCode) { return((PlayEventCode)(eventCode + 1)); }