Beispiel #1
0
        /// <summary>
        /// Parses the result of data read from the websocket.
        /// </summary>
        /// <param name="Result">The WebSocketReceiveResult.</param>
        /// <param name="ReadBuffer">The byte array buffer read from the socket stream.</param>
        private async Task ParseMessage(WebSocketReceiveResult Result, byte[] ReadBuffer)
        {
            if (!Result.EndOfMessage)
            {
                return;
            }
            RequestHistory.Add(DateTime.Now);
            while (RequestHistory.Count > 50)
            {
                RequestHistory.RemoveAt(0);
            }
            if (RequestHistory.Where(time => DateTime.Now - time < TimeSpan.FromSeconds(1)).Count() > 40)
            {
                await Player.WarnOrBan(this);
            }
            if (Result.MessageType == WebSocketMessageType.Text)
            {
                var trimmedString = Encoding.UTF8.GetString(TrimBytes(ReadBuffer));
                var jsonMessage   = JSON.Decode(trimmedString);

                StringMessageReceived?.Invoke(this, jsonMessage);
            }
            else if (Result.MessageType == WebSocketMessageType.Binary)
            {
                BinaryMessageReceived?.Invoke(this, TrimBytes(ReadBuffer));
            }
            else if (Result.MessageType == WebSocketMessageType.Close)
            {
                SocketClosed?.Invoke(this, EventArgs.Empty);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Triggers a <see cref="StringMessageReceived"/> event.
 /// </summary>
 /// <param name="e">Event arguments.</param>
 protected virtual void OnStringMessageReceived(StringMessageReceivedEventArgs e)
 {
     StringMessageReceived?.Invoke(this, e);
 }