public void OnStringReceived(string message)
        {
            Debug.WriteLine(message);
            StringReceived?.Invoke(message);

            if (message.Length > 2)
            {
                //eliminate badly embedded characters if found
                message = message.Replace("{}", "");

                var msg = MessageFactory.FromMessage(message);
                if (msg != null)
                {
                    OnMessageReceived(msg);
                }
            }
        }
    private bool _parse_message()
    {
        int offset         = 0;
        int message_length = 0;

        if (read_more_message)
        {
            _last_message_length -= bytes.Length;
            message_length        = _last_message_length;
            if (message_length < bytes.Length)
            {
                message_length += 8;
            }
        }
        else
        {
            _results.Clear();
            var  trigger    = bytes[0];
            var  magic_byte = bytes[1];
            bool is_text    = (0x1 & trigger) != 0;
            bool is_fin     = (0x80 & trigger) != 0;

            if (trigger == 0x88)
            {
                connected = false;
                return(false);
            }

            /*
             * text = 0x81
             * binary = 0x82
             * close 0x88
             * ping 0x89
             * pong -0x8A
             */


            if (!is_fin)
            {
                return(true);
            }

            if (!is_text)
            {
                return(true);
            }

            //If the second byte minus 128 is between 0 and 125, this is the length of message.
            //If it is 126, the following 2 bytes (16-bit unsigned integer), if 127, the following 8 bytes (64-bit unsigned integer) are the length.
            var r             = magic_byte - 128;
            var key_starts_at = 0;
            if (r <= 125)
            {
                key_starts_at  = 2;
                message_length = r;
            }
            else if (r == 126)
            {
                key_starts_at  = 4;
                message_length = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
            }
            else if (r == 127)
            {
                key_starts_at = 10;
                for (var m = 7; m >= 0; --m)
                {
                    message_length += bytes[m] << (8 * (7 - m));
                }
            }
            else
            {
                // not documented
            }
            //// because its encoded
            _last_message_length = message_length;
            Array.Copy(bytes, key_starts_at, _last_key, 0, 4);

            offset = key_starts_at + 4;
        }
        for (var mx = 0; mx < message_length && offset + mx < bytes.Length; ++mx)
        {
            bytes[offset + mx] = (byte)(bytes[offset + mx] ^ _last_key[mx % 4]);
        }

        var new_result = System.Text.Encoding.ASCII.GetString(bytes, offset, Math.Min(message_length, bytes.Length - offset));

        _results.Append(new_result);
        read_more_message = message_length > bytes.Length;

        if (!read_more_message)
        {
            try
            {
                StringReceived?.Invoke(_results.ToString());
            }
            catch (Exception ex)
            {
            }
        }

        return(true);
    }
예제 #3
0
 internal void OnStringReceived(StringEventArgs eventArgs)
 {
     StringReceived?.Invoke(session, eventArgs);
 }