Esempio n. 1
0
        private void _client_MessageReceivedEvent(object sender, ClientServerUsingNamedPipes.Interfaces.MessageReceivedEventArgs e)
        {
            switch (e.Type)
            {
            case MessageType.Registered:
                txtStatus.Text = "Connected";

                break;

            case MessageType.Published:
                txtStatus.Text = "Data has been published to data channel - " + e.DataId;

                break;

            case MessageType.Subscribed:
                txtStatus.Text = "You have been subscribed to data channel - " + e.DataId;

                if (!lstSubscribedChannels.Items.Contains(e.DataId))
                {
                    lstSubscribedChannels.Items.Add(e.DataId);
                }

                break;

            case MessageType.Unsubscribed:
                txtStatus.Text = "You have been unsubscribed from data channel - " + e.DataId;

                if (lstSubscribedChannels.Items.Contains(e.DataId))
                {
                    lstSubscribedChannels.Items.Remove(e.DataId);
                }

                break;

            case MessageType.PublishSubscribed:
                txtStatus.Text = "You received published data from channel - " + e.DataId;

                lstReceivedData.Items.Add(new SubscribedData()
                {
                    DataId = e.DataId, DateTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), Data = e.JsonData
                });

                break;
            }
        }
Esempio n. 2
0
        private void CentralPipeServer_MessageReceivedEvent(object sender, ClientServerUsingNamedPipes.Interfaces.MessageReceivedEventArgs e)
        {
            if (e.Type == MessageType.Register)
            {
                clientsList[e.ReceiverId] = new PipeClient(e.ReceiverId);
                clientsList[e.ReceiverId].Start();

                clientsList[e.ReceiverId].SendMessage(new MessageReceivedEventArgs()
                {
                    Type = MessageType.Registered
                });
            }
            else if (e.Type == MessageType.Unregister)
            {
                if (clientsList.ContainsKey(e.ReceiverId))
                {
                    clientsList[e.ReceiverId].Stop();
                }
            }
            else if (e.Type == MessageType.Publish)
            {
                jsonDataList[e.DataId] = e.JsonData;

                if (clientsList.ContainsKey(e.ReceiverId))
                {
                    clientsList[e.ReceiverId].SendMessage(new MessageReceivedEventArgs()
                    {
                        Type = MessageType.Published, DataId = e.DataId
                    });
                }

                if (subscribersList.ContainsKey(e.DataId))
                {
                    var list = subscribersList[e.DataId];
                    foreach (var sub in list)
                    {
                        clientsList[sub].SendMessage(new MessageReceivedEventArgs()
                        {
                            DataId = e.DataId, JsonData = e.JsonData, Type = MessageType.PublishSubscribed
                        });
                    }
                }
            }

            else if (e.Type == MessageType.Subscribe)
            {
                if (!subscribersList.ContainsKey(e.DataId))
                {
                    subscribersList[e.DataId] = new List <string>();
                }

                if (!subscribersList[e.DataId].Contains(e.ReceiverId))
                {
                    subscribersList[e.DataId].Add(e.ReceiverId);
                }

                if (clientsList.ContainsKey(e.ReceiverId))
                {
                    clientsList[e.ReceiverId].SendMessage(new MessageReceivedEventArgs()
                    {
                        DataId = e.DataId, Type = MessageType.Subscribed
                    });
                }
            }
            else if (e.Type == MessageType.Unsubscribe)
            {
                if (subscribersList.ContainsKey(e.DataId))
                {
                    if (subscribersList[e.DataId].Contains(e.ReceiverId))
                    {
                        subscribersList[e.DataId].Remove(e.ReceiverId);
                    }
                }

                if (clientsList.ContainsKey(e.ReceiverId))
                {
                    clientsList[e.ReceiverId].SendMessage(new MessageReceivedEventArgs()
                    {
                        DataId = e.DataId, Type = MessageType.Unsubscribed
                    });
                }
            }
        }