在meteor中如何为不同的房间创建具有唯一URL的多厅应用程序?

bne*_*eil 1 node.js meteor

我已经有了基本的聊天室(单页),但是当我浏览到我的URL时,我想生成一个独特的聊天室.例如,用户浏览chatroom.com并被重定向到chatrooom.com/room1,然后他/她可以与朋友分享该网址与之聊天.我该怎么做呢?

Aks*_*hat 6

你需要一个路由器,你可以使用骨干或我个人最喜欢的流星路由器(通过陨石安装):

mrt install router
Run Code Online (Sandbox Code Playgroud)

在你的客户端js

//In your chat query add something to localise your chat messages for your room e.g (if you're using handlebars):

Template.messages.message = function() {
    //assuming messages contains your collection of chats
    return messages.find({room:Session.get("room")}) //get the room name set in the router
};
Run Code Online (Sandbox Code Playgroud)

并为您的路由器(也在客户端js):

Meteor.Router.add({
  '/': 'home',
  '/rooms/:id': function(id) {
     Session.set("room",id); //set the room for the template
     return "messages"; //If you're template is called messages
  },
  '*': 'not_found'
Run Code Online (Sandbox Code Playgroud)

});

因此,如果您要加载/rooms/lobby它,则只加载room值为的消息lobby.

关于meteor路由器的更多文档:https://github.com/tmeasday/meteor-router