Exemple #1
0
 /// <summary>
 /// Queues an event for sending over the Websocket
 /// </summary>
 /// <param name="message"></param>
 public virtual void Queue <TPayload>(
     GatewayEvent <TPayload> gatewayEvent,
     Newtonsoft.Json.JsonSerializerSettings settings = null
     )
 {
     Socket.Send(gatewayEvent.Serialize(settings));
 }
Exemple #2
0
        private void OnReady(GatewayEvent gatewayEvent)
        {
            var ready = gatewayEvent.GetData <Ready>();

            lastReady = ready;

            FireEventOnDelegate(gatewayEvent, Ready);
        }
Exemple #3
0
        private async Task SendHeartbeatAsync()
        {
            var heartbeatEvent = new GatewayEvent
            {
                Operation = OperationCode.Heartbeat.ToInt(),
                Data      = lastGatewayEvent?.SequenceNumber ?? 0
            };

            await _webMessageSocket.SendJsonObjectAsync(heartbeatEvent);
        }
Exemple #4
0
        private int GetEventId(GatewayEvent gatewayEvent)
        {
            int val = -1;

            if (gatewayEvent != null)
            {
                int.TryParse(gatewayEvent.Id, out val);
            }

            return(val);
        }
Exemple #5
0
        private async void IdentifySelfToGateway()
        {
            var identifyEvent = new GatewayEvent
            {
                Type      = EventNames.IDENTIFY,
                Operation = OperationCode.Identify.ToInt(),
                Data      = await GetIdentityAsync()
            };

            await _webMessageSocket.SendJsonObjectAsync(identifyEvent);
        }
Exemple #6
0
 private void OnChannelDeleted(GatewayEvent gatewayEvent)
 {
     if (IsChannelAGuildChannel(gatewayEvent))
     {
         FireEventOnDelegate(gatewayEvent, GuildChannelDeleted);
     }
     else
     {
         FireEventOnDelegate(gatewayEvent, DirectMessageChannelDeleted);
     }
 }
Exemple #7
0
        internal async Task GatewayEvents_InvalidSession(string rawJson, GatewayEvent <object> ev)
        {
            //"Clients are limited to 1 identify every 5 seconds; if they exceed this limit, the gateway will respond with an Opcode 9 Invalid Session."

            //"It's also possible that your client cannot reconnect in time to resume, in which case the client will receive a Opcode 9 Invalid Session
            //  and is expected to wait a random amount of time—between 1 and 5 seconds—then send a fresh Opcode 2 Identify"

            //Wait 5 seconds before re-connecting
            await Task.Delay(5000);

            //Invalidate session then reidentify
            _session = null;
            Configuration.LastSession  = null;
            Configuration.LastSequence = null;
            Identify();
        }
 private static GatewayEvent CreateGatewayEvent(GatewayEvent connectionEvent)
 {
     if (connectionEvent != null)
     {
         GatewayEvent gatewayEvent = new GatewayEvent();
         gatewayEvent.Data      = connectionEvent.Data;
         gatewayEvent.Id        = connectionEvent.Id;
         gatewayEvent.Message   = connectionEvent.Message;
         gatewayEvent.Timestamp = connectionEvent.Timestamp;
         return(gatewayEvent);
     }
     else
     {
         return(null);
     }
 }
Exemple #9
0
        private Task SendResumeAsync()
        {
            var Content = new ResumePayload()
            {
                SessionId = this._sessionId,
                Token     = this.Token
            };

            var pldata = new GatewayEvent()
            {
                Code         = OPCode.Resume,
                EventPayload = Content
            };

            var payload = JsonConvert.SerializeObject(Content);

            WebSocketClient.Send(payload);
            return(Task.CompletedTask);
        }
Exemple #10
0
        private Task SendIdentifyAsync()
        {
            var Content = new IdentifyPayload()
            {
                Token           = $"{this.TokenType} {this.Token}",
                Properties      = new Properties(),
                Compress        = false,
                LargeThreashold = this.LargeThreashold,
                Shard           = new[] { 0, 1 }
            };

            var pldata = new GatewayEvent()
            {
                Code         = OPCode.Identify,
                EventPayload = Content
            };

            var payload = JsonConvert.SerializeObject(pldata);

            WebSocketClient.Send(payload);
            return(Task.CompletedTask);
        }
Exemple #11
0
        private void FireEventOnDelegate <TEventData>(GatewayEvent gatewayEvent, EventHandler <GatewayEventArgs <TEventData> > eventHandler)
        {
            var eventArgs = new GatewayEventArgs <TEventData>(gatewayEvent.GetData <TEventData>());

            eventHandler?.Invoke(this, eventArgs);
        }
Exemple #12
0
 private void OnGuildDeleted(GatewayEvent gatewayEvent)
 {
     FireEventOnDelegate(gatewayEvent, GuildDeleted);
 }
Exemple #13
0
        private bool IsChannelAGuildChannel(GatewayEvent gatewayEvent)
        {
            var dataAsJObject = gatewayEvent.Data as JObject;

            return(dataAsJObject["guild_id"] != null);
        }
Exemple #14
0
 private void OnChannelUpdated(GatewayEvent gatewayEvent)
 {
     FireEventOnDelegate(gatewayEvent, GuildChannelUpdated);
 }
Exemple #15
0
 private void OnMessageDeleted(GatewayEvent gatewayEvent)
 {
     FireEventOnDelegate(gatewayEvent, MessageDeleted);
 }
Exemple #16
0
 private void OnResumeReceived(GatewayEvent gatewayEvent)
 {
     FireEventOnDelegate(gatewayEvent, Resumed);
 }
Exemple #17
0
 private void OnHelloReceived(GatewayEvent gatewayEvent)
 {
     IdentifySelfToGateway();
     BeginHeartbeatAsync(gatewayEvent.GetData <Hello>().HeartbeatInterval);
 }
Exemple #18
0
 /// <inheritdoc />
 /// <summary>
 ///     Creates a new DispatchEventArgs instance
 /// </summary>
 /// <param name="shardId">Shard where the Dispatch occured.</param>
 /// <param name="data">Data of the Dispatch.</param>
 /// <param name="event">The Event of this Dispatch.</param>
 public DispatchEventArgs(int shardId, object data, GatewayEvent @event)
 {
     ShardId = shardId;
     Data    = data;
     Event   = @event;
 }