用于测试SignalR组的C#控制台应用程序

问题描述 投票:0回答:1

我有一个SignalR集线器发送组通知,我想测试SignalR组。以下是Hub服务器代码:

[HubName("MyHubServer")]
public class HubServer : Hub
{
    public override Task OnConnected()
    {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled)
    {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public string JoinGroup(string connectionId, string groupName)
    {
        Groups.Add(connectionId, groupName).Wait();
        return connectionId + " joined " + groupName;
    }

    public string LeaveGroup(string connectionId, string groupName)
    {
        Groups.Remove(connectionId, groupName).Wait();
        return connectionId + " removed " + groupName;
    }

    public string LeaveGroup(string connectionId, string groupName, string customerId = "0")
    {
        Groups.Remove(connectionId, customerId).Wait();
        return connectionId + " removed " + groupName;
    }
}

服务器代码向组发送消息(我将调用此代码在加入组后向组发送消息):

Hub.Clients.Group("Associate").BroadcastAssociateNotification(JsonConvert.SerializeObject(notifications));

现在我想测试SignalR服务器组加入组并在服务器向该组发送消息时接收消息。下面是我用来测试相同代码的代码,这个代码我在Groups.Add(connectionId,groupName)行中遇到“任务被取消”的错误.Wait();

static void Main(string[] args)
{
    MainAsync().Wait();
    Console.ReadLine();
}

static async Task MainAsync()
{
    try
    {
        var hubConnection = new HubConnection("http://localhost:12923/");
        IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
        stockTickerHubProxy.On<string>("Associate", data => Console.WriteLine("Notification received : ", data));
        hubConnection.Start().Wait();

        await stockTickerHubProxy.Invoke("JoinGroup", "123456", "Associate");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
    }
}
c# asp.net signalr signalr-hub signalr.client
1个回答
0
投票

您正在混合异步和阻塞调用(.Wait),这可能导致方法中的死锁。你应该等待,而不是阻止它。

首先将Hub更新为异步

[HubName("MyHubServer")]
public class HubServer : Hub {
    public override Task OnConnected() {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled) {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public async Task<string> JoinGroup(string connectionId, string groupName) {
        await Groups.Add(connectionId, groupName);
        return connectionId + " joined " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName) {
        await Groups.Remove(connectionId, groupName);
        return connectionId + " removed " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
        await Groups.Remove(connectionId, customerId);
        return connectionId + " removed " + groupName;
    }
}

接下来,在主控调用之外的控制台中执行相同操作。

static async Task MainAsync() {
    try {
        var hubConnection = new HubConnection("http://localhost:12923/");
        IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
        stockTickerHubProxy.On<string>("Associate", data => Console.WriteLine("Notification received : ", data));

        await hubConnection.Start();

        await stockTickerHubProxy.Invoke("JoinGroup", "123456", "Associate");
    } catch (Exception ex) {
        Console.WriteLine(ex.Message);
        Console.ReadLine();
    }
}

一旦客户端能够建立连接,一切都应该按预期运行。

测试了以下自托管示例,以显示客户端和集线器之间存在通信。

[HubName("MyHubServer")]
public class HubServer : Hub {
    public override Task OnConnected() {
        // My code OnConnected
        return base.OnConnected();
    }

    public override Task OnDisconnected(bool stopCalled) {
        // My code OnDisconnected
        return base.OnDisconnected(stopCalled);
    }

    public async Task<string> JoinGroup(string connectionId, string groupName) {
        await Groups.Add(connectionId, groupName);
        var message= connectionId + " joined " + groupName;
        Clients.Group("Associate").Notify(message);
        return message;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName) {
        await Groups.Remove(connectionId, groupName);
        return connectionId + " removed " + groupName;
    }

    public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
        await Groups.Remove(connectionId, customerId);
        return connectionId + " removed " + groupName;
    }
}

class Program {
    static void Main(string[] args) {
        string url = "http://localhost:8080";
        using (WebApp.Start(url)) {
            Console.WriteLine("Server running on {0}", url);
            MainAsync(url).Wait();
            Console.ReadLine();
        }
    }

    static async Task MainAsync(string url) {
        try {
            var hubConnection = new HubConnection(url);
            IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
            stockTickerHubProxy.On<string>("Notify",
                data => Console.WriteLine("Notification received : {0}", data));

            await hubConnection.Start();

            var connectionId = hubConnection.ConnectionId;

            var response = await stockTickerHubProxy.Invoke<string>("JoinGroup", connectionId, "Associate");
            Console.WriteLine("Response received : {0}", response);
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }
}

class Startup {
    public void Configuration(IAppBuilder app) {
        //app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.