示例#1
0
 private void LinteningStream()
 {
     try
     {
         while (true)
         {
             var line = _reader.ReadLine();
             if (!string.IsNullOrEmpty(line))
             {
                 NewMessageReceived?.Invoke(this, new NewMessageEventArgs {
                     Message = line, User = ConnectedUserName
                 });
                 Console.WriteLine(line);
             }
             else
             {
                 _reader.Close();
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(" Pipe error " + e.Message);
         _reader.Close();
     }
 }
        public override void ResponseReceived(byte[] parameter)
        {
            switch ((TextChatCommunication)parameter[0])
            {
            case TextChatCommunication.ChatOpened:
                IsStarted = true;
                ChatStatusChanged?.Invoke(this, EventArgs.Empty);
                LogService.Receive((string)Application.Current.Resources["ChatOpened"]);
                break;

            case TextChatCommunication.ChatClosed:
                IsStarted = false;
                ChatStatusChanged?.Invoke(this, EventArgs.Empty);
                LogService.Warn((string)Application.Current.Resources["ChatClosed"]);
                break;

            case TextChatCommunication.ResponseMessage:
                NewMessageReceived?.Invoke(this,
                                           new ChatMessage
                {
                    Content   = Encoding.UTF8.GetString(parameter, 9, parameter.Length - 9),
                    Timestamp = new DateTime(BitConverter.ToInt64(parameter, 1)).ToLocalTime()
                });
                break;

            case TextChatCommunication.InitializationFailed:
                ChatInitalizationFailed?.Invoke(this, EventArgs.Empty);
                LogService.Error((string)Application.Current.Resources["ChatInitializationFailed"]);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#3
0
        /// <summary>
        /// Run on a background thread; returns only when done listening.
        /// </summary>
        public void ListenForUDPPackages()
        {
            try
            {
                _listener = new UdpClient(_portToListen);
            }
            catch (SocketException e)
            {
                //do nothing
            }

            if (_listener != null)
            {
                IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 0);

                while (_listening)
                {
                    try
                    {
                        byte[] bytes = _listener.Receive(ref groupEP);                         // waits for packet from Android.

                        //raise event
                        NewMessageReceived?.Invoke(this, new AndroidMessageArgs(bytes));
                    }
                    catch (SocketException se)
                    {
                        if (!_listening || se.SocketErrorCode == SocketError.Interrupted)
                        {
                            return;                             // no problem, we're just closing up shop
                        }
                        throw se;
                    }
                }
            }
        }
        private void GetMessages()
        {
            var queueUrlRequest       = new GetQueueUrlRequest("Messenger.fifo");
            var queueUrl              = _sqsClient.GetQueueUrl(queueUrlRequest).QueueUrl;
            var receiveMessageRequest = new ReceiveMessageRequest {
                QueueUrl = queueUrl
            };

            while ()
            {
                Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                var receiveMessageResponse = _sqsClient.ReceiveMessage(receiveMessageRequest);
                foreach (var message in receiveMessageResponse.Messages)
                {
                    var deserializedMessage = JsonConvert.DeserializeObject <MessageModel>(message.Body);
                    if (deserializedMessage.ChatId.Equals(_parentViewModel.ChatId, StringComparison.InvariantCulture) && !deserializedMessage.Author.Equals(_parentViewModel.Author))
                    {
                        NewMessageReceived?.Invoke(deserializedMessage);
                        var messageRecieptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;
                        var deleteRequest        = new DeleteMessageRequest
                        {
                            QueueUrl      = queueUrl,
                            ReceiptHandle = messageRecieptHandle
                        };
                        _sqsClient.DeleteMessage(deleteRequest);
                    }
                }
            }
        }
示例#5
0
        private void Receive()
        {
            using (Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, _port);
                serverSocket.Bind(remoteEndPoint);

                server = serverSocket;

                //serverSocket.Listen(5);

                while (!_timeToDie)
                {
                    if (serverSocket.Poll(-1, SelectMode.SelectRead))
                    {
                        byte[] inBuffer = new byte[serverSocket.Available];
                        int    count    = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);

                        byte[] trimedBytes = new byte[count];
                        Array.Copy(inBuffer, 0, trimedBytes, 0, count);

                        NewMessageReceived?.Invoke(remoteEndPoint, trimedBytes);
                    }
                    else
                    {
                        Thread.Sleep(0);
                    }
                }
            }
        }
示例#6
0
        public void AddNewMessage(string user, string message)
        {
            Message messageModel = new Message
            {
                Name = user,
                Text = message
            };

            NewMessageReceived?.Invoke(this, messageModel);
        }
        private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
        {
            // Send Response
            ValueSet response = new ValueSet
            {
                { "ResCode", 0 }
            };
            await args.Request.SendResponseAsync(response);

            NewMessageReceived?.Invoke(this, args.Request.Message);
        }
        void AddNewMessage(JObject message)
        {
            Message messageModel = new Message
            {
                Name         = message.GetValue("name").ToString(),
                Text         = message.GetValue("text").ToString(),
                TimeReceived = DateTime.Now
            };

            NewMessageReceived?.Invoke(this, messageModel);
        }
示例#9
0
        void AddNewMessage(JObject message)
        {
            var messageModel = new
            {
                sender       = message.GetValue("sender").ToString(),
                text         = message.GetValue("text").ToString(),
                timereceived = DateTime.Now
            };

            NewMessageReceived?.Invoke(this, messageModel);
        }
 void GenerateMessage()
 {
     while (true)
     {
         Thread.Sleep(_random.Next(500, 10000));
         NewMessageReceived?.Invoke(new MessageModel
         {
             Author      = "Numbers Generator",
             DateTimeUtc = DateTime.UtcNow,
             Text        = string.Join(", ", Enumerable.Range(_random.Next(1, 1000), 5))
         });
     }
 }
示例#11
0
        private void HandleSocketMessage(object sender, MessageEventArgs e)
        {
            var message = e.Data;

            try
            {
                var baseMessage = JsonConvert.DeserializeObject <BaseMessage>(message);
                if (baseMessage.Type == SocketMessageTypes.SuccessfullyAuthorized)
                {
                    Authorized?.Invoke();
                }
                else if (baseMessage.Type == SocketMessageTypes.RoomCreated)
                {
                    var response = JsonConvert.DeserializeObject <SocketResponseDTO <RoomDTO> >(message);
                    RoomCreated?.Invoke(response);
                }
                else if (baseMessage.Type == SocketMessageTypes.NewMessage)
                {
                    var response = JsonConvert.DeserializeObject <SocketResponseDTO <MessageDTO> >(message);
                    NewMessageReceived?.Invoke(response);
                }
                else if (baseMessage.Type == SocketMessageTypes.RoomParticipated)
                {
                    var response = JsonConvert.DeserializeObject <SocketResponseDTO <RoomParticipatedDTO> >(message);
                    RoomParticipated?.Invoke(response);
                }
                else if (baseMessage.Type == SocketMessageTypes.RoomLeft)
                {
                    var response = JsonConvert.DeserializeObject <SocketResponseDTO <RoomParticipatedDTO> >(message);
                    RoomLeft?.Invoke(response);
                }
                else if (baseMessage.Type == SocketMessageTypes.FileStatusChanged)
                {
                    var response = JsonConvert.DeserializeObject <SocketResponseDTO <FileStatusDTO> >(message);
                    FileStatusChanged?.Invoke(response);
                }
            }
            catch (Exception ex)
            {
                Failed?.Invoke(ex.Message);
            }
        }
示例#12
0
 private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     if (e.ProgressPercentage == 0)
     {
         try
         {
             var chkin_info = JsonConvert.DeserializeObject <CheckInInfo>(e.UserState.ToString());
             NewMessageReceived?.Invoke(this, new NewCheckInMessage
             {
                 CheckInInfo = chkin_info
             });
         }
         catch (Exception)
         {
             Debug.WriteLine("SMS NOT IN VALID FORMAT");
         }
         finally
         {
             Debug.WriteLine(e.UserState.ToString());
         }
     }
 }
示例#13
0
 private void AddNewMessage(MessageObject messageObject)
 {
     NewMessageReceived.Invoke(this, new EventArgs <MessageObject>(messageObject));
 }
示例#14
0
 /// <summary>
 /// Raises the NewMessageReceived event
 /// </summary>
 /// <param name="chatMessage">Chat message received</param>
 private void OnNewMessageReceived(ChatMessage chatMessage)
 {
     _messages.Add(chatMessage);
     NewMessageReceived?.Invoke(this, chatMessage);
 }
 protected virtual void OnNewMessageReceived(byte[] e)
 {
     NewMessageReceived?.Invoke(this, e);
 }
示例#16
0
 private void OnNewMessage(TMessage message)
 => NewMessageReceived?.Invoke(message);