示例#1
0
        /// <summary>
        /// Starts a background listener that waits for any message from the client
        /// </summary>
        private void StartBackgroundListener()
        {
            Task.Factory.StartNew(async() =>
            {
                while (Running)
                {
                    Console.WriteLine("Receiving...");
                    dynamic msg = await MessagingUtil.ReceiveMessage(_stream);

                    if (msg == null)
                    {
                        CloseConnection();
                        return;
                    }

                    IMessage message = JsonDecoder.Decode(msg);
                    Console.WriteLine(message.Type);
                    switch (message.Type)
                    {
                    case MessageType.OK_MESSAGE:
                        HandleOkMessage((OkMessage)message);
                        break;

                    case MessageType.ERROR_MESSAGE:
                        HandleErrorMessage((ErrorMessage)message);
                        break;

                    case MessageType.LOGIN_MESSAGE:
                        HandleLoginMessage((LoginMessage)message);
                        break;

                    case MessageType.CHAT_MESSAGE:
                        HandleChatMessage((ChatMessage)message);
                        break;

                    case MessageType.PATCH_MESSAGE:
                        HandlePatchMessage((PatchMessage)message);
                        break;

                    case MessageType.OUT_OF_SYNC_MESSAGE:
                        HandleOutOfSyncMessage((OutOfSyncMessage)message);
                        break;
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }
示例#2
0
        /// <summary>
        /// The StartBackgroundListener is listening for new Messages that come in.
        /// These messages will come in from the stream the sender is always the server.
        /// The messages that will come forward from the stream are messages with certain types so the switch cases can recongnize it.
        /// </summary>
        private void StartBackgroundListener()
        {
            Console.WriteLine("Connected!");
            Task.Factory.StartNew(async() =>
            {
                while (Running)
                {
                    dynamic msg = await MessagingUtil.ReceiveMessage(_stream);

                    Console.WriteLine($"In Client BackgroundListener: {msg}");
                    IMessage message = JsonDecoder.Decode(msg);
                    Console.WriteLine($"In Client BackgroundListener: {message.Type}");
                    switch (message.Type)
                    {
                    case MessageType.OK_MESSAGE:
                        HandleOkMessage((OkMessage)message);
                        break;

                    case MessageType.OK_LOGIN_MESSAGE:
                        HandleOkLoginMessage((OkLoginMessage)message);
                        break;

                    case MessageType.ERROR_MESSAGE:
                        HandleErrorMessage((ErrorMessage)message);
                        break;

                    case MessageType.CHAT_MESSAGE:
                        HandleChatMessage((ChatMessage)message);
                        break;

                    case MessageType.PATCH_MESSAGE:
                        HandlePatchMessage((PatchMessage)message);
                        break;

                    case MessageType.PATCH_ERROR_MESSAGE:
                        HandlePatchErrorMessage((PatchErrorMessage)message);
                        break;

                    case MessageType.OUT_OF_SYNC_RESPONSE:
                        HandleOutOfSyncResponse((OutOfSyncResponse)message);
                        break;
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }