public void Fire(string _text) { if (NewMessageEvent != null) { NewMessageEvent.Invoke(_text); } }
public void StartService() { if (_isRun) { return; } Task.Run(() => { _isRun = true; while (_isRun) { var parameters = new Dictionary <string, string>(); var result = _httpHelper.GetRequest("msg.getUpdates", parameters); var data = (JObject)result.Data; GetUpdates updates; if (result.Status) { updates = data.ToObject <GetUpdates>(); } else { throw new BoopRootException(data?.ToObject <Error>()?.Message, (data?.ToObject <Error>()).Code); } if (updates.Messages.Count > 0) { NewMessageEvent?.Invoke(updates.Messages); } } }); }
public void OnNext(LogEvent value) { string message = value.RenderMessage(); string formatted = value.Level + ": " + message; NewMessageEvent?.Invoke(value.Level, formatted); }
public void AddNewMessagesFromJson(string json) { RestreamChatMessage[] messages = JsonUtility.FromJson <RestreamChatMessage[]>(json); RestreamChatMessage[] newMessages = messages.Except(_messages).ToArray(); for (int i = 0; i < newMessages.Length; i++) { _messages.Add(newMessages[i]); _onMessagesDetected.Invoke(newMessages[i]); } }
public void MessageProcessor(object sender, MessageEventArgs e) { if (!_isStarted) { return; } var model = new Message(); model.Platform = Enums.MessengerPlatform.Telegam; model.MessageTG = e.Message; model.ChatId = e.Message.Chat.Id; model.Text = e.Message.Text; NewMessageEvent.Invoke(model); }
/// <summary> /// 接收数据的回调函数 /// </summary> /// <param name="ar"></param> private void PipeReadCallback(IAsyncResult ar) { this.NamedPipeClientStream = (NamedPipeClientStream)ar.AsyncState; var count = this.NamedPipeClientStream.EndRead(ar); if (count > 0) { string message = encoding.GetString(data, 0, count); NewMessageEvent?.Invoke(message); //简化调用方式 等同于if(NewMessageEvent != Null) {NewMessageEvent(message)} this.NamedPipeClientStream.BeginRead(data, 0, data.Length, new AsyncCallback(PipeReadCallback), this.NamedPipeClientStream); } else if (count == 0) { ConnectFailEvent?.Invoke(); } }
private void GetPendingMessages() { FlattiverseMessage message; bool messagesReceived = false; messageLock.AcquireWriterLock(100); while (_connector.NextMessage(out message)) { _messages.Add(message); messagesReceived = true; } messageLock.ReleaseWriterLock(); if (messagesReceived) { NewMessageEvent?.Invoke(); } }
public void Read() { try { int bytesRead = _stream.ReadAsync(_readBuffer, 0, _readBuffer.Length).Result; if (bytesRead > 0) { StringBuilder messageData = new StringBuilder(); Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(_readBuffer, 0, bytesRead)]; decoder.GetChars(_readBuffer, 0, bytesRead, chars, 0); messageData.Append(chars); NewMessageEvent?.Invoke(messageData); } } catch (Exception e) { throw e; } }
//public delegate void ClientClosedDelegate(object sender); //public event ClientClosedDelegate ClientClosedEvent; /// <summary> /// Initialize a new TcpClient from exsiting built-in TcpClient. /// </summary> /// <param name="client">New TcpClient</param> /// <param name="isConnected">If the client is already connected.</param> public MyClient(TcpClient client, bool isConnected) { IsRunning = isConnected; Client = client; ClientThread = new Thread(delegate() // Create a new thread to handle the event. { while (IsRunning) { if (Client.GetStream().DataAvailable) { // When new data is available to be read. byte[] data = new byte[Client.Available]; Client.GetStream().Read(data, 0, data.Length); if (data.Length == 1) { // If it is an empty message. NewMessageEvent?.Invoke(this, null); } else { // Send the received data to the handler. var result = new byte[data.Length - 1]; Array.Copy(data, 1, result, 0, data.Length - 1); NewMessageEvent?.Invoke(this, result); } } } Dispose(); }) { IsBackground = true }; if (isConnected) { ClientThread.Start(); // Srart the thread. } }
/// <summary> /// New message arrives. It will resend the message to an handler. /// </summary> /// <param name="sender">The server that gets the message.</param> /// <param name="data">The data that is received.</param> private void NewClient_NewMessageEvent(object sender, byte[] data) { NewMessageEvent?.Invoke(this, sender, data); }
/// <summary> /// Send new message /// </summary> /// <param name="message">your message</param> public static void SetMessage(string message) { NewMessageEvent?.Invoke(message); }
private void SeriesLongPoll() { while (_isStart) { try { var json = Request(); if (json == null) { goto End; } var response = JsonConvert.DeserializeObject <RootLongPollModel>(json); if (response.Ts == 0) { _logger.Error("Возникла ошибка в longPoll. Получаю новые server и ts..."); ResetSettingsLongPoll(); SetSettingLongPoll(); } _ts = response.Ts; _logger.Trace("Обработка полученных сообщений."); _logger.Trace($"Получено обновлений: {response.Updates.Count}"); foreach (var update in response.Updates) { var type = update.Type; if (type == "message_new") { var model = new Message(); var obj = (JObject)update.Object; var message = obj.ToObject <VkNet.Model.Message>(); model.Platform = Enums.MessengerPlatform.Vkontakte; model.MessageVK = message; NewMessageEvent?.Invoke(model); } else if (type == "message_reply") { var model = new Message(); var obj = (JObject)update.Object; var message = obj.ToObject <VkNet.Model.Message>(); model.Platform = Enums.MessengerPlatform.Vkontakte; model.MessageVK = message; NewMessageReplyEvent?.Invoke(model); } else if (type == "message_edit") { var model = new Message(); var obj = (JObject)update.Object; var message = obj.ToObject <VkNet.Model.Message>(); model.Platform = Enums.MessengerPlatform.Vkontakte; model.MessageVK = message; MessageEditEvent?.Invoke(model); } else if (type == "message_allow") { var obj = (JObject)update.Object; var model = obj.ToObject <UserMessagesActionModel>(); MessageAllowEvent?.Invoke(model); } else if (type == "message_deny") { var obj = (JObject)update.Object; var model = obj.ToObject <UserMessagesActionModel>(); MessageDenyEvent?.Invoke(model); } else if (type == "group_leave") { var obj = (JObject)update.Object; var model = obj.ToObject <UserMessagesActionModel>(); GroupLeaveEvent?.Invoke(model); } else if (type == "group_join") { var obj = (JObject)update.Object; var model = obj.ToObject <UserMessagesActionModel>(); GroupJoinEvent?.Invoke(model); } else { _logger.Error($"Тип обновления {type} пока что не поддерживается. Отключите его получение в настройках сообщества, если это сообщение Вам мешает."); } } End: var i = 1; //_logger.Trace("\n"); } catch (Exception e) { _logger.Error($"Произошла ошибка при обработке обновлений:\n {e.ToString()}"); } } }