Example #1
0
    private void SendMessageToChat(string text, Message.MessageType messageType)
    {
        if (messageList.Count >= MAX_MESSAGES)
        {
            Destroy(messageList[0].textObject.gameObject);
            messageList.Remove(messageList[0]);
            Debug.Log("Max messages in list, removing message at index 0.");
        }

        // Create messaage
        Message newMessage = new Message();

        newMessage.text = text;
        GameObject textObject = Instantiate(pfTextContainer, chatPanel);

        // Set text
        newMessage.textObject      = textObject.transform.GetChild(0).GetComponent <TMP_Text>();
        newMessage.textObject.text = newMessage.text;

        // Apply color
        newMessage.messageType      = messageType;
        newMessage.textObject.color = newMessage.messageColor;

        // Save reference and fire event
        messageList.Add(newMessage);
        OnMessageSend?.Invoke();
    }
    /// <summary>
    /// Tells the server to send a packet, saves it and triggers the OnMessageSend event
    /// </summary>
    /// <param name="data"> The data for the packet (a serialized NetworkEvent)</param>
    /// <param name="time"> The time the packet was send (0 for the current time, other time if this is a packet that will be resend)</param>
    public void SendPacket(byte[] data, float time)
    {
        Packet packet = _gameServer.SendPacket(_endPoint, _sequenceNumber++, _remoteSequenceNumber, _ackBitField, data, time);

        _packetsSend.Add(packet.GetPacketSequenceNumber(), packet);
        OnMessageSend?.Invoke(packet);
    }
 private void p_OnMessageSend(P2PClient sender, ulong userId, Memory <byte> msg)
 {
     if (crypter.IsConnectionSafe(userId, msg))
     {
         Memory <byte> deMsg = crypter.Decrypt(userId, msg);
         if (deMsg.Length > 0)
         {
             dynamic json = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(deMsg.Span));
             messages.Push(new PackageInfo(json, userId));
             OnMessageSend?.Invoke(this, userId, messages.Peek());
         }
     }
 }
Example #4
0
        private async void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            foreach (KeyValuePair <string, SapPacket> item in this.Program)
            {
                byte[] buffer = item.Value.ToBytes();
                await _udpClient.SendAsync(buffer, buffer.Length, new IPEndPoint(this.Address, this.Port));

                OnMessageSend?.Invoke(this,
                                      new MessageSendEventArgs()
                {
                    Packet = item.Value
                });
            }
        }
Example #5
0
        public async Task SendAsync(byte[] buffer, WebSocketMessageType messageType = WebSocketMessageType.Text)
        {
            try
            {
                await ws.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), messageType, true, CancellationToken.None);

                OnMessageSend?.Invoke(this, new Message {
                    Client = this, MessageType = messageType, Data = buffer
                });
            }
            catch
            {
                Close();
            }
        }
 public async Task SendAsync(byte[] buffer, WebSocketMessageType messageType = WebSocketMessageType.Text)
 {
     var t = ws.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), messageType, true, CancellationToken.None);
     await t.ContinueWith((task) =>
     {
         if (task.IsFaulted)
         {
             Close();
         }
         else
         {
             OnMessageSend?.Invoke(this, new Message {
                 Client = this, MessageType = messageType, Data = buffer
             });
         }
     });
 }
Example #7
0
        public void Start()
        {
            // first init
            foreach (KeyValuePair <string, SapPacket> item in this.Program)
            {
                byte[] buffer = item.Value.ToBytes();
                _udpClient.SendAsync(buffer, buffer.Length, new IPEndPoint(this.Address, this.Port));

                OnMessageSend?.Invoke(this,
                                      new MessageSendEventArgs()
                {
                    Packet = item.Value
                });
            }

            // start timer
            _timer.Start();
        }
Example #8
0
        public void Stop()
        {
            // stop timer
            _timer.Stop();

            // Send SAP Deletion packet
            foreach (KeyValuePair <string, SapPacket> item in this.Program)
            {
                SapPacket sap = (SapPacket)item.Value.Clone();
                sap.MessageType = MessageType.Deletion;
                byte[] buffer = sap.ToBytes();
                _udpClient.SendAsync(buffer, buffer.Length, new IPEndPoint(this.Address, this.Port));

                OnMessageSend?.Invoke(this,
                                      new MessageSendEventArgs()
                {
                    Packet = sap
                });
            }
        }
Example #9
0
        private void SendMessage(Message message)
        {
            if (gatewayState != GatewayState.Connected &&
                gatewayState != GatewayState.ConnectingToGateway)
            {
                LogError("Failed to send message. Gateway is not connected.");
                return;
            }

            message.incoming = false;

            OnMessageSend?.Invoke(message);

            UpdateSensorFromMessage(message);

            string mes = message.ParseToMySensorsMessage();

            connectionPort.SendMessage(mes);


            AddDecodedMessageToLog(message);
        }
Example #10
0
        /// <summary>
        /// Sends a message to the client.
        /// </summary>
        /// <param name="message">Message to send.</param>
        private void SendMessage(NetworkMessage message)
        {
            if (client == null)
            {
                throw new Exception("Client is null, no messages can be send anymore");
            }

            try
            {
                if (!client.Connected)
                {
                    return;
                }

                // Serialize message
                string serializedMessage = JsonConvert.SerializeObject(message);

                // Get message length
                byte[] data   = Encoding.UTF8.GetBytes(serializedMessage);
                byte[] length = IntToBytes(data.Length);

                // Send length followed by message
                clientStream.Write(length, 0, length.Length);
                clientStream.Write(data, 0, data.Length);

                // Notify listeners.
                if (OnMessageSend != null)
                {
                    OnMessageSend.Invoke(this, message);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #11
0
 protected void OnMessageSendEvent(string message)
 {
     OnMessageSend?.Invoke(this, new MessageEventArgs(message));
 }
Example #12
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public static async Task Send(SendableMsg message)
        {
            OnMessageSend?.Invoke(message);
            return;
        }
 virtual protected void WshOnMessageSend(object sender, Message message)
 {
     OnMessageSend?.Invoke(this, message);
 }
Example #14
0
File: Config.cs Project: Iyui/Gmage
 public void MessageSend(MessageEventArgs e)
 {
     OnMessageSend?.Invoke(e);
 }