使用Web API进行群聊信号R.

时间:2017-01-06 11:24:58

标签: signalr signalr-hub signalr.client signalr-2

下面我有三个用于GroupChat的表,首先我创建了一个组,它的所有数据都将插入到GroupTable中,然后在groupCreation成功时我调用 insertConnectionId 函数,它将调用后端的 MapGroupConnectionId 生成SignalR连接ID,并使用groupId和groupCreaterId插入GroupTable,工作正常,我有Group Friends Table,其中包含Friends ID,现在当任何朋友向特定Group发送消息时,我需要通过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");

 }

1 个答案:

答案 0 :(得分:1)

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

看看这个例子它会对你有所帮助:)。

https://github.com/DenizGokce/SignalR