Exemplo n.º 1
0
        /// <summary>
        /// 元事件处理和分发
        /// </summary>
        /// <param name="messageJson">消息</param>
        /// <param name="connection">连接GUID</param>
        private async void MetaAdapter(JObject messageJson, Guid connection)
        {
            switch (GetMetaEventType(messageJson))
            {
            //心跳包
            case "heartbeat":
                ApiHeartBeatEventArgs heartBeat = messageJson.ToObject <ApiHeartBeatEventArgs>();
                ConsoleLog.Debug("Sora", $"Get heartbeat from [{connection}]");
                //刷新心跳包记录
                if (heartBeat != null)
                {
                    ConnectionManager.HeartBeatUpdate(connection);
                }
                break;

            //生命周期
            case "lifecycle":
                ApiLifeCycleEventArgs lifeCycle = messageJson.ToObject <ApiLifeCycleEventArgs>();
                if (lifeCycle != null)
                {
                    ConsoleLog.Debug("Sore", $"Lifecycle event[{lifeCycle.SubType}] from [{connection}]");
                }

                (int retCode, string clientType, string clientVer) = await ApiInterface.GetClientInfo(connection);

                if (retCode != 0)     //检查返回值
                {
                    ConsoleLog.Error("Sora", $"获取客户端版本失败(retcode={retCode})");
                    break;
                }

                ConsoleLog.Info("Sora", $"已连接到{clientType}客户端,版本:{clientVer}");
                if (OnClientConnect == null)
                {
                    break;
                }
                //执行回调函数
                await OnClientConnect(typeof(EventInterface),
                                      new ConnectEventArgs(connection, "lifecycle",
                                                           lifeCycle?.SelfID ?? -1, clientType, clientVer,
                                                           lifeCycle?.Time ?? 0));

                break;

            default:
                ConsoleLog.Warning("Sora", $"接收到未知事件[{GetMetaEventType(messageJson)}]");
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 事件分发
        /// </summary>
        /// <param name="messageJson">消息json对象</param>
        /// <param name="connection">客户端链接接口</param>
        internal void Adapter(JObject messageJson, Guid connection)
        {
            switch (GetBaseEventType(messageJson))
            {
            //元事件类型
            case "meta_event":
                MetaAdapter(messageJson, connection);
                break;

            case "message":
                MessageAdapter(messageJson, connection);
                break;

            case "request":
                RequestAdapter(messageJson, connection);
                break;

            case "notice":
                NoticeAdapter(messageJson, connection);
                break;

            default:
                //尝试从响应中获取标识符
                if (messageJson.TryGetValue("echo", out JToken echoJson) &&
                    Guid.TryParse(echoJson.ToString(), out Guid echo))
                {
                    //取出返回值中的数据
                    ApiInterface.GetResponse(echo, messageJson);
                }
                else
                {
                    ConsoleLog.Debug("Sora", $"Unknown message :\r{messageJson}");
                }

                break;
            }
        }