示例#1
0
        /// <summary>
        /// 获取客户端ID
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        private static MessageAuthKey GetAppAuthKey(IWebSocketConnection socket)
        {
            MessageAuthKey      messageAuthKey = null;
            NameValueCollection queryParams    = UriUtil.GetQueryString(socket.ConnectionInfo.Path);
            string appKey = queryParams["appKey"];
            string appId  = queryParams["appId"];
            // string authKeyStr =socket.ConnectionInfo.Path.Substring(socket.ConnectionInfo.Path.LastIndexOf("?") + 1);
            string callbackMsg = "";

            if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(appId))
            {
                callbackMsg = $"appKey或appId 信息不存在,连接被拒绝";
                LOG.Error(callbackMsg);
                socket.Send(callbackMsg);
                socket.Close();
                return(null);
            }
            try
            {
                // 获取agent类型,支持多端同时连接
                EnumUserAgentType enumUserAgentType = NetAssist.GetUserAgentType(socket.ConnectionInfo.Headers["User-Agent"]);
                string            agentType         = EnumUtil.GetEnumName <EnumUserAgentType>((int)enumUserAgentType);

                if (appId_Agent_AuthKeyMap.ContainsKey(appId))
                {
                    if (appId_Agent_AuthKeyMap[appId].ContainsKey(agentType))
                    {
                        return(appId_Agent_AuthKeyMap[appId][agentType]);
                    }
                }
                messageAuthKey = new MessageAuthKey
                {
                    AppId     = appId,
                    AppKey    = appKey,
                    AgentType = agentType
                };

                // @TODO 验证appkey的有限性和appId是否唯一
                return(messageAuthKey);
            }
            catch (Exception ex)
            {
                LOG.Error(ex, ex.Message);
                callbackMsg = $"连接信息缺失,连接被拒绝。详情:{ex.Message}";
                socket.Send(callbackMsg);
                socket.Close();
                return(null);
            }
        }