Ejemplo n.º 1
0
        public async Task SendWebSocket(ISakuraIOMessage msg)
        {
            var jsonStr = SerializeChannelMessage(msg);
            var buff    = new ArraySegment <byte>((new UTF8Encoding()).GetBytes(jsonStr));

            Console.WriteLine(jsonStr);

            await sakuraioSocket.SendAsync(buff, WebSocketMessageType.Text, true, CancellationToken.None);
        }
Ejemplo n.º 2
0
        private string SerializeChannelMessage(ISakuraIOMessage message)
        {
            Type type;

            if (message.Type == "channels")
            {
                type = typeof(ChannelMessage);
            }
            else
            {
                throw new InvalidDataException("messageに含まれるtypeの値が不正です");
            }

            using (var ms = new MemoryStream())
                using (var sr = new StreamReader(ms))
                {
                    var serializer = new DataContractJsonSerializer(type);
                    serializer.WriteObject(ms, message);
                    ms.Position = 0;
                    return(sr.ReadToEnd());
                }
        }