2017-01-06 34 views
0

下面我有三个表,这是用于GroupChat,首先我创建该组的所有数据将被插入GroupTable,然后成功的groupCreation我打电话insertConnectionId函数将在后端调用MapGroupConnectionId,该后端生成SignalR连接ID并使用groupId和groupCreaterId插入GroupTable,其工作正常,并且我拥有包含Friends ID的Group Friends Table,现在当任何朋友向特定Group发送消息时,我需要通过SignalR将消息发送给所有在特定组中的人,我想,我将把组表中的groupConnectionID分配给与该组相关的所有成员,然后我可以发送消息给该特定组,由Gr接收oupFriends,我中心只为Hub.Client.ALL而不适用于其他功能,请指导我,如果我在错误的方式做这件事,我是新来signalR群聊信号R与Web API

//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"); 

} 

回答

0

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

退房这个例子中它会帮助你:)

https://github.com/DenizGokce/SignalR

+0

@thanks为您的项目它帮助我,我只是想知道,这是我所使用的方法供用户绑定信号连接字符串并将其存储在数据库中上面的代码,它实现它的好方法,或者如果有更好的方式让我知道 – Nikil