//send nomal coap request private Request PerformCoAP(Request.Method method, Uri uri, Boolean messageVisible, Boolean observe) { if (null != _currentRequest) { _currentRequest.Cancel(); } Request request = Request.Create(method); _currentRequest = request; request.URI = uri; if (method == Request.Method.POST || method == Request.Method.PUT) { request.SetPayload(this.Payload, MediaType.TextPlain); } if (messageVisible) { request.Responding += new EventHandler <ResponseEventArgs>(request_Responding); request.Responded += new EventHandler <ResponseEventArgs>(request_Responded); } request.ResponseQueueEnabled = true; request.Execute(); return(request); }
//send observe request private void PerformObserve(Request.Method method, Uri uri) { if (null != _currentRequest) { _currentRequest.Cancel(); } Request request = Request.Create(method); _currentRequest = request; request.URI = uri; request.AddOption(Option.Create(OptionType.Observe, 60)); request.Token = TokenManager.Instance.AcquireToken(false); request.Responded += new EventHandler <ResponseEventArgs>(observe_Responded); _observingRequest = request; //try Websocket connect try { //wait for websocket connect Thread.Sleep(5000); observedSession = coapWebSocektService.SessionList[coapWebSocektService.SessionList.Count - 1]; request.ResponseQueueEnabled = true; request.Execute(); } catch { _observingRequest.RemoveOptions(OptionType.Observe); _observingRequest.Execute(); _observingRequest = null; Console.WriteLine("Bad websocket connect"); this.clientSocket.Send(Encoding.UTF8.GetBytes("Bad websocket connect")); this.clientSocket.Disconnect(false); this.clientSocket.Dispose(); } }