private void SendRaw(DispatcherMessage message) { message.seq = _sequenceNumber++; var asJson = JsonConvert.SerializeObject(message); byte[] jsonBytes = s_encoding.GetBytes(asJson); string header = string.Format(CultureInfo.InvariantCulture, "Content-Length: {0}{1}", jsonBytes.Length, TWO_CRLF); byte[] headerBytes = s_encoding.GetBytes(header); byte[] data = new byte[headerBytes.Length + jsonBytes.Length]; System.Buffer.BlockCopy(headerBytes, 0, data, 0, headerBytes.Length); System.Buffer.BlockCopy(jsonBytes, 0, data, headerBytes.Length, jsonBytes.Length); try { _outputStream.Write(data, 0, data.Length); _outputStream.Flush(); } catch (Exception e) { Debug.Fail("Exception encountered during stream write: " + e.Message); } }
private void SendMessage(DispatcherMessage message) { if (message.type == "response") { ResponseCallback?.Invoke(string.Format(CultureInfo.InvariantCulture, " R: {0}", JsonConvert.SerializeObject(message))); } else if (message.type == "event") { DispatcherEvent e = (DispatcherEvent)message; ResponseCallback?.Invoke(string.Format("E {0}: {1}", e.eventType, JsonConvert.SerializeObject(e.body))); } SendRaw(message); }