예제 #1
0
        public async Task ShouldCreateRoomChannel()
        {
            var session = await _client.AuthenticateCustomAsync($"{Guid.NewGuid()}");

            await _socket.ConnectAsync(session);

            var channel = await _socket.JoinChatAsync("myroom", ChannelType.Room);

            Assert.NotNull(channel);
            Assert.NotNull(channel.Id);
            Assert.AreEqual(channel.Self.UserId, session.UserId);
            Assert.AreEqual(channel.Self.Username, session.Username);
        }
        private async void Start()
        {
            var deviceId = SystemInfo.deviceUniqueIdentifier;
            var session  = await _client.AuthenticateDeviceAsync(deviceId);

            Debug.LogFormat("Session user id: '{0}'", session.UserId);

            var roomUsers = new List <IUserPresence>(10);

            _socket                = _client.NewSocket();
            _socket.Connected     += () => Debug.Log("Socket connected.");
            _socket.ReceivedError += Debug.LogError;
            _socket.Closed        += () =>
            {
                Debug.Log("Socket closed.");
                roomUsers.Clear();
            };
            _socket.ReceivedChannelPresence += presenceEvent =>
            {
                foreach (var presence in presenceEvent.Leaves)
                {
                    roomUsers.Remove(presence);
                }

                roomUsers.AddRange(presenceEvent.Joins);
                Debug.LogFormat("Room users: [{0}]", string.Join(",\n  ", roomUsers));
            };
            _socket.ReceivedChannelMessage += message => Debug.LogFormat("Received message: '{0}'", message);
            await _socket.ConnectAsync(session);

            // Join chat channel.
            var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

            roomUsers.AddRange(channel.Presences);

            // Simulate a disconnect.
            await Task.Delay(TimeSpan.FromSeconds(3));

            await _socket.CloseAsync();

            await Task.Delay(TimeSpan.FromSeconds(3));

            // Reconnect and rejoin chat channel(s).
            await _socket.ConnectAsync(session);

            var channel2 = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

            roomUsers.AddRange(channel2.Presences);
            Debug.Log("Rejoined chat!");
        }
예제 #3
0
        public async void TopicJoin(string roomNameOrUserId, ChannelType channelType)
        {
            var channel = await _socket.JoinChatAsync(roomNameOrUserId, channelType, true, false);

            Debug.LogFormat($"You Can now send messages to channel id : {channel.Id}");
            StateManager.Instance.Topics.Add(roomNameOrUserId, channel.Id);
            StateManager.Instance.ChatMessages.Add(channel.Id, new Dictionary <string, IApiChannelMessage>());
        }
예제 #4
0
    // +++ class methods ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    public async void JoinRoom(string roomName)
    {
        this.OnStatusChanged(this, "OnJoinRoom called");
        _channel = await _socket.JoinChatAsync(roomName, ChannelType.Room);

        this.OnStatusChanged(this, string.Format("{0} joined", roomName));
        _connectedUsers.AddRange(_channel.Presences);
    }
        private async void Start()
        {
            var deviceId = SystemInfo.deviceUniqueIdentifier;
            var session  = await _client.AuthenticateDeviceAsync(deviceId);

            Debug.LogFormat("Session user id: '{0}'", session.UserId);

            _socket                = _client.NewSocket();
            _socket.Connected     += () => Debug.Log("Socket connected.");
            _socket.Closed        += () => Debug.Log("Socket closed.");
            _socket.ReceivedError += Debug.LogError;

            var roomUsers = new List <IUserPresence>(10);

            _socket.ReceivedChannelPresence += presenceEvent =>
            {
                foreach (var presence in presenceEvent.Leaves)
                {
                    roomUsers.Remove(presence);
                }

                roomUsers.AddRange(presenceEvent.Joins);
                Debug.LogFormat("Room users: [{0}]", string.Join(",\n  ", roomUsers));
            };

            var messageList = new List <IApiChannelMessage>(100);

            _socket.ReceivedChannelMessage += message =>
            {
                Debug.LogFormat("Received message: '{0}'", message);
                AddListSorted(messageList, message);
                Debug.LogFormat("Message list: {0}", string.Join(",\n  ", messageList));
            };
            await _socket.ConnectAsync(session);

            Debug.Log("After socket connected.");

            // Join chat channel.
            var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

            roomUsers.AddRange(channel.Presences);
            Debug.LogFormat("Joined chat channel: {0}", channel);

            // Send many chat messages.
            var content = new Dictionary <string, string> {
                { "hello", "world" }
            }.ToJson();

            _ = _socket.WriteChatMessageAsync(channel, content);
            _ = _socket.WriteChatMessageAsync(channel, content);
            _ = _socket.WriteChatMessageAsync(channel, content);
            _ = _socket.WriteChatMessageAsync(channel, content);
            _ = _socket.WriteChatMessageAsync(channel, content);
            _ = _socket.WriteChatMessageAsync(channel, content);
        }
        // -------------------------------------------

        /*
         * JoinMainChat
         */
        public async Task JoinMainChat()
        {
            var roomname    = "YourNetworkTools";
            var persistence = true;
            var hidden      = false;

            m_channel = await Socket.JoinChatAsync(roomname, ChannelType.Room, persistence, hidden);

            Debug.LogFormat("Now connected to channel id: '{0}'", m_channel.Id);
            m_connectedToMainChat = true;
        }
예제 #7
0
    async void Start()
    {
        var deviceid = SystemInfo.deviceUniqueIdentifier;
        // NOTE should cache a user session.
        var session = await _client.AuthenticateDeviceAsync(deviceid);

        Debug.LogFormat("Session '{0}'", session);

        _socket = _client.CreateWebSocket();

        var connectedUsers = new List <IUserPresence>(0);

        _socket.OnChannelPresence += (sender, presenceChange) =>
        {
            connectedUsers.AddRange(presenceChange.Joins);
            foreach (var leave in presenceChange.Leaves)
            {
                connectedUsers.RemoveAll(item => item.SessionId.Equals(leave.SessionId));
            }
            ;

            // Print connected presences.
            var presences = string.Join(", ", connectedUsers);
            Debug.LogFormat("Presence List\n {0}", presences);
        };
        _socket.OnChannelMessage += (sender, message) =>
        {
            Debug.LogFormat("Received Message '{0}'", message);
        };
        _socket.OnConnect    += (sender, evt) => Debug.Log("Socket connected.");
        _socket.OnDisconnect += (sender, evt) => Debug.Log("Socket disconnected.");

        await _socket.ConnectAsync(session);

        // Join chat channel.
        var channel = await _socket.JoinChatAsync(RoomName, ChannelType.Room);

        connectedUsers.AddRange(channel.Presences);

        // Send chat message.
        var content = new Dictionary <string, string> {
            { "hello", "world" }
        }.ToJson();
        await _socket.WriteChatMessageAsync(channel, content);
    }
예제 #8
0
        public async void Login()
        {
            int port = DEFAULT_GATEWAY_PORT;

            if (!Int32.TryParse(_portInputField.text, out port))
            {
                _failedNotification.SetActive(true);
                return;
            }

            _client = new Client(SERVER_KEY, _serverInputField.text, port, false);

            // TODO there is actually a server-managed server field that currently holds a random string, we should update that:
            _username = _usernameInputField.text;
            try {
                _session = await _client.AuthenticateEmailAsync(
                    _username + FAKE_EMAIL_SUFFIX,
                    _passwordInputField.text);
            } catch {
                _failedNotification.SetActive(true);
                return;
            }

            _loginModal.SetActive(false);

            // TODO move everything below this line to a chat-management module?:
            _chatSocket = _client.CreateWebSocket();
            await _chatSocket.ConnectAsync(_session);

            _chatChannel = await _chatSocket.JoinChatAsync(
                CHAT_ROOM, ChannelType.Room, CHAT_ROOM_PERSISTENT, CHAT_ROOM_HIDDEN);

            _chatMessagesArea.text += $"Welcome {_username}, you have connected with session {_session} to {_chatChannel.Id}.\n";

            // Handle any incoming messages:
            _chatSocket.OnChannelMessage += (_, message) => {
                if (_chatChannel.Id == message.ChannelId)
                {
                    // TODO This is naive, we can't strongly type network input so directly..
                    _chatMessagesArea.text += message.Content.FromJson <ChatMessage>().Flatten();
                }
            };
        }
    // Start is called before the first frame update
    async void Start()
    {
        // Access the gameSceneController
        gameSceneController = FindObjectOfType <GameSceneController>();

        // Create the client
        var client = new Client("http", gameHost, gamePort, gameKey);

        const string email    = "*****@*****.**";
        const string password = "******";
        var          session  = await client.AuthenticateEmailAsync(email, password);

        Debug.Log(session);

        socket            = client.NewSocket();
        socket.Connected += () => Debug.Log("Socket connected.");
        socket.Closed    += () => Debug.Log("Socket closed");

        socket.ReceivedChannelMessage += async matched =>
        {
            Debug.LogFormat("Match: {0}", matched);
        };

        await socket.ConnectAsync(session);

        await socket.AddMatchmakerAsync("*", 2, 2);

        var channel = await socket.JoinChatAsync(RoomName, ChannelType.Room);

        Debug.LogFormat("Join chat channel: {0}", channel);

        var content = new Dictionary <string, string> {
            { "hello", "world" }
        }.ToJson();

        _ = socket.WriteChatMessageAsync(channel, content);
    }