【发布时间】:2013-02-06 02:29:52
【问题描述】:
我的基本聊天室正常工作(单页),但我想在浏览到我的 URL 时生成一个独特的聊天室。例如。用户浏览到 chatroom.com 并被重定向到 chatrooom.com/room1,然后她/他可以与朋友分享该 URL 以与之聊天。我该怎么做?
【问题讨论】:
我的基本聊天室正常工作(单页),但我想在浏览到我的 URL 时生成一个独特的聊天室。例如。用户浏览到 chatroom.com 并被重定向到 chatrooom.com/room1,然后她/他可以与朋友分享该 URL 以与之聊天。我该怎么做?
【问题讨论】:
你需要一个路由器,你可以使用骨干网或我个人最喜欢的流星路由器(通过meteorite安装):
mrt install router
在您的客户端 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
};
对于您的路由器(也在客户端 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'
});
因此,如果您要加载 /rooms/lobby,它将仅加载 room 值为 lobby 的消息。
更多关于流星路由器的文档:https://github.com/tmeasday/meteor-router
【讨论】: