用signalR创建代理

本文关键字:代理 创建 signalR | 更新日期: 2023-09-27 18:10:37

我正在研究一个signalR网站和一个连接到signalR服务器的signalR客户端控制台应用程序,但我在连接时遇到了一些问题。

我注意到在一些教程和其他主题中,他们正在使用"CreateProxy"方法,但我没有这个方法,也无法在对象浏览器中找到它。

我的问题是,如果有替代方法,如果没有,我该如何告诉signalR我想连接什么集线器?

代码:

  Connection = new HubConnection(GetUrl());
        while (Connection.State != ConnectionState.Connected)
        {
            var connectionTask = Connection.Start().ContinueWith(task =>
            {
                if (Connection.State == ConnectionState.Connected)
                {
                    Console.WriteLine("Succesvol verbonden!");
                    // Subscribe to receive messages
                    Connection.Received += Connection_Received;
                }
                else
                {
                    Console.WriteLine("Het is niet gelukt om te verbinden!");
                }
            });
            // Wait till outcome
            connectionTask.Wait();
            // Check if we are succesfully connected
            if (Connection.State != ConnectionState.Connected)
            {
                Connection = new Connection(GetUrl());
            }
            else
            {
                // Send a message
                Connection.Send("Hallooo!").ContinueWith(sendTask =>
                {
                    if (connectionTask.IsFaulted)
                    {
                        Console.WriteLine("Het is niet gelukt om een bericht te versturen!");
                    }
                });
            }
        }

用signalR创建代理

我猜你的连接属性不是HubConnection类型,而是Connection或IConnection类型?

如果是这种情况,您应该首先将Connection转换回HubConnection类型,以便能够使用CreateHubProxy():

        Connection = new HubConnection(GetUrl());
        var hubProxy = ((HubConnection) Connection).CreateHubProxy("YourHubName");