コード例 #1
0
ファイル: Play.cs プロジェクト: wujun4code/Play-SDK-dotNET
        /// <summary>
        /// Join or create room.
        /// </summary>
        /// <param name="roomName">Room name.</param>
        /// <param name="roomConfig">Room config.</param>
        public static void JoinOrCreateRoom(string roomName, PlayRoom.RoomConfig roomConfig)
        {
            if (string.IsNullOrEmpty(roomName))
            {
                LogError("roomName can NOT be null or empty.");
                return;
            }
            var joinOrCreateRoomCommand = new PlayCommand()
            {
                Body = new Dictionary <string, object>()
                {
                    { "cmd", "conv" },
                    { "op", "add" },
                    { "cid", roomName },
                    { "createOnNotFound", true },
                },
            };

            if (roomConfig.ExpectedUsers != null)
            {
                joinOrCreateRoomCommand.Body.Add("expectMembers", roomConfig.ExpectedUsers.ToArray());
            }

            RunSocketCommand(joinOrCreateRoomCommand, PlayEventCode.OnJoinOrCreatingRoom, (req, res) =>
            {
                if (res.Body.ContainsKey("cid"))
                {
                    DoJoinRoom(req, res);
                }
                else
                {
                    CreateRoom(roomConfig, roomName);
                }
            });
        }
コード例 #2
0
ファイル: Play.cs プロジェクト: wujun4code/Play-SDK-dotNET
        /// <summary>
        /// create a room with config and name.
        /// </summary>
        /// <param name="roomConfig">config of Room</param>
        /// <param name="roomName">name of Room</param>
        public static void CreateRoom(PlayRoom.RoomConfig roomConfig, string roomName = null)
        {
            var createRoomCommand = new PlayCommand()
            {
                Body = new Dictionary <string, object>()
                {
                    { "cmd", "conv" },
                    { "op", "start" },
                },
            };

            if (string.IsNullOrEmpty(roomName))
            {
                roomName = PlayRoom.RandomRoomName(24);
            }

            RunSocketCommand(createRoomCommand, done: (request, response) =>
            {
                // get game server address from game router
                var roomRemoteSecureAddress = response.Body["secureAddr"] as string;
                var roomRemoteAddress       = response.Body["addr"] as string;

                // open websocket connection with game server
                DoConnectToGameSever(roomRemoteSecureAddress, () =>
                {
                    Play.InvokeEvent(PlayEventCode.OnCreatingRoom);
                    // create room at game server
                    peer.SessionCreateRoom(roomName, roomConfig, (room) =>
                    {
                        Play.Room = room;
                        Play.InvokeEvent(PlayEventCode.OnCreatedRoom);
                        Play.InvokeEvent(PlayEventCode.OnJoinedRoom);
                    });
                });
            });
        }
コード例 #3
0
        internal void SessionCreateRoom(string roomName, PlayRoom.RoomConfig roomConfig, Action <PlayRoom> roomCreated = null)
        {
            IDictionary <string, object> body = new Dictionary <string, object>();

            if (roomConfig.CustomRoomProperties != null)
            {
                body.Add("attr", roomConfig.CustomRoomProperties.ToDictionary());
            }
            if (roomConfig.MaxPlayerCount > 0 && roomConfig.MaxPlayerCount != PlayRoom.DefaultMaxPlayerCount)
            {
                body.Add("maxMembers", roomConfig.MaxPlayerCount);
            }
            if (roomConfig.EmptyTimeToLive > 0 && roomConfig.EmptyTimeToLive != PlayRoom.DefaultMaxEmptyTimeToLive)
            {
                body.Add("emptyRoomTtl", roomConfig.EmptyTimeToLive);
            }
            if (roomConfig.PlayerTimeToKeep > 0 && roomConfig.PlayerTimeToKeep != PlayRoom.DefaultMaxKeepPlayerTime)
            {
                body.Add("playerTtl", roomConfig.PlayerTimeToKeep);
            }
            if (roomConfig.ExpectedUsers != null)
            {
                body.Add("expectMembers", roomConfig.ExpectedUsers);
            }
            if (!roomConfig.IsVisible)
            {
                body.Add("visible", roomConfig.IsVisible);
            }
            if (!roomConfig.IsOpen)
            {
                body.Add("open", roomConfig.IsOpen);
            }
            if (roomConfig.LobbyMatchKeys != null)
            {
                body.Add("lobbyAttrKeys", roomConfig.LobbyMatchKeys);
            }
            body.Add("cmd", "conv");
            body.Add("op", "start");
            body.Add("cid", roomName);
            var createCommand = new PlayCommand()
            {
                Body = body
            };

            Play.RunSocketCommand(createCommand, done: (req, response) =>
            {
                if (response.IsSuccessful)
                {
                    var room = new PlayRoom(roomConfig, roomName);
                    Play.DoSetRoomProperties(room, response);
                    if (roomCreated != null)
                    {
                        roomCreated(room);
                    }
                }
                else
                {
                    Play.InvokeEvent(PlayEventCode.OnCreateRoomFailed, response.ErrorCode, response.ErrorReason);
                }
            });
        }