Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client StartWithTimeout #144

Open
FengLiWeng opened this issue Jun 25, 2024 · 2 comments
Open

client StartWithTimeout #144

FengLiWeng opened this issue Jun 25, 2024 · 2 comments

Comments

@FengLiWeng
Copy link

After the client sets the connection timeout to 2 seconds, if the connection fails, it still takes a long time to return

        // 创建WatsonWsClient实例
        var client = new WatsonWsClient("192.168.1.11",19405,false);

        // 设置连接超时为30秒
        int timeout = 2;

        DateTime cc = DateTime.Now;

        // 创建CancellationTokenSource用于取消连接
        var cancellationTokenSource = new CancellationTokenSource();
        CancellationToken token = cancellationTokenSource.Token;

        try
        {
            Console.WriteLine("Attempting to connect...");
            // 尝试连接到服务器
            bool isConnected = client.StartWithTimeout(timeout, token);


            Console.WriteLine(cc.Subtract(DateTime.Now).TotalSeconds);


            // 检查连接是否成功
            if (isConnected)
            {
                Console.WriteLine("Connected to the server successfully.");
            }
            else
            {
                Console.WriteLine("Failed to connect to the server.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
        finally
        {
            // 停止客户端
            //client.Stop();
            Console.WriteLine("Client stopped.");
        }
@jchristn
Copy link
Owner

Can you reproduce this with the Test.ClientTimeout project? It seems to be working fine for me.

@FengLiWeng
Copy link
Author

WebSocketDemo.zip
Out.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants