假设页面上有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 :(得分: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);
}
}
这样每个在线用户都会收到新评论,但只有谁能够查看该帖子才能看到。
或者,您可以按照此link了解服务器端用户分组的更多管理方式