private void QueueRequest(string jsonRequestData, S2SCallback callback) { #if DOT_NET //create new request HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(ServerURL); //customize request httpRequest.Method = "POST"; httpRequest.ContentType = "application/json; charset=utf-8"; #endif #if USE_WEB_REQUEST //create new request UnityWebRequest httpRequest = UnityWebRequest.Post(ServerURL, new Dictionary<string, string>()); //customize request httpRequest.SetRequestHeader("Content-Type", "application/json; charset=utf-8"); #endif //store request info S2SRequest req = new S2SRequest(); req.request = httpRequest; req.requestData = jsonRequestData; req.callback = callback; //add to requestqueue _requestQueue.Add(req); SendData(req.request, req.requestData); }
public void OnAuthenticationCallback(string responseString) { Dictionary<string, object> response = null; try { response = (Dictionary<string, object>)JsonReader.Deserialize(responseString); } catch { return; } if (response != null) { ////check if its a failure if (!response.ContainsKey("reason_code")) { Dictionary<string, object> data = (Dictionary<string, object>)response["data"]; if (data.ContainsKey("sessionId") && data.ContainsKey("heartbeatSeconds")) { SessionId = (string)data["sessionId"]; if (data.ContainsKey("heartbeatSeconds")) { _heartbeatSeconds = (int)data["heartbeatSeconds"]; // get the heartbeat seconds from braincloud. } ResetHeartbeat(); _state = State.Authenticated; for (int i = 0; i < _waitingForAuthRequestQueue.Count; i++) { S2SRequest req = (S2SRequest)_waitingForAuthRequestQueue[i]; Request(req.requestData, req.callback); } } } //clear in case a reauthentication is needed. _waitingForAuthRequestQueue.Clear(); } }
/** * Send an S2S request. * * @param json S2S operation to be sent as a string * @param callback Callback function */ public void Request(string jsonRequestData, S2SCallback callback) { if (_autoAuth == true) { if (!(_state == State.Authenticated) && _packetId == 0) //this is an authentication request no matter what { Authenticate(OnAuthenticationCallback); } } if (!(_state == State.Authenticated)) // these are the requests that have been made that are awaiting authentication. We NEED to store the request so we can properly call this function back for additional requests that are made after authenitcation. { S2SRequest nonAuthRequest = new S2SRequest(); nonAuthRequest.requestData = jsonRequestData; nonAuthRequest.callback = callback; _waitingForAuthRequestQueue.Add(nonAuthRequest); } else { QueueRequest(jsonRequestData, callback); } }
public void RunCallbacks() { if (activeRequest.request == null) //if there is no active request, make the first in the queue the active request. { if (_requestQueue.Count != 0) //make sure the queue isn't empty { activeRequest = (S2SRequest)_requestQueue[0]; } } else //on an update, if we have an active request we need to process it. This is VITAL for WEB_REQUEST becasue it handles requests differently than DOT_NET { #if DOT_NET HttpWebResponse csharpResponse = null; try { LogString("Sending Request: " + activeRequest.requestData); csharpResponse = (HttpWebResponse)activeRequest.request.GetResponse(); } catch (Exception e) { LogString("S2S Failed: " + e.ToString()); activeRequest.request.Abort(); activeRequest.request = null; _requestQueue.RemoveAt(0); return; } #endif #if USE_WEB_REQUEST string unityResponse = null; if(activeRequest.request.downloadHandler.isDone) { LogString("Sending Request: " + activeRequest.request); unityResponse = activeRequest.request.downloadHandler.text; } if(!string.IsNullOrEmpty(activeRequest.request.error)) { LogString("S2S Failed: " + activeRequest.request.error); activeRequest.callback(activeRequest.request.error); activeRequest.request.Abort(); activeRequest.request = null; _requestQueue.RemoveAt(0); } #endif #if DOT_NET if (csharpResponse != null) { //get the response body string responseString = ReadResponseBody(csharpResponse); #endif #if USE_WEB_REQUEST if (unityResponse != null) { //get the response body string responseString = unityResponse; #endif Dictionary<string, object> responseBody = (Dictionary<string, object>)JsonReader.Deserialize(responseString); if (responseBody.ContainsKey("messageResponses")) { //extract the map array Dictionary<string, object>[] messageArray = (Dictionary<string, object>[])responseBody["messageResponses"]; //extract the map from the map array Dictionary<string, object> messageResponses = (Dictionary<string, object>)messageArray.GetValue(0); if ((int)messageResponses["status"] == 200) //success 200 { LogString("S2S Response: " + responseString); //callback if (activeRequest.callback != null) { activeRequest.callback(JsonWriter.Serialize((Dictionary<string, object>)messageResponses)); } //remove the request finished request form the queue _requestQueue.RemoveAt(0); } else //failed { //check if its a session expiry if (responseBody.ContainsKey("reason_code")) { if ((int)responseBody["reason_code"] == SERVER_SESSION_EXPIRED) { LogString("S2S session expired"); activeRequest.request.Abort(); Disconnect(); return; } } LogString("S2S Failed: " + responseString); //callback if (activeRequest.callback != null) { activeRequest.callback(JsonWriter.Serialize((Dictionary<string, object>)messageResponses)); } activeRequest.request.Abort(); //remove the finished request from the queue _requestQueue.RemoveAt(0); } } activeRequest.request = null; //reset the active request so that it can move onto the next request. } } //do a heartbeat if necessary. if (_state == State.Authenticated) { if (DateTime.Now.Subtract(_lastHeartbeat) >= _heartbeatTimer) { SendHeartbeat(OnHeartbeatCallback); ResetHeartbeat(); } } }