コード例 #1
0
        private void HandleMsg(Socket client, Message <object> msg)
        {
            _logger.LogDebug($"收到客户端指令:{msg.MessageType}");
            switch (msg.MessageType)
            {
            case MessageType.C_LogIn:
                HandleLogin(client, _loginHandler.GetConfig(msg.Content));

                // 递归调用
                ReceiveClient(client, null);
                break;

            case MessageType.Heart:
                client.Send(new Message <string>()
                {
                    MessageType = MessageType.Heart, Content = null
                });

                // 递归调用
                ReceiveClient(client, null);
                break;

            case MessageType.C_SwapMsg:
                var        msgId = msg.Content as string;
                NewRequest request;

                if (!string.IsNullOrEmpty(msgId) && newRequest.TryGetValue(msgId, out request))
                {
                    // Join
                    Task.Run(() =>
                    {
                        (new SocketSwap(request.CustomerClient, client))
                        .BeforeSwap(() => { if (request.Buffer != null)
                                            {
                                                client.Send(request.Buffer);
                                            }
                                    })
                        .StartSwap();
                    });
                }
                else
                {
                    // 未找到,关闭连接
                    _logger.LogError($"未找到请求:{msgId}");
                    client.Send(new Message <string> {
                        MessageType = MessageType.Error, Content = $"未找到请求:{msgId}"
                    });
                }

                break;

            case MessageType.S_NewCustomer:
            default:
                throw new Exception($"参数异常, 不支持消息类型 {msg.MessageType}");
            }
        }