Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

signalr - Group Chat Signal R with Web API

Below i have three table which is used for GroupChat,Firstly I am creating the group its all data wil be inserted into GroupTable,then on success of groupCreation i am calling insertConnectionId function which will call the MapGroupConnectionId on backend which generate the SignalR connection ID and insert into GroupTable using groupId and groupCreaterId, its working fine and i have Group Friends Table which consist of Friends ID,Now when any friend sent message to particular Group, i need to send the Message to all who are in that Particular Group through SignalR, i thought, i ll assign the groupConnectionID in group table to all the Members who are related to that Group then i can send message to that particular group which will be received by GroupFriends,My hub which works only for Hub.Client.ALL but not for other function, please guide me if i am doing it in wrong way,I am new to 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");

 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have done a similar project like and used it in different areas,

Check out this example it will help you :)

https://github.com/DenizGokce/SignalR


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...