1

下面我有三个用于 GroupChat 的表,首先我正在创建组,它的所有数据将被插入到 GroupTable 中,然后在 groupCreation 成功时我调用insertConnectionId函数,该函数将调用MapGroupConnectionId在生成 SignalR 连接 ID 并使用 groupId 和 groupCreaterId 插入 GroupTable 的后端上,它工作正常,我有由 Friends ID 组成的 Group Friends Table,现在当任何朋友向特定 Group 发送消息时,我需要将 Message 发送给所有人谁通过 SignalR 在该特定组中,我想,我将组表中的 groupConnectionID 分配给与该组相关的所有成员,然后我可以向 GroupFriends 接收的特定组发送消息,我的集线器可以工作仅适用于 Hub.Client.ALL 但不适用于其他功能,如果我做错了,请指导我,我是 signalR 的新手

 //Group Table

 groupID   groupName groupImage  groupCreaterId groupConnectionId groupsignalrIsConnected
   1         dude       someurl        421       somestringID       TRUE
   2         god        someurl        444       somestringID       TRUE
   3         heaven     someurl        543       Null               FALSE
   4         hell       someurl        678       Null               FALSE


 //Group Friends Table

groupFriendsTabID   groupId groupFriendsId
      111              2    444
      112              3    678
      113              2    421
      114              4    444
      115              1    543
      116              4    421
      117              1    678
      118              2    543
      119              3    444


  //Group Message Table

  groupMesTabId groupId groupsenderId   groupMessage
         22             1   543             hello
         23             3   678             hi


 //My HUB
[HubName("groupChatHub")]
public class GroupChatHub : Hub
{
    GroupRepository group= new GroupRepository ();


    public void **MapGroupConnectionId**(long groupID,int groupCreatorId)
      {
       if (groupID != 0 && groupCreatorId!=0)
            {   
             CBG.MapGroupConnectionId(groupID, Context.ConnectionId);
              Clients.Client(Context.ConnectionId).configureUserSettings();}
             }

    public override Task OnConnected()
    {
        var connectionId = Context.ConnectionId;
        return base.OnConnected();
    }
    public override Task OnDisconnected(bool stopCalled)
    {
        var connectionId = Context.ConnectionId;
        return base.OnDisconnected(stopCalled);
    }
    public override Task OnReconnected()
    {
        return base.OnReconnected();
    }
}

  //My WEBAPI//
   public class GroupController : ApiControllerWithHub<GroupchatHub>
    {
        public IhttpActionResult InsertNewMessage(messageModel model)
        {
              //Here i am  inserting new message to Database using some    function it works fine//

            after success of inserting message i need to send that message  to groupmembers using signalr 

     //here i am fetching the connectionID through groupID from group table
      var connectionID=repository.getConnection(model.groupID)

           var messager = "11";
   Hub.Clients.All.getGroupChat(messager); // this works 

   Hub.Clients.clients(connectionID).getGroupChat(messager);this not working it except IList<string>

  Hub.Clients.Groups(groupName,ConnectionID).getGroupChat(messager); this is not  working
         }
   return ok(Model);
    }



 //MyClientEnd//

  var Grouphub

//this insertConnectionId is called once the group is created on the success  of group creation and SignalRconnectionstring is inserted in GroupTable 

function insertConnectionId(groupId,groupCreatorID)
 $.connection.hub.start().done(function () {
    console.log('Now connected, connection ID=' + $.connection.hub.id);
    console.log("Connected, transport = " +      $.connection.hub.transport.name);
    Grouphub.server.mapGroupConnectionId(groupID, groupCreatorID);
 });
 $.connection.hub.error(function (error) {
    console.log('SignalR error: ' + error)
 });

 });

 Grouphub = $.connection.groupChatHub
 Grouphub.client.getGroupChat = function (data) {
  alert("in");

 }
4

1 回答 1

1

我做了一个类似的项目,并在不同的领域使用它,

看看这个例子它会帮助你:)

https://github.com/DenizGokce/SignalR

于 2017-01-06T12:12:30.443 回答