当前位置: 首页>>代码示例>>C#>>正文


C# IConnectionFactory.Connect方法代码示例

本文整理汇总了C#中IConnectionFactory.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# IConnectionFactory.Connect方法的具体用法?C# IConnectionFactory.Connect怎么用?C# IConnectionFactory.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IConnectionFactory的用法示例。


在下文中一共展示了IConnectionFactory.Connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Connect

        internal async Task Connect(IConnectionFactory connectionFactory, IIrcIdentity identity)
        {
            lock (this)
            {
                State state = _state;
                if (state == State.Connected)
                    throw new InvalidOperationException("Already connected");
                else if (state == State.Connecting)
                    throw new InvalidOperationException("Already connecting");

                _state = State.Connecting;
            }

            ClearAll();
            _logger.Debug("Initializing connection");
            _stream = await connectionFactory.Connect(_hostname, _port, _useSsl);

            if (_stream == null)
                throw new IrcException(Resources.ConnectionIsNull);
            _logger.Debug("Connected to server");

            var encoding = new UTF8Encoding(false);
            _reader = new StreamReader(_stream, encoding);
            _writer = new StreamWriter(_stream, encoding);
            _writer.NewLine = CRLF;

            StartOutboundThread();
            if (_password != null)
                await SendCommand("PASS", _password);

            await SendCommand("NICK", identity.Nick);
            await SendCommand("USER", identity.Login, "8", "*", VERSION.AsMultiParameter());
            _nick = identity.Nick;
            _login = identity.Login;

            //OnRawMessageIn(await _reader.ReadLineAsync());
            //await Task.Delay(TimeSpan.FromSeconds(10));

            // Read stuff back from the server to see if we connected.
            string line;
            int tries = 0;
            Command cmd = default(Command);

            while ((line = await _reader.ReadLineAsync()) != null)
            {
                OnRawMessageIn(line);
                cmd = ParseCmd(line);
                switch (cmd._command)
                {
                    //Check for both a successful connection. Inital connection (001-4), user stats (251-5), or MOTD (375-6)
                    case "001":
                    case "002":
                    case "003":
                    case "004":
                    case "005":
                    case "251":
                    case "252":
                    case "253":
                    case "254":
                    case "255":
                    case "375":
                    case "376":
                        goto end;

                    case "433":
                        var tmpNick = identity.Nick + (++tries);
                        await SendCommand("NICK", tmpNick);
                        _nick = tmpNick;
                        break;

                    case "439":
                        //EXAMPLE: PircBotX: Target change too fast. Please wait 104 seconds
                        // No action required.
                        break;

                    default:
                        if (cmd._command.StartsWith("5") || cmd._command.EndsWith("4"))
                        {
                            _stream.Dispose();
                            throw new IrcException(String.Format(Resources.CouldNotConnect, cmd));
                        }
                        break;
                }
            }

            _stream.Dispose();
            throw new IrcException(String.Format(Resources.CouldNotConnect, cmd));

        end:
            _logger.Debug("Logged in");

            lock (this)
                _state = State.Connected;

            if (cmd._parameters != null)
                await HandleCmd(cmd);

            StartInboundThread();

        }
开发者ID:Alxandr,项目名称:dotRant,代码行数:100,代码来源:IrcConnection.cs


注:本文中的IConnectionFactory.Connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。