/// <inheritdoc />
        public async ValueTask <bool> Send(ISocketCacheKey key, IMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var webSocket = _socketManager.Get(key);

            if (webSocket == null)
            {
                return(false);
            }

            if (webSocket.State != System.Net.WebSockets.WebSocketState.Open || webSocket.CloseStatus.HasValue)
            {
                return(false);
            }

            var str = JsonConvert.SerializeObject(message);
            await webSocket.SendAsync(new ArraySegment <byte>(Encoding.UTF8.GetBytes(str)),
                                      System.Net.WebSockets.WebSocketMessageType.Text, true, default);

            return(true);
        }
        /// <inheritdoc />
        public bool Register(ISocketCacheKey key, EventHandler <MessageReceiveEventArgs> handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            var socket = _socketManager.Get(key);

            if (socket == null)
            {
                return(false);
            }

            var receiver = WebSocketReceiver.GetReceiver(socket);

            if (receiver == null)
            {
                return(false);
            }
            receiver.MessageHandler += handler;
            return(true);
        }