2017-05-03 19 views
0

說在頁面上有50個職位。每篇文章都包含一些評論。 當我點擊帖子時,一個彈出窗口會顯示註釋。信號發送評論給連接的客戶端,但在特定的職位

發生的情況是,如果兩個用戶連接到服務器。一個是看到帖子1,另一個看到帖子34,每個人發送評論,交換意見。

當且僅當在彈出窗口中打開特定帖子ID才能發表評論時,我該如何告訴信號員將帖子發送給對方?我是新來的。任何指針都可以。

這是我工作的代碼

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> 

這是我chathub

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

按照這個link用戶分組管理多方式,我們如何有計劃通過爲每個崗位差異組/渠道去做。當有人在一篇文章中(評論彈出窗口),那麼他將獲得更新/推送/相關的帖子 –

+0

將創建一個連接,並在用戶打開彈出窗口時將該用戶添加到組中?當他關閉彈出窗口時,他被刪除了? – maztt

+0

是的。這正是事情。還記得在signalr中創建一個單獨的組是沒有頭腦的。 –

回答

1

一個簡單的解決方案,你可以這樣做。

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

這樣每個在線用戶收到新評論,但只有誰在觀看該職位將能夠看到它。

或者你可以在服務器端

相關問題