コード例 #1
0
        private async Task <bool> ReconnectWithoutAuth()
        {
            if (m_state == ChatState.Disconnected)
            {
                return(false);
            }
            UpdateStatus(ChatState.Connecting);

            // Disconnect the current socket.
            await m_ws.Disconnect();

            m_ws = null;

            Logger.Info($"Reconnecting to channel {m_channelId} WITHOUT AUTH.");

            // Connect with auth
            if (!(await ConnectInternal(false)))
            {
                // If we fail, fire disconnected.
                await Disconnect();

                return(false);
            }

            UpdateStatus(ChatState.Connected);
            return(true);
        }
コード例 #2
0
        private async Task <bool> ConnectInternal(bool withCreds)
        {
            // Get the details.
            ChatServerDetails details = await GetChatServerDetails(withCreds);

            if (details == null)
            {
                return(false);
            }

            if (details.endpoints == null || details.endpoints.Count == 0)
            {
                Logger.Error($"No chat endpoints returned for channel: {m_channelId}");
                return(false);
            }

            Random rand     = new Random();
            string endpoint = details.endpoints[rand.Next(0, details.endpoints.Count)];

            m_ws = new SimpleWebySocket(this, endpoint);
            m_ws.MinTimeBetweenSends = c_minTimeBetweenSends;

            Logger.Info($"Connecting to channel {m_channelId} on server {endpoint}...");
            if (!await m_ws.Connect())
            {
                Logger.Error($"Failed to connect to chat server for channel: {m_channelId}");
                return(false);
            }

            // Authorize!
            WebSocketMessage msg = new WebSocketMessage()
            {
                type      = "method",
                method    = "auth",
                arguments = new List <string>()
                {
                    $"{m_channelId}"
                }
            };

            if (withCreds)
            {
                msg.arguments.Add(m_userId + String.Empty);
                msg.arguments.Add(details.authkey);
            }

            if (!await SendMessage(msg, true))
            {
                Logger.Error($"Failed to send auth message to channel: {m_channelId}");
                return(false);
            }

            // Note the message id
            m_authCallMessageId = msg.id;

            return(true);
        }
コード例 #3
0
        public async Task Disconnect()
        {
            UpdateStatus(ChatState.Disconnected, false);
            SimpleWebySocket ws = m_ws;

            if (ws != null)
            {
                await ws.Disconnect();
            }
            m_callback = null;
        }
コード例 #4
0
        private async Task <bool> SendMessage(WebSocketMessage msg, bool overrideState = false)
        {
            m_lastSendTime = DateTime.Now;

            if (!overrideState)
            {
                // If we aren't connected with auth, do so now.
                if (m_state == ChatState.Connected)
                {
                    bool updated = false;
                    lock (this)
                    {
                        if (m_state == ChatState.Disconnected)
                        {
                            return(false);
                        }
                        if (m_state == ChatState.Connected)
                        {
                            updated = true;
                        }
                        UpdateStatus(ChatState.ConnectingWithAuth);
                    }

                    // If we updated the value, connect now.
                    if (updated)
                    {
                        await ReconnectWithAuth();
                    }
                }

                // Wait for the socket to connect.
                while (m_state == ChatState.ConnectingWithAuth)
                {
                    await Task.Delay(100);
                }

                // Make sure we connected.
                if (m_state == ChatState.Disconnected)
                {
                    return(false);
                }
            }

            SimpleWebySocket ws = m_ws;

            if (ws != null)
            {
                msg.id = m_rand.Next();
                return(await ws.Send(JsonConvert.SerializeObject(msg)));
            }
            return(false);
        }