public async Task SendResponseAsync(OM2MResponsePrimitive response) { // Correct primitiveContent if (response.Content != null && response.PrimitiveContent == null) { response.PrimitiveContent = new OM2MPrimitiveContent(); response.PrimitiveContent.Any = new List <object>(); response.PrimitiveContent.Any.Add(response.Content); } var responseMessage = OM2MJsonSerializer.Serialize(response); var responseMessageData = Encoding.UTF8.GetBytes(responseMessage); ArraySegment <byte> buffer = new ArraySegment <byte>(responseMessageData); await WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); }
public OM2MResponsePrimitive SendRequest(OM2MRequestPrimitive request) { // Correct primitiveContent if (request.Content != null && request.PrimitiveContent == null) { request.PrimitiveContent = new OM2MPrimitiveContent(); request.PrimitiveContent.Any = new List <object>(); request.PrimitiveContent.Any.Add(request.Content); } var requestMessage = OM2MJsonSerializer.Serialize(request); var requestMessageData = Encoding.UTF8.GetBytes(requestMessage); ArraySegment <byte> buffer = new ArraySegment <byte>(requestMessageData); using (var resultWait = new ResultWait <OM2MResponsePrimitive>()) { OM2MResponsePrimitive response; m_waittingResponses.Add(request.RequestIdentifier, resultWait); try { Task.WaitAll(WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None)); response = resultWait.Wait(TimeSpan.FromSeconds(30)); } finally { m_waittingResponses.Remove(request.RequestIdentifier); } return(response); } }