SignalR-发送消息OnConnected [英] SignalR - Send message OnConnected

查看:110
本文介绍了SignalR-发送消息OnConnected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天一直在使用SignalR进行实验,这真的很整洁.基本上,我想实现以下目标:

I've been experimenting with SignalR today and It's really neat. Basically what I wanted to achieve is the following:

设备连接后,应立即向第一个发送消息.如果连接的设备多于1个,我想发送两条消息.一个以外的最后一个客户端.一条消息仅发送给最后一个连接的客户端.

As soon as a device connects it should send a message to the first one. If there are more devices than 1 connected I would like to send two messages. One to all except the last connected client. And one message to only the last connected client.

当我将其放在自定义API控制器中并基本上调用该操作时,我一直在使用的代码非常完美,但这不是我想要的.

The code I've been using works perfect when I place it in a custom API controller and basically call the action, but that's not what I want.

我希望设备在 OnConnected 中进行连接时立即发送消息,而无需任何用户交互,但是当我将代码放在 OnConnected 替代中时,它将停止工作.它不再发送给特定的客户端(首先连接和最后连接).

I would like to send the messages as soon as a device connects within OnConnected without any user interaction, but when I place my code inside the OnConnected override it stops working. It doesn't send to the specific clients anymore (first connected and last connected).

我希望有人能够帮助我解决这个问题,因为我已经敲了好几个小时了.

I hope someone is able to help me out with this, because I've been banging my head for a few hours now.

    public override System.Threading.Tasks.Task OnConnected()
    {
        UserHandler.ConnectedIds.Add(Context.ConnectionId, UserHandler.ConnectedIds.Count + 1);

        int amountOfConnections = UserHandler.ConnectedIds.Count;
        var lastConnection = UserHandler.ConnectedIds.OrderBy(x => x.Value).LastOrDefault();
        var allExceptLast = UserHandler.ConnectedIds.Take(amountOfConnections - 1).Select(x => x.Key).ToList();

        if (amountOfConnections == 1)
        {
            Clients.Client(UserHandler.ConnectedIds.First().Key).hello("Send to only(also first) one");
        }
        else
        {
            Clients.Clients(allExceptLast).hello("Send to everyone except last");
            Clients.Client(lastConnection.Key).hello("Send to only the last one");
        }

        return base.OnConnected();
    }

推荐答案

感谢所有帮助(赞成你们).实际发现了问题..这是在我的客户内部.我首先订阅了"hello"功能,然后启动了HubConnection.我更改此顺序后,一切正常.

Thanks for all the help (upvoted you guys). Actually found the problem.. it was inside my client. I first subscribed to the 'hello' function and after that I started the HubConnection. As soon as I changed this order everything worked fine.

它与以下客户端代码一起使用:

It worked with the following client code:

    private async Task ConnectToSignalR()
    {
        var hubConnection = new HubConnection("url");
        hubConnection.Headers["x-zumo-application"] = "clientapikey";

        IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");

        proxy.On<string>("hello", async (msg) =>
        {
            Console.WriteLine(msg);
        });

        await hubConnection.Start();
    }

这篇关于SignalR-发送消息OnConnected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆