private void PeerList_Click(object sender, RoutedEventArgs e) { // Убедимся, что пользователь щелкнул по кнопке с именем MessageButton if (((Button)e.OriginalSource).Name == "MessageButton") { // Получение пира и прокси, для отправки сообщения PeerEntry peerEntry = ((Button)e.OriginalSource).DataContext as PeerEntry; if (peerEntry != null && peerEntry.ServiceProxy != null) { try { peerEntry.ServiceProxy.SendMessage("Привет друг!", ConfigurationManager.AppSettings["username"]); } catch (CommunicationException) { } } } }
void resolver_ResolveProgressChanged(object sender, ResolveProgressChangedEventArgs e) { PeerNameRecord peer = e.PeerNameRecord; foreach (IPEndPoint ep in peer.EndPointCollection) { if (ep.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { try { string endpointUrl = string.Format("net.tcp://{0}:{1}/P2PService", ep.Address, ep.Port); NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; IP2PService serviceProxy = ChannelFactory<IP2PService>.CreateChannel( binding, new EndpointAddress(endpointUrl)); PeerEntry pe = new PeerEntry(); pe.PeerName = peer.PeerName; pe.ServiceProxy = serviceProxy; pe.DisplayString = serviceProxy.GetName(); pe.ButtonsEnabled = true; PeerList.Items.Add(pe); } catch (EndpointNotFoundException) { PeerList.Items.Add( new PeerEntry { PeerName = peer.PeerName, DisplayString = "Неизвестный пир", ButtonsEnabled = false }); } } } }