Exemple #1
0
        public void OpenOrCloseWs(bool isResetFailCount = false)
        {
            try {
                if (isResetFailCount)
                {
                    ResetFailCount();
                }
                switch (_appType)
                {
                case NTMinerAppType.MinerClient:
                    _isOuterUserEnabled = NTMinerRegistry.GetIsOuterUserEnabled();
                    break;

                case NTMinerAppType.MinerStudio:
                    _isOuterUserEnabled = true;
                    break;

                default:
                    _isOuterUserEnabled = false;
                    break;
                }
                string outerUserId;
                switch (_appType)
                {
                case NTMinerAppType.MinerClient:
                    outerUserId = NTMinerRegistry.GetOuterUserId();
                    break;

                case NTMinerAppType.MinerStudio:
                    outerUserId = RpcRoot.RpcUser.LoginName;
                    break;

                default:
                    outerUserId = string.Empty;
                    break;
                }
                if (!_isOuterUserEnabled)
                {
                    ResetFailCount();
                    if (_ws != null && _ws.ReadyState == WebSocketState.Open)
                    {
                        _ws.CloseAsync(CloseStatusCode.Normal, "外网群控已禁用");
                    }
                    return;
                }
                if (string.IsNullOrEmpty(outerUserId))
                {
                    ResetFailCount();
                    _ws?.CloseAsync(CloseStatusCode.Normal, "未填写用户");
                    return;
                }
                bool isUserIdChanged = _outerUserId != outerUserId;
                if (isUserIdChanged)
                {
                    _outerUserId = outerUserId;
                }
                // 1,进程启动后第一次连接时
                if (_ws == null)
                {
                    NewWebSocket(webSocket => _ws = webSocket);
                }
                else if (isUserIdChanged)
                {
                    ResetFailCount();
                    if (_ws.ReadyState == WebSocketState.Open)
                    {
                        // 因为旧的用户名密码成连接过且用户名或密码变更了,那么关闭连接即可,无需立即再次连接因为一定连接不成功因为用户名密码不再正确。
                        string why = string.Empty;
                        if (isUserIdChanged)
                        {
                            why = ",因为用户变更";
                        }
                        _ws.CloseAsync(CloseStatusCode.Normal, $"关闭连接{why}");
                    }
                    else
                    {
                        // 因为旧用户名密码没有成功连接过,说明旧用户名密码不正确,而现在用户名密码变更了,有可能变更正确了所以尝试连接一次。
                        ConnectAsync(_ws);
                    }
                }
                else if (_ws.ReadyState != WebSocketState.Open)
                {
                    ConnectAsync(_ws);
                }
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
        }