示例#1
0
 protected void OnReceivedMessage(SBPMessageEventArgs e)
 {
     if (ReceivedMessageEvent != null)
     {
         ReceivedMessageEvent.Invoke(this, e);
     }
 }
 private Task ProcessMessage(Message message, CancellationToken token)
 {
     lock (MessageRecieved)
     {
         MessageRecieved.Message = message;
         ReceivedMessageEvent.Invoke(this, MessageRecieved);
     }
     return(Task.CompletedTask);
 }
示例#3
0
        private void ReceivedMessageEventHandler(object state)
        {
            var msg = (state as NetServer)?.ReadMessage();

            if (msg == null)
            {
                return;
            }
            var args = new ReceivedMessageEventArgs()
            {
                ReceivedMessage = msg
            };

            ReceivedMessageEvent?.Invoke(this, args);
            (state as NetServer)?.Recycle(msg);
        }
示例#4
0
        private void ReceivedMessage(object client)
        {
            var msg = (client as NetClient)?.ReadMessage();

            if (msg == null)
            {
                return;
            }

            var args = new ReceivedMessageEventArgs()
            {
                ReceivedMessage = msg
            };

            ReceivedMessageEvent?.Invoke(this, args);
            (client as NetClient)?.Recycle(msg);
        }
示例#5
0
 private void Consumer_Received(object sender, BasicDeliverEventArgs e)
 {
     //var message = Encoding.UTF8.GetString(e.Body);
     ReceivedMessageEvent?.Invoke(null, e.Body);
 }
示例#6
0
        public bool Load()
        {
            if (WebsocketUrl.EndsWith("/"))
            {
                WebsocketUrl = WebsocketUrl.Substring(0, WebsocketUrl.Length - 1);
            }
            _httpClient    = new HttpApiClient(ListenUrl, AccessToken);
            _wsApiListener = new CqHttpWebSocketEvent(WebsocketUrl, AccessToken);
            _wsApiListener.StartListen();
            _wsApiListener.MessageEvent += (api, message) =>
            {
                if (message is GroupMessage groupContent)
                {
                    var groupMessage = new AcceptedMessage()
                    {
                        IsGroupMessage = true,
                        FromGroup      = groupContent.GroupId,
                        FromUser       = groupContent.Sender.UserId,
                        Content        = groupContent.Content.Text
                    };
                    ReceivedMessageEvent?.Invoke(this, groupMessage);
                }

                if (message is PrivateMessage privateContent)
                {
                    var privateMessage = new AcceptedMessage()
                    {
                        IsGroupMessage = false,
                        FromUser       = privateContent.UserId,
                        Content        = privateContent.Content.Text
                    };
                    ReceivedMessageEvent?.Invoke(this, privateMessage);
                }
            };
            _wsApiListener.GroupInviteEvent += (api, request) => true;
            _wsApiListener.GroupAddedEvent  += (api, request) =>
            {
                var message = (SendingMessage)"欢迎使用Vtuber-Bot 查看帮助请输入help";
                message.TargetGroupId = request.GroupId;
                _sendingMessageQueue.Enqueue(message);
            };
            _enabled = true;
            new Thread(async() =>
            {
                while (_enabled)
                {
                    await Task.Delay(1500);
                    if (_sendingMessageQueue.TryDequeue(out var message))
                    {
                        var sendingMessage = new Sisters.WudiLib.SendingMessage();
                        foreach (var content in message.Contents)
                        {
                            if (content.Type == MessageType.Image)
                            {
                                sendingMessage += Sisters.WudiLib.SendingMessage.ByteArrayImage(content.Data);
                            }
                            if (content.Type == MessageType.Text)
                            {
                                sendingMessage += new Sisters.WudiLib.SendingMessage(content.Content);
                            }
                        }
                        await _httpClient.SendGroupMessageAsync(message.TargetGroupId, sendingMessage)
                        .Retry(5, handle: e => LogHelper.Error("Send message error", ex: e));
                    }
                }
            })
            {
                IsBackground = true
            }.Start();
示例#7
0
 private void Mp3Client_OnReceivedMessage(object sender, ReceivedEventArgs e)
 {
     ReceivedMessageEvent?.Invoke(this, e);
 }
        private void BeginProcessMessage()
        {
            new Thread(async() =>
            {
                while (Running)
                {
                    try
                    {
                        var buffer = new byte[1024 * 1024]; //1mb
                        var result = await Websocket.ReceiveAsync(buffer, CancellationToken.None);
                        var json   = JToken.Parse(Encoding.UTF8.GetString(buffer.Take(result.Count).ToArray()));
                        if (json["id"] != null && Callbacks.ContainsKey(json["id"].ToString()))
                        {
                            Callbacks[json["id"].ToString()].Invoke(json);
                            Callbacks.Remove(json["id"].ToString());
                            continue;
                        }

                        if (json["event"] != null)
                        {
                            switch (json["event"].ToString())
                            {
                            case "message":
                                var type = json["params"]["type"].ToObject <LightMessageType>();
                                if (type == LightMessageType.Group || type == LightMessageType.Friend)
                                {
                                    var message = new AcceptedMessage()
                                    {
                                        IsGroupMessage = type == LightMessageType.Group,
                                        Content        = json["params"]["content"].ToString(),
                                        FromGroup      = type == LightMessageType.Group
                                                ? json["params"]["group"].ToObject <long>()
                                                : 0,
                                        FromUser = json["params"]["qq"].ToObject <long>()
                                    };
                                    ReceivedMessageEvent?.Invoke(this, message);
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (Running && Websocket.State != WebSocketState.Open)
                        {
                            try
                            {
                                Websocket = new ClientWebSocket();
                                await Websocket.ConnectAsync(new Uri(ListenUrl), CancellationToken.None);
                            }
                            catch
                            {
                                LogHelper.Error("Cannot reconnect websocket.");
                            }
                        }
                    }
                }
            }).Start();
            Task.Factory.StartNew(async() =>
            {
                while (Running)
                {
                    await Task.Delay(500);
                    if (SendingMessageQueue.Any())
                    {
                        var message = SendingMessageQueue.Dequeue();
                        if (message.Contents.All(v => v.Type != MessageType.Image))
                        {
                            await SendMessageAsync("sendMessage", new
                            {
                                type    = LightMessageType.Group,
                                group   = message.TargetGroupId.ToString(),
                                content = message.ToString()
                            });
                        }
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }
示例#9
0
        private void BeginProcessMessage()
        {
            new Thread(async() =>
            {
                while (Running)
                {
                    try
                    {
                        var buffer = new byte[1024 * 1024]; //1mb
                        var result = await Websocket.ReceiveAsync(buffer, CancellationToken.None);
                        var bytes  = new byte[result.Count];
                        Array.Copy(buffer, bytes, result.Count);
                        var json     = JToken.Parse(Encoding.UTF8.GetString(bytes));
                        var typeCode = json["TypeCode"].ToString();
                        if (typeCode.EqualsIgnoreCase("Event"))
                        {
                            var type = (MahuaMessageType)json["EventType"].ToObject <int>();
                            if (type == MahuaMessageType.Friend || type == MahuaMessageType.Group)
                            {
                                var message = new AcceptedMessage()
                                {
                                    FromGroup      = json["FromNum"].ToObject <long>(),
                                    FromUser       = json["EventOperator"].ToObject <long>(),
                                    Content        = json["Message"].ToString(),
                                    IsGroupMessage = type == MahuaMessageType.Group
                                };
                                ReceivedMessageEvent?.Invoke(this, message);
                            }
                            continue;
                        }

                        if (typeCode.EqualsIgnoreCase("Api_GetQQListApiOut"))
                        {
                            QQ = json["Result"].ToObject <long>();
                            continue;
                        }
                        //WDNM为什么HTTP API请求过去的返回值会跑到Websocket返回
                        if (Callbacks.ContainsKey(typeCode.ToLower()))
                        {
                            Callbacks[typeCode.ToLower()]?.Invoke(json);
                            Callbacks.Remove(typeCode.ToLower());
                            continue;
                        }
                    }
                    catch
                    {
                        if (Running && Websocket.State != WebSocketState.Open)
                        {
                            try
                            {
                                Websocket = new ClientWebSocket();
                                await Websocket.ConnectAsync(new Uri(WebsocketUrl), CancellationToken.None);
                            }
                            catch
                            {
                                LogHelper.Error("Cannot reconnect websocket.");
                            }
                        }
                    }
                }
            }).Start();
        }