【问题标题】:Group Chat Signal R with Web API带有 Web API 的群聊 Signal R
【发布时间】:2017-01-06 11:24:57
【问题描述】:

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

 }

【问题讨论】:

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


    【解决方案1】:

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

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

    https://github.com/DenizGokce/SignalR

    【讨论】:

    • @thanks for your project 它帮助了我,我只是想知道,我使用的用户绑定信号器连接字符串并将其存储在数据库中的方法如上面的代码,这是实现的好方法它或者如果有更好的方法让我知道
    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多