Exemplo n.º 1
0
        public async Task Send <T>(T evt, CancellationToken cancellation = default(CancellationToken))
        {
            var typeName = ExtensionsClient.GetNotificationMessageTypeName(evt.GetType());

            if (typeName == null)
            {
                throw new InvalidOperationException($"{evt.GetType().Name} does not have an associated typeName");
            }
            JObject jobj       = new JObject();
            var     serialized = JsonConvert.SerializeObject(evt, _SerializerSettings);
            var     data       = JObject.Parse(serialized);

            jobj.Add(new JProperty("type", new JValue(typeName)));
            jobj.Add(new JProperty("data", data));
            var bytes = UTF8.GetBytes(jobj.ToString());

            using (var cts = new CancellationTokenSource(5000))
            {
                using (var cts2 = CancellationTokenSource.CreateLinkedTokenSource(cancellation))
                {
                    await Socket.SendAsync(new ArraySegment <byte>(bytes), WebSocketMessageType.Text, true, cts2.Token);
                }
            }
        }
Exemplo n.º 2
0
        private object ParseMessage(ArraySegment <byte> buffer)
        {
            var str = UTF8.GetString(buffer.Array, 0, buffer.Count);

            return(ExtensionsClient.ParseNotificationMessage(str, _SerializerSettings));
        }