Example #1
0
 public static int GetOpCode(WebSocketMessageType messageType)
 {
     switch (messageType)
     {
         case WebSocketMessageType.Text: return Constants.OpCodes.TextFrame;
         case WebSocketMessageType.Binary: return Constants.OpCodes.BinaryFrame;
         case WebSocketMessageType.Close: return Constants.OpCodes.CloseFrame;
         default: throw new NotImplementedException(messageType.ToString());
     }
 }
        /// <summary>
        /// Sends the async.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="type">The type.</param>
        /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken = default)
        {
            await _sendResource.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (!Enum.TryParse(type.ToString(), true, out System.Net.WebSockets.WebSocketMessageType nativeType))
                {
                    _logger.LogWarning("Unrecognized WebSocketMessageType: {0}", type.ToString());
                }

                var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token);

                await _client.SendAsync(new ArraySegment <byte>(bytes), nativeType, true, linkedTokenSource.Token).ConfigureAwait(false);
            }
            finally
            {
                _sendResource.Release();
            }
        }
Example #3
0
        public static int GetOpCode(WebSocketMessageType messageType)
        {
            switch (messageType)
            {
            case WebSocketMessageType.Text: return(Constants.OpCodes.TextFrame);

            case WebSocketMessageType.Binary: return(Constants.OpCodes.BinaryFrame);

            case WebSocketMessageType.Close: return(Constants.OpCodes.CloseFrame);

            default: throw new NotImplementedException(messageType.ToString());
            }
        }
Example #4
0
            public WebSocketFrame(byte[] bytes, WebSocketMessageType type)
            {
                switch (type)
                {
                case WebSocketMessageType.Text:
                    MessageBytes    = bytes;
                    MessageAsString = Encoding.UTF8.GetString(bytes);
                    break;

                case WebSocketMessageType.Binary:
                    MessageBytes    = bytes;
                    MessageAsString = null;
                    break;

                default:
                    throw new NotSupportedException(type.ToString());
                }
            }