Exemplo n.º 1
0
        private void RecvCallback(IAsyncResult result)
        {
            int byte_len = stream.EndRead(result);

            if (byte_len <= 0)
            {
                Logger.LogError("Received length < 0!");

                ClientErrored?.Invoke("Invalid length!");

                return;
            }

            var data = new byte[byte_len];

            Array.Copy(receiveBuf, data, byte_len);

            string msg = Encoding.UTF8.GetString(data);

            Logger.Log($"Received message: {LocalUser}: {msg}");

            ChatMessageReceived?.Invoke(LocalUser, msg);

            stream.BeginRead(receiveBuf, 0, 4096, RecvCallback, null);
        }
Exemplo n.º 2
0
 private void Reconnect(int delay, int index)
 {
     ClientErrored?.Invoke("Reconnecting........");
     Dispose();
     Thread.Sleep(delay);
     ConnectAndAuthenticate(_config, index);
 }
Exemplo n.º 3
0
        public void StartReceive(object inputindex)
        {
            ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;

            while (true)
            {
                Thread.Sleep(1000);

                try
                {
                    string message = Post(URL, data);

                    RawPayload?.Invoke(message);
                }
                catch (Exception e)
                {
                    ClientErrored?.Invoke("Error occured trying to read stream: " + e);
                }
            }
        }
Exemplo n.º 4
0
        public void StartReceive()
        {
            while (true)
            {
                try
                {
                    if (!_client.Connected)
                    {
                        Dispose();
                        ConnectAndAuthenticate(_config);
                    }

                    string message = _output.ReadLine();
                    RawPayload?.Invoke(message);
                }
                catch (Exception e)
                {
                    ClientErrored?.Invoke("Error occured trying to read stream: " + e);
                    Reconnect(5000);
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }
Exemplo n.º 5
0
        /// <summary>
        /// process the json result which response from BiliBili
        /// </summary>
        /// <param name="json"></param>
        private void ProcessJson(string json)
        {
            if (json == null)
            {
                return;
            }

            var rt = JsonConvert.DeserializeObject <Root>(json);

            try
            {
                List <RoomItem> room = rt.data.room;

                foreach (Message m in room.Select(r => new Message(r.nickname, r.timeline, r.text)).Where(m => !_log.Contains(m)))
                {
                    _log.Add(m);

                    // Don't execute messages > 30 seconds.
                    if (TimeOut(m))
                    {
                        continue;
                    }

                    ChatMessageReceived?.Invoke(m.user, m.text);
                }
            }
            catch
            {
                ClientErrored?.Invoke($"{rt == null} Please Check your Roomid[{data["roomid"]}] \r\n {json}");
            }

            if (_log.Count > 1000)
            {
                _log.RemoveRange(0, 800);
            }
        }
Exemplo n.º 6
0
 public void Dispose()
 {
     ClientErrored?.Invoke("BiliBili Client Closed");
 }