internal static void ClientConnected(CrowdControlClient c)
        {
            Console.WriteLine("Client connected");
            Console.WriteLine("Awaiting CC Command");

            c.Receive();
        }
        private static void StartCrowdControlServer()
        {
            var client = new CrowdControlClient();

            // Setup handlers
            client.Connected        += new ConnectedHandler(CrowdControlEventManager.ClientConnected);
            client.MessageReceived  += new ClientMessageReceivedHandler(CrowdControlEventManager.ClientMessageReceived);
            client.MessageSubmitted += new ClientMessageSubmittedHandler(CrowdControlEventManager.ClientMessageSent);

            client.StartClient();
        }
        private static void StartCrowdControlServer()
        {
            // https://codereview.stackexchange.com/questions/24758/tcp-async-socket-server-client-communication
            var client = new CrowdControlClient();

            // Setup handlers

            client.Connected        += new ConnectedHandler(CrowdControlEventManager.ClientConnected);
            client.MessageReceived  += new ClientMessageReceivedHandler(CrowdControlEventManager.ClientMessageReceived);
            client.MessageSubmitted += new ClientMessageSubmittedHandler(CrowdControlEventManager.ClientMessageSent);
            client.StartClient();
            //new Thread(new ThreadStart(client.StartClient)).Start();
        }
        internal static void ClientMessageReceived(CrowdControlClient c, string raw)
        {
            try {
                Console.WriteLine("Message received: " + raw);
                // Decode the message from CC
                // { "id":1,"code":"kill","viewer":"sdk","type":1}
                var request = JsonSerializer.Deserialize <CrowdControlRequest>(raw);

                // Do the thing
                var eventName = EventNameDict[request.Code];
                var status    = 0;

                if (TimerCooldown.IsInitialised())
                {
                    try
                    {
                        EventLookup.Lookup(eventName, request.Viewer);
                    }
                    catch (Exception)
                    {
                        // On exception set failed flag
                        status = 1;
                    }
                }
                else
                {
                    // Game is not yet ready for events, retry until it is.
                    status = 3;
                }


                // Send the result to CC
                var response = new CrowdControlResponse();
                response.Id      = request.Id;
                response.Status  = status;
                response.Message = "Effect: " + request.Code + ": " + response.Status;

                var resJson = JsonSerializer.Serialize <CrowdControlResponse>(response);
                Console.WriteLine("Sending response: " + resJson);

                c.Send(resJson, false);
            } catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
 internal static void ClientMessageSent(CrowdControlClient c, bool close)
 {
     Console.WriteLine("Message Sent.");
     // Await next command
     c.Receive();
 }
Esempio n. 6
0
        internal static void ClientMessageReceived(CrowdControlClient c, string raw)
        {
            try {
                Console.WriteLine("Message received: " + raw);
                // Decode the message from CC
                // { "id":1,"code":"kill","viewer":"sdk","type":1}
                var request = JsonSerializer.Deserialize <CrowdControlRequest>(raw);

                // Do the thing
                var eventName = EventNameDict[request.Code];
                var status    = 0;

                if (request.Id == 0 && request.Type == 255)
                {
                    // Test message, ignore
                    return;
                }

                if (TimerCooldown.IsInitialised())
                {
                    try
                    {
                        // Check to see if the event can fire currently fire, or is on cooldown.
                        if (!EventLookup.IsRunningOrCooldown(eventName))
                        {
                            // Not running or on cooldown, activate it if the type is "start"
                            if (request.Type == 1)
                            {
                                EventLookup.Lookup(eventName, request.Viewer);
                            }
                        }
                        else
                        {
                            // Event in use, retry.
                            status = 3;
                        }
                    }
                    catch (Exception)
                    {
                        // On exception set failed flag
                        status = 1;
                    }
                }
                else
                {
                    // Game is not yet ready for events, retry until it is.
                    status = 3;
                }


                // Send the result to CC
                var response = new CrowdControlResponse();
                response.Id      = request.Id;
                response.Status  = status;
                response.Message = "Effect: " + request.Code + ": " + response.Status;

                var resJson = JsonSerializer.Serialize <CrowdControlResponse>(response);
                Console.WriteLine("Sending response: " + resJson);

                c.Send(resJson, false);
            } catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }