简体   繁体   English

流星聊天-多个房间

[英]Meteor chat - multiple rooms

I am trying to create an application with multiple chat rooms. 我正在尝试创建具有多个聊天室的应用程序。 At the moment one of the rooms works by sending and receiving messages. 目前,其中一个房间通过发送和接收消息来工作。 However when I route and change template and try send a message in another room the message does not send but the link at the top changes? 但是,当我路由和更改模板并尝试在另一个房间中发送消息时,该消息未发送但顶部的链接发生了变化? ie localhost:3000/java?text=hi - It says the message is going to cache. localhost:3000 / java?text = hi-它表示消息将要缓存。

How can I get messages to send in the other rooms, store in the collection and display? 如何获取要发送到其他房间的消息,存储在收藏夹中并进行显示? Here is my code: Client.js 这是我的代码:Client.js

// This code only runs on the client
    Meteor.subscribe("messages", {
    onReady: function () {
      scrollToBottom();
      autoScrollingIsActive = true;
    }
  });  


  /* helper code */
    Template.body.helpers({
    recentMessages: function () {
      return Messages.find({}, {sort: {createdAt: 1}});
    },
    /* unread message helper */
      thereAreUnreadMessages: function () {
      return thereAreUnreadMessages.get();
    }
  });

  /*chat window scrolling*/
  Template.message.onRendered(function () {
    if (autoScrollingIsActive) {
      scrollToBottom(250);
    } else {
      if (Meteor.user() && this.data.username !== Meteor.user().username) {
        thereAreUnreadMessages.set(true);
      }
    }
  });
Template.body.events({
"submit .new-message": function (event) {
  var text = event.target.text.value;

  Meteor.call("sendMessage", text);
    scrollToBottom(250);

  event.target.text.value = "";
  event.preventDefault();
},


/* scroll event */
  "scroll .message-window": function () {
  var howClose = 80;  // # pixels leeway to be considered "at Bottom"
  var messageWindow = $(".message-window");
  var scrollHeight = messageWindow.prop("scrollHeight");
  var scrollBottom = messageWindow.prop("scrollTop") + messageWindow.height();
  var atBottom = scrollBottom > (scrollHeight - howClose);
  autoScrollingIsActive = atBottom ? true : false;
  if (atBottom) {        // <--new
    thereAreUnreadMessages.set(false);
  }
},

 "click .more-messages": function () {
  scrollToBottom(500);
  thereAreUnreadMessages.set(false);
}
});

Template.footer.usercount = function () {
  return Meteor.users.find().count();
};

Here is my server.js 这是我的server.js

// This code only runs on the server
  Meteor.publish("messages", function () {
    return Messages.find();
  });

  // This code only runs on the server
  Meteor.publish("messages_java", function () {
    return MessagesJava.find();
  });

And here is the template code for the "Java room": 这是“ Java室”的模板代码:

    <template name="java">
<!--<h6>This is the Java template</h6>-->
<!--<p>*Put chatroom here*</p>-->
<div class="container">
    <header>
      <h1>ITHub Java Room</h1>
      {{> loginButtons}} 
    </header>

<!-- chat messages here -->
<!--  -->
    <ul class="message-window">
      {{#each recentMessagesJava}}
        {{> message}}
      {{/each}}
    </ul>
    <!-- more-messages button -->
    {{#momentum plugin="fade"}}
      {{#if thereAreUnreadMessages}}
        <button class="more-messages">New Messages &#x25BE;</button>
      {{/if}}
    {{/momentum}}

    <footer>
      {{#if currentUser}}
        <form class="new-message">
          <input type="text" name="text" placeholder="Add a message" autocomplete="off" />
        </form>
      {{/if}}
      <br />
    </footer>

    <hr />
<li class="message-java">
    <div class="username">{{username}}</div>
    <div class="message-text">{{messageText}}</div>
</li>

  </div>

Your template isn't called body , but java , so to attach events you need to add them via Template.java.helpers , like so: 您的模板不是body ,而是java ,因此要附加事件,您需要通过Template.java.helpers来添加它们,如下所示:

Template.java.events({
"submit .new-message": function (event) {
  var text = event.target.text.value;

  Meteor.call("sendMessage", text);
    scrollToBottom(250);

  event.target.text.value = "";
  event.preventDefault();
},


/* scroll event */
  "scroll .message-window": function () {
  var howClose = 80;  // # pixels leeway to be considered "at Bottom"
  var messageWindow = $(".message-window");
  var scrollHeight = messageWindow.prop("scrollHeight");
  var scrollBottom = messageWindow.prop("scrollTop") + messageWindow.height();
  var atBottom = scrollBottom > (scrollHeight - howClose);
  autoScrollingIsActive = atBottom ? true : false;
  if (atBottom) {        // <--new
    thereAreUnreadMessages.set(false);
  }
},

 "click .more-messages": function () {
  scrollToBottom(500);
  thereAreUnreadMessages.set(false);
}
});

And same thing is with helpers . helpers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM