/// <summary>
        /// After protocol handshake is completed, this method is called to handle events for the specified client
        /// </summary>
        public async Task HandleConnection(IConnectionInfo info, ProtocolHandshakeResult handshakeResult)
        {
            //if user makes a mistake in ready method, we should not interrupt connection handling
            try
            {
                await _handler.Ready(_server, (WsServerSocket)handshakeResult.Socket);
            }
            catch (Exception e)
            {
                if (_server.Logger != null)
                {
                    _server.Logger.LogException("Unhandled Exception", e);
                }
            }

            WebSocketReader reader = new WebSocketReader();
            Stream          stream = info.GetStream();

            while (info.Socket != null && info.Socket.IsConnected)
            {
                WebSocketMessage message = await reader.Read(stream);

                if (message == null)
                {
                    info.Close();
                    return;
                }

                if (handshakeResult.Socket.SmartHealthCheck)
                {
                    handshakeResult.Socket.KeepAlive();
                }

                await ProcessMessage(info, handshakeResult.Socket, message);
            }
        }
 /// <summary>
 /// Creates byte array data of only message content
 /// </summary>
 public async Task <byte[]> CreateContent(WebSocketMessage value)
 {
     await using MemoryStream ms = new MemoryStream();
     value.Content.WriteTo(ms);
     return(ms.ToArray());
 }
Esempio n. 3
0
        /// <summary>
        /// Sends string message to client
        /// </summary>
        public async Task SendAsync(string message)
        {
            byte[] data = await _writer.Create(WebSocketMessage.FromString(message));

            Send(data);
        }
Esempio n. 4
0
 /// <summary>
 /// Sends string message to client
 /// </summary>
 public bool Send(string message)
 {
     byte[] data = _writer.Create(WebSocketMessage.FromString(message)).Result;
     return(Send(data));
 }
Esempio n. 5
0
        /// <summary>
        /// Sends websocket message to client
        /// </summary>
        public async Task <bool> SendAsync(WebSocketMessage message)
        {
            byte[] data = await _writer.Create(message);

            return(Send(data));
        }
Esempio n. 6
0
 /// <summary>
 /// Sends websocket message to client
 /// </summary>
 public bool Send(WebSocketMessage message)
 {
     byte[] data = _writer.Create(message).Result;
     return(Send(data));
 }