2017-05-03 4 views
0

Sagen Sie, es gibt 50 Post auf der Seite. Jeder Beitrag enthält einige Kommentare. Wenn ich auf einen Beitrag klicke, erscheint ein Popup mit Kommentaren.signaler sende kommentar an verbundenen client, aber innerhalb bestimmter post

Was passiert, wenn zwei Benutzer mit dem Server verbunden sind. Einer sieht Post 1 und der andere Seeing Post 34, jeder sendet einen Kommentar, die Kommentare wurden ausgetauscht.

Wie kann ich dem Signalgeber mitteilen, dass er den Post senden soll, wenn und nur wenn diese spezielle Post-ID für Kommentare im Popup-Fenster geöffnet wird? Ich bin neu in diesem Bereich. jeder Zeiger wird es tun.

Das ist mein Arbeits Code

var viewModel = function() { 
    var self = this; 
    self.hub = $.connection.chathub; 
    self.commenttext = ko.observable(); 
    self.comments = ko.observableArray(); 
    self.commentdate = ko.observable(); 


    self.init = function() { 
    self.hub.server.getPosts().fail(function(err) { 
     console.log("connection started"); 
    }); 
    } 

    self.hub.client.loadPosts = function(data) { 
    ko.mapping.fromJS(data, {}, self.comments); 
    } 

    self.hub.client.newCommentss = function(comment) { 
    self.comments.push(comment); 
    } 

    self.addcomments = function() { 
    var t = { 
     "comment": self.commenttext(), 
     "cardid": 20 
    }; 
    $.ajax({ 
     url: "@Url.Action(" 
     AddComments ", " 
     Home ")", 
     type: "post", 
     contentType: "application/json", 
     data: JSON.stringify(t), 
     success: function(data) { 
     self.hub.server.addCommentss(t).done(function(comment) { 


     }).fail(function(err) { 

     }); 
     } 
    }); 
    } 


}; 

var vm = new viewModel(); 
ko.applyBindings(vm); 

$.connection.hub.start().done(function() { 
    vm.init(); 
}); 
<div id="div1"> 
    <textarea data-bind="value: commenttext"></textarea><br /> 
    <a href="#" data-bind="click: addcomments" style="margin-bottom:4em">Send</a><br /><br /><br /> 
    <ul data-bind="foreach: comments,visible: comments().length > 0"> 
    <li> 
     <span data-bind="text:commentdate"></span> 
     <strong><span data-bind="text: comment"> </span></strong><br /><br /> 
    </li> 
    </ul> 
</div> 

Das ist mein chathub ist

public class chathub : Hub 
{ 

    private readonly ApplicationDbContext _context; 

    public chathub(ApplicationDbContext context) 
    {   
     _context = context; 
    } 

    public void GetComments(int id) 
    { 
     List<CommentsViewModel> commentss= new List<CommentsViewModel>(); 
     var comments = _context.Comments.Where(m => m.cardid == id); 
     foreach (var item in comments) 
     { 
      CommentsViewModel b = new CommentsViewModel(); 
      b.commentid = item.commentid; 
      b.comment = item.comment; 
      b.commentdate = item.commentdate; 
      b.cardid = item.cardid; 
      commentss.Add(b); 
     } 
     Clients.All.loadComments(commentss); 
    } 
    public bool addCommentss(Comment newComment) 
    { 
     Comment commentobj = new Comment(); 
     commentobj.comment = newComment.comment; 
     commentobj.commentdate = System.DateTime.Now; 
     commentobj.cardid = newComment.cardid; 
     _context.Add(commentobj); 
     _context.SaveChanges(); 
     Clients.All.newCommentss(commentobj); 
     return true; 
    } 

    public bool removeCommentss(int id) 
    { 
     Comment commentobject = _context.Comments.FirstOrDefault(m => m.commentid == id); 
     if (commentobject != null) 
     { 
      _context.Comments.Remove(commentobject); 
      _context.SaveChanges(); 
     } 
     // return Json(true); 
     Clients.All.deleteCommentss(commentobject); 
     return true; 
    } 
} 
+0

folgen Wie wir geplant haben, es zu tun, indem sie für jeden Post diff Gruppen/Kanäle zu schaffen. Wenn jemand auf einem Post ist (Kommentare Popup), dann wird er Updates/Pushs/​​Post bezogen nur auf diesen Beitrag erhalten –

+0

eine Verbindung wird erstellt und die Verwendung wird der Gruppe hinzugefügt, wenn Benutzer ein Popup öffnet? und wenn er das Popup schließt, wurde er entfernt? – maztt

+0

ja. Das ist genau das Richtige. Auch wenn ich mich erinnere, gibt es keinen Overhead, um eine separate Gruppe im Signal zu erstellen. –

Antwort

1

als einfache Lösung, Sie können wie folgt tun.

// save opened/clicked post ID into a local variable (clear when closed) 
var currentPostID = 22; 

// in your signalr receive event 
self.hub.client.newCommentss = function(comment) { 
    // assuming cardid is postID 
    if(currentPostID == comment.cardid) { 
    self.comments.push(comment); 
    } 
} 

auf diese Weise jeder Online-Nutzer Kommentar erhält aber nur, wer in der Lage, dass die Post sehen, es zu sehen.

Oder Sie können diese link für mehrere verwaltete Art und Weise der Benutzer Gruppierung auf Server-Seite

Verwandte Themen