Exemplo n.º 1
0
        public void GivenAConnectedUserWithOneConnection_WhenTheUserConnectWithANewConnection_ThenNothingHappens()
        {
            var chat = new ChatRoom();
            chat.Connect("username", DummyDateTime, DummyConnectionId());

            ChatEvent chatEvent = chat.Connect("username", Now, "newConnection");

            chatEvent.Should().Be(ChatEvent.NothingHappens(Now));
        }
Exemplo n.º 2
0
        public void GivenAConnectedUserWithManyConnections_WhenTheUserDisconnectFromOneConnection_ThenNothingHappens()
        {
            var chat = new ChatRoom();
            chat.Connect("username", DummyDateTime, "connection1");
            chat.Connect("username", DummyDateTime, "connection2");

            ChatEvent chatEvent = chat.Disconnect("connection1", Now);

            chatEvent.Should().Be(ChatEvent.NothingHappens(Now));
        }
Exemplo n.º 3
0
        public void GivenAConnectedUserWithManyConnections_ThenTheJoinedAtDatetimeShouldBeTheTimestampOfTheFirstConnection()
        {
            var chat = new ChatRoom();
            chat.Connect("username", Now.AddMinutes(-2), DummyConnectionId());
            chat.Connect("username", Now.AddMinutes(-1), DummyConnectionId());

            chat.Connect("username", Now, DummyConnectionId());

            chat.ConnectedUserByUsername("username").Value
                .JoinedAt.Should().Be(Now.AddMinutes(-2));
        }
Exemplo n.º 4
0
        public void GivenAConnectedUserWithManyConnections_WhenTheUserDisconnectFromAllConnections_ThenTheUserShouldLeaveChat()
        {
            var chat = new ChatRoom();
            chat.Connect("username", DummyDateTime, "connection1");
            chat.Connect("username", DummyDateTime, "connection2");

            chat.Disconnect("connection1", DummyDateTime);
            ChatEvent chatEvent = chat.Disconnect("connection2", Now);

            chatEvent.Should().Be(ChatEvent.UserLeavesChat("username", Now));
        }
Exemplo n.º 5
0
        public void CanGetConnectedUsers()
        {
            var chat = new ChatRoom();
            chat.Connect("username1", Now.AddMinutes(-1), "connection1");
            chat.Connect("username2", Now, "connection2");

            chat.ConnectedUsers.ShouldAllBeEquivalentTo(
                new[]
                {
                    new ConnectedUser("username1", new UserConnection("connection1", Now.AddMinutes(-1))),
                    new ConnectedUser("username2", new UserConnection("connection2", Now))
                });
        }
    public void Connect()
    {
        var host = IPField.text;
        var name = NameField.text;

        if (string.IsNullOrEmpty(name))
        {
            return;
        }

        m_ChatRoom.Connect(host, m_Port, name);
    }
Exemplo n.º 7
0
        public void GivenAConnectedUser_WhenTheUserDisconnect_ThenTheUserShouldLeaveChat()
        {
            var chat = new ChatRoom();
            chat.Connect("username", DummyDateTime, "connection");

            ChatEvent chatEvent = chat.Disconnect("connection", Now);

            chatEvent.Should().Be(
                ChatEvent.UserLeavesChat("username", Now));
        }
Exemplo n.º 8
0
        public void GivenUsersLimitReached_WhenAnExistingUserMakeANewConnection_ThenShouldNotThrow()
        {
            var chat = new ChatRoom(2);
            chat.Connect("username", DummyDateTime, "connection1");
            chat.Connect("other", DummyDateTime, DummyConnectionId());

            Action act = () =>
                chat.Connect("username", DummyDateTime, "connection2");

            act.ShouldNotThrow<InvalidOperationException>();
        }
Exemplo n.º 9
0
        public void GivenUsersLimitReached_WhenANewUserConnect_ThenShouldThrowException()
        {
            var chat = new ChatRoom(2);
            chat.Connect("username1", DummyDateTime, DummyConnectionId());
            chat.Connect("username2", DummyDateTime, DummyConnectionId());

            Action act = () =>
                chat.Connect("username3", DummyDateTime, DummyConnectionId());

            act.ShouldThrow<InvalidOperationException>();
        }
Exemplo n.º 10
0
        public void GivenSomeConnectedUsers_WhenANotConnectedUserDisconnect_ThenNothingHappens()
        {
            var chat = new ChatRoom();
            chat.Connect("dummyUser1", DummyDateTime, DummyConnectionId());
            chat.Connect("dummyUser2", DummyDateTime, DummyConnectionId());

            ChatEvent chatEvent = chat.Disconnect("newConnection", Now);

            chatEvent.Should().Be(
               ChatEvent.NothingHappens(Now));
        }
Exemplo n.º 11
0
        public void GivenSomeConnectedUsers_WhenANewUserConnect_ThenTheUserShouldJoinChat()
        {
            var chat = new ChatRoom();
            chat.Connect("dummyUser1", DummyDateTime, DummyConnectionId());
            chat.Connect("dummyUser2", DummyDateTime, DummyConnectionId());

            ChatEvent chatEvent = chat.Connect("username", Now, DummyConnectionId());

            chatEvent.Should().Be(
                ChatEvent.UserJoinsChat("username", Now));
        }
Exemplo n.º 12
0
        public void GivenAnEmptyChatRoom_WhenANewUserConnect_ThenTheUserShouldJoinChat()
        {
            var chat = new ChatRoom();

            ChatEvent chatEvent = chat.Connect("username", Now, DummyConnectionId());

            chatEvent.Should().Be(
                ChatEvent.UserJoinsChat("username", Now));
        }
        /// <summary>
        /// establish a connection to the chat server
        /// </summary>
        /// <returns></returns>
        private async Task Connect()
        {
            ToggleBlockConnectionCommands(false);

            var character = SimpleIoc.Default.GetInstance <CharacterViewModel>().Character;
            var settings  = settingsViewModel.Setting;

            Username = SimpleIoc.Default.GetInstance <CharacterViewModel>().SelectedCharacter.Name;

            if (string.IsNullOrWhiteSpace(Username))
            {
                Username = character.Name != "" ? character.Name : "User" + new Random().Next(1000, 9999);
            }

            UserType  = (SimpleIoc.Default.GetInstance <SettingsViewModel>().Setting.GameMasterMode) ? UserType.GameMaster : UserType.Player;
            Address   = SimpleIoc.Default.GetInstance <SettingsViewModel>().Setting.ServerAddress;
            Port      = SimpleIoc.Default.GetInstance <SettingsViewModel>().Setting.ServerPort;
            ColorCode = (SimpleIoc.Default.GetInstance <SettingsViewModel>().Setting.GameMasterMode) ? "Red" : "Black";

            var validIp = IPAddress.TryParse(Address, out IPAddress ipAddress);

            if (!validIp)
            {
                try
                {
                    ipAddress = Dns.GetHostAddresses(Address)[0];
                }
                catch
                {
                    DisplayError("Server Adresse leer oder ungültig, bitte in den Einstellungen überprüfen.");
                    return;
                }
            }

            UserModel user = new(Username, UserType, settings.UserIdentifier);

            var validPort = int.TryParse(Port, out int socketPort);

            if (!validPort)
            {
                DisplayError("Gibt bitte einen gültigen Server Port an");
                return;
            }

            if (string.IsNullOrWhiteSpace(Address))
            {
                DisplayError("Gib bitte eine gültige Server Adresse an");
                return;
            }

            if (string.IsNullOrWhiteSpace(Username))
            {
                DisplayError("Gib bitte einen Namen an");
                return;
            }

            ChatRoom.Clear();

            await Task.Run(() => ChatRoom.Connect(Address, socketPort));

            settingsViewModel.AllowEdit = false;
            ToggleBlockConnectionCommands(true);
        }
Exemplo n.º 14
0
    // Use this for initialization
    public IEnumerator Initialize(Action <string> callback)
    {
        // Note that ws:// stands for websocket protocol
        String uri = "ws://" + serverName + ":" + port;

        client          = new Client(uri, username);
        client.OnOpen  += OnOpenHandler;
        client.OnClose += (object sender, EventArgs e) => {
      #if debug_server
            Debug.Log("Network connection closed.");
      #endif
        };
        client.OnError += (object sender, Colyseus.ErrorEventArgs e) => Debug.Log(e);

        yield return(StartCoroutine(client.Connect()));

        // === Join game lobby & matchmaking (registrar) ===
        registrar = client.Join <RegistrarRoom>("registrar", new Dictionary <string, object>()
        {
            { "create", true }, // The existence and disposal of this room is to be managed by the server
            { "dispose", false }
        });

        registrar.OnReadyToConnect += (sender, e) => {
            StartCoroutine(registrar.Connect(this));
        };

        registrar.OnJoin        += registrar.OnRoomJoined; // Event raised by Room is handled in RegistrarRoom
        registrar.OnStateChange += registrar.OnStateChangeHandler;
        registrar.OnMessage     += registrar.OnServerMessage;

        // --- Chat ---
        chat = client.Join <ChatRoom>("chat", new Dictionary <string, object>()
        {
            { "create", true },
            { "dispose", true }
        });

        chat.OnReadyToConnect += (sender, e) => {
            StartCoroutine(chat.Connect(this));
        };

        chat.OnJoin        += chat.OnRoomJoined;
        chat.OnStateChange += chat.OnStateChangeHandler;

        //chat.Listen ("players/:id/:axis", this.OnPlayerMove);
        chat.Listen("messages/:number", this.chat.OnMessageAdded);
        chat.Listen(this.chat.OnChangeFallback);

        chat.OnMessage += chat.OnServerMessage;


        callback("Network connection active on port " + port);
        int i = 0;

        while (true)
        {
            client.Recv();

            yield return(0);
        }
    }