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); } } }