Exemplo n.º 1
0
        private static async Task SendAsync(WebSocket webSocket, CancellationToken cancellationToken)
        {
            Memory <byte> buffer = new byte[1024 * 1024];

            //
            // speech.config
            //
            {
                var message = new TextMessage();

                message.Path      = "speech.config";
                message.RequestId = Guid.NewGuid();
                message.Headers.Add("Content-Type", "application/json;charset=utf-8");

                message.Body = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "speech.config.json"))));

                int totalLength = s_textMessageSerializer.Serialize(message, buffer.Span);

                await webSocket.SendAsync(buffer.Slice(0, totalLength), WebSocketMessageType.Text, true, cancellationToken);
            }

            //
            // send audio file
            //
            {
                var message = new BinaryMessage();

                message.Path      = "audio";
                message.RequestId = Guid.NewGuid();
                message.Headers.Add("Content-Type", "audio/x-wav");

                int bodyLength = 0;
                using (var stream = File.OpenRead(Path.Combine(Directory.GetCurrentDirectory(), "data", "b0050.wav")))
                {
                    bodyLength += await stream.ReadAsync(buffer, cancellationToken);
                }

                // Rewrite wav header.
                WavHeaderWriter.TryWritePcmWavHeader(buffer.Span, 1, 16000, 16, 0);

                message.Body = buffer.Slice(0, bodyLength).ToArray();

                int totalLength = s_binaryMessageSerializer.Serialize(message, buffer.Span);

                await webSocket.SendAsync(buffer.Slice(0, totalLength), WebSocketMessageType.Binary, true, cancellationToken);
            }
        }
Exemplo n.º 2
0
        public static async Task SendMessageAsync(this Stream stream, object messageObject, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var serializedMessage = BinaryMessageSerializer.Serialize(messageObject);
            var sizeBytes         = serializedMessage.Length.ToHexBytes();

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Write(sizeBytes, 0, sizeBytes.Length);
                memoryStream.Write(ControlSeq, 0, ControlSeq.Length);
                memoryStream.Write(serializedMessage, 0, serializedMessage.Length);

                memoryStream.Seek(0, SeekOrigin.Begin);

                await memoryStream.CopyToAsync(stream, BufferSize, cancellationToken).ConfigureAwait(false);
            }
        }
        public void The_binary_formatter_should_make_mouths_happy()
        {
            byte[] serializedMessageData;

            IMessageSerializer serializer = new BinaryMessageSerializer();

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }
        public void The_binary_formatter_should_make_mouths_happy()
        {
            byte[] serializedMessageData;

            IMessageSerializer serializer = new BinaryMessageSerializer();

            using (MemoryStream output = new MemoryStream())
            {
                serializer.Serialize(output, _message);

                serializedMessageData = output.ToArray();
            }

            using (MemoryStream input = new MemoryStream(serializedMessageData))
            {
                SerializationTestMessage receivedMessage = serializer.Deserialize(input) as SerializationTestMessage;

                Assert.AreEqual(_message, receivedMessage);
            }
        }