コード例 #1
0
        /// <summary>
        /// Sends a WebSocket frame to the client.
        /// </summary>
        /// <param name="dt">The data to be sent.</param>
        /// <param name="msgType">The type of message sent.</param>
        /// <returns>Completed Task</returns>
        public virtual async Task SendAsync(ArraySegment <byte> dt, WSOpcodeType msgType)
        {
            if (!handShaked)
            {
                return;
            }
            if (disposed)
            {
                return;
            }
            if (closed)
            {
                return;
            }
            var data = dt.Array.SubArray(dt.Offset, dt.Count);

            //if (data.Array == null)
            //throw new ArgumentNullException($"Inner array of argument \"{nameof(data)}\" cannot be equal to null.");
            byte[] d = WSArrayHelpers.ToFrameData(data, msgType);

            if (secure)
            {
                await secureStream.WriteAsync(d, 0, d.Length);
            }
            else
            {
                await client.GetStream().WriteAsync(d, 0, d.Length);
            }

            if (msgType == WSOpcodeType.Close && !disposed)
            {
                await DisposeAsync();
            }
        }
コード例 #2
0
        /// <summary>
        /// Receives a WebSocket packet from the client.
        /// </summary>
        /// <returns>Returns the external data, the status code of closing the connection (if there is one) and the opcode type of the message.</returns>
        public virtual async Task <Tuple <byte[], WebSocketCloseStatus, WSOpcodeType>?> ReceiveAsync()
        {
            if (!handShaked)
            {
                return(null);
            }
            if (disposed)
            {
                return(null);
            }
            if (closed)
            {
                return(null);
            }

            uint buff = 0;

            if (config.BufferSize != null)
            {
                buff = config.BufferSize.GetValueOrDefault();
            }
            else if (config.DynamicBufferSize != null)
            {
                buff = await config.DynamicBufferSize(this);
            }
            else
            {
                buff = 65535;
            }

            byte[] data = new byte[buff];
            if (secure)
            {
                int i = await secureStream.ReadAsync(data, 0, data.Length);

                data = data.SubArray(0, i);
            }
            else
            {
                int i = await client.GetStream().ReadAsync(data, 0, data.Length);

                data = data.SubArray(0, i);
            }
            ;
            var t = WSArrayHelpers.ConvertFrame(data);

            if (t == null)
            {
                await DisposeAsync();

                return(null);
            }
            if (t.Item4 == WSOpcodeType.Close)
            {
                await CloseAsync(WebSocketCloseStatus.Empty, null);
                await DisposeAsync(false);
            }
            return(new Tuple <byte[], WebSocketCloseStatus, WSOpcodeType>(t.Item1, t.Item2, t.Item4));
        }