Javascript 仅允许登录用户聊天?流星聊天应用
我正在使用meteor开发一个聊天应用程序,我希望它是这样,你必须先登录,然后才能添加评论。我这样做的原因是因为我希望我的网站上的每个人都有一个昵称,而不是像匿名385HG83F之类的名字。本教程使用帐户用户界面,以下是聊天应用程序的代码: javascript:Javascript 仅允许登录用户聊天?流星聊天应用,javascript,html,meteor,chat,accounts,Javascript,Html,Meteor,Chat,Accounts,我正在使用meteor开发一个聊天应用程序,我希望它是这样,你必须先登录,然后才能添加评论。我这样做的原因是因为我希望我的网站上的每个人都有一个昵称,而不是像匿名385HG83F之类的名字。本教程使用帐户用户界面,以下是聊天应用程序的代码: javascript: // get the value for handlerbar helper user Template.chatMessage.helpers({ "user": function() { if(this.userId
// get the value for handlerbar helper user
Template.chatMessage.helpers({
"user": function() {
if(this.userId == 'me') {
return this.userId;
} else if(this.userId) {
getUsername(this.userId);
return Session.get('user-' + this.userId);
} else {
return 'anonymous-' + this.subscriptionId;
}
}
});
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
"click #send": function() {
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
},
"keypress #chat-message": function(e) {
if (e.which == 13) {
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
console.log("you pressed enter");
e.preventDefault();
//repeat function from #send click event here
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
}
}
});
chatStream.on('chat', function(message) {
chatCollection.insert({
userId: this.userId,
subscriptionId: this.subscriptionId,
message: message
});
});
我不知道该怎么做。有人知道如何让用户在能够聊天之前必须注册/登录吗?我可以用两种方法来做到这一点。第一个是不向客人显示聊天内容 在HTML文件中,您可以使用:
{{#if currentUser}}
<!-- The code to show the chat -->
{{else}}
<!-- The code if is not logged in -->
You must login to use the chat
{{/if}}
这两种方法可能都不是用户友好的方法,但它们显示了您如何处理用户是否登录的问题
这是一个很好的教程,它将逐步向您展示如何进行聊天感谢第二种方法非常有效,正是我想要的。也谢谢你花时间回答这个问题,好像没有其他人会这么做,哈哈。
// when Send Chat clicked at the message to the collection
Template.chatBox.events({
"click #send": function() {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
},
"keypress #chat-message": function(e) {
if (Meteor.user() == null) {
alert("You must login to post");
return;
}
if (e.which == 13) {
$('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
console.log("you pressed enter");
e.preventDefault();
//repeat function from #send click event here
var message = $('#chat-message').val();
chatCollection.insert({
userId: 'me',
message: message
});
$('#chat-message').val('');
//add the message to the stream
chatStream.emit('chat', message);
}
}
});