private void OnBinaryMessageReceived(WebSocket webSocket, byte[] buffer) { Debug.Log("Binary Message received from server. Length: " + buffer.Length); labelMessage.text = "Length:" + buffer.Length; SocketResponse socketResponse; try { buffer = GZipHelper.Decompress(DESHelper.DecodeBytes(buffer, AppContext.GetInstance().getDesKey())); socketResponse = Serializer.Deserialize <SocketResponse>(new MemoryStream(buffer)); } catch (Exception) { Debug.LogError("SocketResponse parse error"); ShowMessage(ErrorCode.EC_PARSE_DATA_ERROR); DataHelper.GetInstance().CleanProfile(dbManager); return; } if (socketResponse != null) { String code = socketResponse.p1; if (!"0".Equals(code)) { ShowMessage(code); } else { HandleSocketResponse(socketResponse); } } }
void OnRequestFinished(HTTPRequest req, HTTPResponse resp) { HttpFinished(); switch (req.State) { // The request finished without any problem. case HTTPRequestStates.Finished: if (resp.StatusCode == 200) { bool isNormal = false; if (resp.Headers.ContainsKey("normal")) { isNormal = "true".Equals(resp.Headers["normal"][0]); } byte[] protoBytes = GZipHelper.Decompress(DESHelper.DecodeBytes(resp.Data, AppContext.GetInstance().getDesKey())); //Debug.Log("isNormal:" + isNormal); if (!isNormal) { SimpleApiResponse response = null; try { response = Serializer.Deserialize <SimpleApiResponse>(new MemoryStream(protoBytes)); Debug.Log("error response:" + response); } catch (Exception) { Debug.LogError("HttpMonoBehaviour SimpleApiResponse parse error"); ShowMessage(ErrorCode.EC_PARSE_DATA_ERROR); } if (response != null) { ShowMessage(response.code); } } else { Callback(protoBytes); } } else { ShowMessage(ErrorCode.EC_SERVER_ERROR); } break; // The request finished with an unexpected error. // The request's Exception property may contain more information about the error. case HTTPRequestStates.Error: Debug.LogError("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception")); ShowMessage(ErrorCode.EC_NETWORK_UNREACHED); break; // The request aborted, initiated by the user. case HTTPRequestStates.Aborted: Debug.LogWarning("Request Aborted!"); ShowMessage(ErrorCode.EC_NETWORK_UNREACHED); break; // Ceonnecting to the server timed out. case HTTPRequestStates.ConnectionTimedOut: Debug.LogError("Connection Timed Out!"); ShowMessage(ErrorCode.EC_NETWORK_TIMEOUT); break; // The request didn't finished in the given time. case HTTPRequestStates.TimedOut: Debug.LogError("Processing the request Timed Out!"); ShowMessage(ErrorCode.EC_NETWORK_TIMEOUT); break; default: Debug.LogError("Connection Error!"); ShowMessage(ErrorCode.EC_NETWORK_TIMEOUT); break; } }