简体   繁体   English

Meteor.js中的房间/频道和userId

[英]Rooms/Channels and userId in Meteor.js

I'm building a multiplayer, turn-based game using meteor.js. 我正在使用meteor.js构建多人,回合制游戏。 The application will handle multiple games, so I'd like to separate my users into rooms. 该应用程序将处理多个游戏,因此我想将用户划分为多个房间。 I've done it before using socket.io channels, but I'm struggling to understand how it should be done in Meteor. 在使用socket.io通道之前,我已经完成了此操作,但我一直在努力了解应如何在Meteor中完成它。

The flow I'd like to achieve is: 我想要实现的流程是:

  1. User visits http://localhost:3000/join/userId 用户访问http:// localhost:3000 / join / userId

  2. I make a server-side call to an external API using "sessionId" as parameter, getting user's userId, his assigned roomId and an array of allowed userId's for this room 我使用“ sessionId”作为参数对服务器端进行外部调用,获取用户的userId,为其分配的roomId和该房间允许的userId数组

  3. I'd like to create a room with roomId for the user or join him to an existing one. 我想为用户创建一个带有roomId的房间,或将他加入现有房间。 I know I should create a 'Rooms' collection, but I don't know how to tie users to my rooms and publish messages only to those present in the given room. 我知道我应该创建一个“房间”集合,但是我不知道如何将用户绑定到我的房间并仅向给定房间中的用户发布消息。

I'd like to avoid using 'accounts' package, because I don't need authorisation on my side - it'll be handled by step #2 mentioned above - but if the easiest and cleanest way of doing it involves adding this package, I can change my mind. 我想避免使用“帐户”程序包,因为我不需要我的授权-上面提到的第2步将处理该程序-但是,如果最简便的方法是添加此程序包,我可以改变主意。

Your Rooms collection could look like: 您的“ Rooms集合可能如下所示:

{
    _id: "<auto-generated>",
    roomId: "roomId",
    users: [ "user1", "user2", "user3", ... ],
    messages: [
        { message: "", userId: "" },
        { message: "", userId: "" },
        { message: "", userId: "" },
        ...
    ]
}

The server-side API call returns 服务器端API调用返回

userId and roomId among other information. userIdroomId等信息。

So you can do a 所以你可以做一个

Rooms.update({ roomId: roomId }, { $push: { users: userId } }, { upsert: true });

This would push the user into the exiting room or create a new room and add the user. 这会将用户推送到退出房间或创建新房间并添加用户。

Your publish function could look like: 您的发布功能可能类似于:

Meteor.publish("room", function(roomId) {
    // Since you are not using accounts package, you will have to get the userId using the sessionId that you've specified or some other way.
    // Let us assume your function getUserId does just that.

    userId: getUserId( sessionId );
    return Rooms.find({ roomId: roomId, users: userId });

    // Only the room's users will get the data now.

});

Hope this helps. 希望这可以帮助。

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

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