protected void sendFailure(string cause, string data) { var failure = new WebSocketFailureMessage(); failure.Cause = cause; failure.Data = data; Send(_marshaller.Marshal(failure)); }
protected void HandleBuzzer(WebSocketRequestMessageJson msg) { var res = new WebSocketResponseMessageJson(WebSocketMessageKind.BuzzerResponse); res.Ref = msg.ID; try { res.Data = _dispatcher.Dispatch(_context, msg.Service, msg.Method, msg.Data); } catch (Exception ex) { res.Kind = WebSocketMessageKind.BuzzerFailure; res.Data = ex.Message; } _ws.Send(_marshaller.Marshal(res)); }
public void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx) { try { var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", endpoint, service, method)); request.Timeout = Timeout * 1000; request.Method = payload == null ? "GET" : "POST"; if (HttpHeaders != null) { foreach (var key in HttpHeaders.AllKeys) { foreach (var value in HttpHeaders.GetValues(key)) { request.Headers.Add(key, value); } } } if (payload != null) { var data = Marshaller.Marshal <I>(payload); if (data == null) { throw new TransportException("HttpTransport only supports Marshallers which return a string."); } request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(Encoding.UTF8.GetBytes(data), 0, data.Length); } } using (var response = (HttpWebResponse)request.GetResponse()) { using (var respStream = response.GetResponseStream()) { using (var reader = new StreamReader(respStream, Encoding.UTF8)) { string jsonString = reader.ReadToEnd(); if (string.IsNullOrEmpty(jsonString)) { throw new TransportException("Empty Response"); } var data = Marshaller.Unmarshal <O>(jsonString); callback.Success(data); } } } } catch (Exception ex) { callback.Failure( new TransportException("Unexpected exception occured during async request.", ex) ); } }
protected UnityWebRequest PrepareRequest <I>(string service, string method, I payload) { var request = new UnityWebRequest { downloadHandler = new DownloadHandlerBuffer(), url = string.Format("{0}/{1}/{2}", _endpoint, service, method), method = payload == null ? "GET" : "POST" }; if (_headers != null) { foreach (KeyValuePair <string, string> kv in _headers) { request.SetRequestHeader(kv.Key, kv.Value); } } if (_auth != null) { request.SetRequestHeader("Authorization", _auth.ToValue()); } // API cached requests might be a pain, let's suppress that request.SetRequestHeader(CACHE_CONTROL_HEADER_KEY, CACHE_CONTROL_HEADER_VALUES); request.SetRequestHeader(PRAGMA_HEADER_KEY, PRAGMA_HEADER_VALUES); if (payload != null) { var data = _marshaller.Marshal <I>(payload); if (data == null) { throw new TransportException("UnityTransport only supports Marshallers which return a string."); } byte[] bytes = Encoding.UTF8.GetBytes(data); request.uploadHandler = new UploadHandlerRaw(bytes) { contentType = "application/json" }; } request.timeout = _timeout; return(request); }
public virtual void Send <I, O>(string service, string method, I payload, ClientTransportCallback <O> callback, C ctx) { try { var request = (HttpWebRequest)WebRequest.Create(string.Format("{0}/{1}/{2}", _endpoint, service, method)); request.Timeout = _timeout * 1000; request.Method = payload == null ? "GET" : "POST"; if (_headers != null) { foreach (var key in _headers.Keys) { request.Headers.Add(key, _headers[key]); } } if (_auth != null) { request.Headers.Add("Authorization", _auth.ToValue()); } var state = new RequestState <O>(request, callback, null); if (payload == null) { request.BeginGetResponse(ProcessResponse <O>, state); } else { var data = _marshaller.Marshal <I>(payload); state.JsonString = data; request.ContentType = "application/json"; request.BeginGetRequestStream(ProcessStreamRequest <O>, state); } } catch (Exception ex) { callback.Failure( new TransportException("Unexpected exception occured during async request.", ex) ); } }