SignalR或加入群组 [英] SignalR and Joining Groups

查看:397
本文介绍了SignalR或加入群组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看SignalR的一个项目,我的工作,而且似乎正是我想要的。但是一位,我还是有点困惑所是团体和加盟。

I am looking at SignalR for a project I am working on, and it seems to be EXACTLY what I want. However one bit that I am still a bit baffled by is the groups and joining.

我会尽量描述实现的情况下第一。因此,现有系统中的用户将要举行一个关于某个主题的会议,将创建一个带一个给定的名称/标识符,他们会邀请其他人进入它,然后它会基本上像一个私人聊天室。

I will try to describe the context of the implementation first. So a user within an existing system will want to hold a meeting about a given topic and will then create a room with a given name/identifier, they will then invite others into it and then it will basically be like a private chat room.

所以,我的步骤假定将作为东道主,创造一个房间,并加入它,然后发出邀请这需要用户点击它会以某种方式告诉服务器加入到哪个房间。

So the steps I assume would be as the host, create a room and join it, and then send out invites which would require users to click which would somehow tell the server which room to join into.

现在我从有一个加入,并且您可以挂接到把人成团断开方法的文档看,但它似乎加入并没有与它比查询字符串其他相关联的情况下,让我有点困惑,什么触发一个加入,因为我希望它会越过某个对象提供上下文到哪个房间把他们的,因为你可以有上百个包房客户端上的手动触发方式。

Now I see from the documentation that there is a Join and Disconnect method that you can hook into to put someone into a group, however it seems that the Join has no context associated with it other than the query string, so I am a bit confused as to what triggers a Join, as I would expect it would be a manually triggered method on the client passing over some object giving context as to what room to put them in, as you could have hundreds of private rooms.

那么,你如何给join方法的一些背景,并且断开的,让他们知道什么房您要求加入,就好像它是不是手工触发你怎么能设置查询字符串,如果是手动触发为什么你能不能送过来的自定义对象。即

So how do you give the Join method some context, and the disconnect one, so they know what room you are requesting to join, as if it is not manually triggered how can you set the query string, and if it is manually triggered why can you not send over a custom object. i.e

public Task Join()
{
    var groupName = Context.QueryString["some-room-identifier"];
    return Groups.Add(Context.ConnectionId, groupName);
}

VS

public Task Join(string groupName)
{
    return Groups.Add(Context.ConnectionId, groupName);
}

所以,我失去了一些东西或者是有一些方法给上下文一个加入的用户把他们在正确的地方第一次?

So am I missing something or is there some way to give context to a joining user to put them in the right place first time?

推荐答案

我有pretty的很多相同的经历,你和排序工作过/通过它争夺我的路,所以我会分享我已经做了。

I've had pretty much the same experience as you and have sort of worked/battled my way through it, so I'll share what I've done.

您需要的地方捕获的状态。退房SignalR集线器维基 https://github.com/SignalR/SignalR/wiki/Hubs往返从客户端到服务器下。我发现有用,因为它会告诉你如何设置状态,在服务器集线器或客户端,并在另一端进行检索。

You'll need to capture a state somewhere. Check out the SignalR Hubs wiki https://github.com/SignalR/SignalR/wiki/Hubs under Roundtripping from Client to Server. I found that useful as it tells you how to set state on the server hub or client and retrieve it at the other end.

我来到了同样的结论,你,那加入手动某些事件触发。 <击>我使用的一项民意调查,看是否有人已请求与用户聊天。实际上,你可以从服务器推送(见的 SignalR加入集团从控制器),这意味着处理添加一个新的聊天消息的控制器动作可以调用中心的加盟方法:

I came to same conclusion as you, that the Join is manually triggered by some event. I'm using a poll to see if anyone has requested a chat with the user. You can actually push from the server (see SignalR Join Group From Controller), meaning that the controller action that handles adding a new chat message can call the hub's Join method:

// join chatroom
public Task Join(dynamic mygroup)
{
var groupName = groupy.GetGroupName(parent.userId, (int)mygroup.userIdTo);
return Groups.Add(Context.ConnectionId, groupName);
}

有关我的团名,我使用单生成和使用词典&LT跟踪名称;字符串,INT []&GT; (即 INT [] 是用户id)。

For my group names, I use a Singleton to generate and keep track of names using a Dictionary<string, int[]> (the int[] is for userIds).

有关客户端状态的东西,我最终使这一管理组名称自定义JavaScript对象,像这样...

For the client state stuff, I ended up making a custom JavaScript object that managed the group name, like this...

Chat.Chatgroup = function (groupId, userIdTo) {
this.GroupId = groupId; // our group's title / id
this.UserIdTo = userIdTo; // who the other user is
}

然后我连接后调用signalR方法

Then I call the signalR method after connecting

onechat.getOurGroup(mygroup) // invoke method on server
.done(function (response) {
mygroup = response;
 /* + whatever else you need to do*/ });

现在,你可以在服务器上设置的状态和保持客户端上的那种状态:

Now you can set a state on the server and hold that state on the client:

public dynamic GetOurGroup(dynamic mygroup)
{
var ourId = groupy.GetGroupName(parent.userId, (int)mygroup.UserIdTo);
mygroup.GroupId = ourId;                    
return mygroup;
}

希望它至少隐约有帮助!

Hope it's at least vaguely helpful!

这篇关于SignalR或加入群组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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