Presentation is loading. Please wait.

Presentation is loading. Please wait.

Msdevcon.ru#msdevcon. Windows Phone 8 Networking Survival Kit Andy Wigley Microsoft UK.

Similar presentations


Presentation on theme: "Msdevcon.ru#msdevcon. Windows Phone 8 Networking Survival Kit Andy Wigley Microsoft UK."— Presentation transcript:

1 msdevcon.ru#msdevcon

2 Windows Phone 8 Networking Survival Kit Andy Wigley Microsoft UK

3

4 Http Programming with async and await

5

6 APIWP7.1WP8W8 System.Net.WebClient  System.Net.HttpWebRequest System.Net.Http.HttpClient  ( NuGet) Windows.Web.Syndication.SyndicationClient  Windows.Web.AtomPub.AtomPubClient  ASMX Web Services WCF Services OData Services

7 using System.Net;... WebClient client; public MainPage() {... client = new WebClient(); client.DownloadStringCompleted += client_DownloadStringCompleted; } void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { this.downloadedText = e.Result; } private void loadButton_Click(object sender, RoutedEventArgs e) { client.DownloadStringAsync(new Uri("http://MyServer/ServicesApplication/rssdump.xml")); }

8 using System.Net; using System.Threading.Tasks;... private async void LoadWithWebClient() { var client = new WebClient(); string response = await client.DownloadStringTaskAsync( new Uri("http://MyServer/ServicesApplication/rssdump.xml")); this.downloadedText = response; } private async void LoadWithHttpWebRequest() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.odata.org/Northwind/Northwind.svc/Suppliers"); request.Method = HttpMethod.Get; HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();... }

9 // Following requires HttpClient.Compression NuGet package var handler = new AdvancedREI.Net.Http.Compression.CompressedHttpClientHandler(); // Create the HttpClient HttpClient httpClient = new HttpClient(handler); // To use without compression support (but why do that?), use default HttpClient constructor // without the compression handler: HttpClient httpClient = new HttpClient(); // Optionally, define HTTP headers httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); // Make the call HttpResponseMessage response = await httpClient.GetAsync( "http://services.odata.org/Northwind/Northwind.svc/Suppliers"); response.EnsureSuccessStatusCode(); // Throws exception if bad HTTP status code string responseBodyAsText = await response.Content.ReadAsStringAsync();

10 DEMO HTTP Networking using Async Andy Wigley

11 Make Smart Decisions About Data Transfer

12 12

13

14 private const int IANA_INTERFACE_TYPE_OTHER = 1; private const int IANA_INTERFACE_TYPE_ETHERNET = 6; private const int IANA_INTERFACE_TYPE_PPP = 23; private const int IANA_INTERFACE_TYPE_WIFI = 71;... string network = string.Empty; // Get current Internet Connection Profile. ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile(); if (internetConnectionProfile != null) // if ‘null’, we are offline. { switch (internetConnectionProfile.NetworkAdapter.IanaInterfaceType) { case IANA_INTERFACE_TYPE_OTHER: cost += "Network: Other"; break; case IANA_INTERFACE_TYPE_ETHERNET: cost += "Network: Ethernet"; break; case IANA_INTERFACE_TYPE_WIFI: cost += "Network: Wifi\r\n"; break; default: cost += "Network: Unknown\r\n"; break; } }

15

16 DEMO Wire Serialization Andy Wigley

17 Wire Serialization FormatSize in Bytes ODATA XML73786 ODATA JSON ATOM34030 JSON ‘Lite’15540 JSON ‘Lite’ GZip8680

18 Implementing Compression

19

20 var request = HttpWebRequest.Create("http://yourPC:15500/NorthwindDataService.svc/Suppliers") as HttpWebRequest; request.Accept = "application/json"; request.Method = HttpMethod.Get; request.Headers["Accept-Encoding"] = "gzip"; HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); // Read the response into a Stream object. System.IO.Stream responseStream = response.GetResponseStream(); string data; var stream = new GZipInputStream(response.GetResponseStream()); using (var reader = new System.IO.StreamReader(stream)) { data = reader.ReadToEnd(); } responseStream.Close();

21 private void EnableGZipResponses(DataServiceContext ctx) { ctx.WritingRequest += new EventHandler ( (_, args) => { args.Headers["Accept-Encoding"] = "gzip"; } ); ctx.ReadingResponse += new EventHandler ( (_, args) => { if (args.Headers.ContainsKey("Content-Encoding") && args.Headers["Content-Encoding"].Contains("gzip")) { args.Content = new GZipStream(args.Content); } } ); } Reference:

22

23

24

25

26 DEMO Compression Andy Wigley

27 Store Files in SkyDrive

28

29

30

31

32

33 DEMO Store Files in SkyDrive Andy Wigley

34 Accessing Local Services from the Emulator

35

36

37

38

39 DEMO Accessing local services from the emulator Andy Wigley

40 Bluetooth

41

42

43

44

45 try { PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; var peers = await PeerFinder.FindAllPeersAsync(); } catch (Exception ex) { if ((uint)ex.HResult == 0x F) MessageBox.Show("Bluetooth is switched off"); }

46

47 // Register for incoming connection requests PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested; // Start advertising ourselves so that our peers can find us PeerFinder.DisplayName = "TicTacToe BT"; PeerFinder.Start();

48 // Register for incoming connection requests PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested; // Start advertising ourselves so that our peers can find us PeerFinder.DisplayName = "TicTacToe BT"; PeerFinder.Start();

49 StreamSocket socket; async void PeerFinder_ConnectionRequested(object sender, ConnectionRequestedEventArgs args) { if ( args.PeerInformation.DisplayName == "RobsPhone" ) { socket = await PeerFinder.ConnectAsync(args.PeerInformation); PeerFinder.Stop(); }

50 DEMO Bluetooth communication Andy Wigley

51 NFC

52

53

54

55 ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device != null) { PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnectionStateChanged; // Start finding peer apps, while making this app discoverable by peers PeerFinder.Start(); }

56 ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device != null) { PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnStateChanged; // Include the Windows 8 version of our app as possible peer PeerFinder.AlternateIdentities.Add("Windows", "my Win8 appID"); // Start finding peer apps, while making this app discoverable by peers PeerFinder.Start(); }

57 void OnTriggeredConnStateChanged(object sender, TriggeredConnectionStateChangedEventArgs args) { switch (args.State) { case TriggeredConnectState.Listening: // Connecting as host break; case TriggeredConnectState.PeerFound: // Proximity gesture is complete – setting up link break; case TriggeredConnectState.Connecting: // Connecting as a client break; case TriggeredConnectState.Completed: // Connection completed, get the socket streamSocket = args.Socket; break; case TriggeredConnectState.Canceled: // ongoing connection cancelled break; case TriggeredConnectState.Failed: // Connection was unsuccessful break; }

58 PeerFinder.AllowBluetooth = true; PeerFinder.AllowInfrastructure = true;

59 DEMO NFC ‘Tap to Connect’ and ‘Tap to Share’ Andy Wigley

60 Windows.Networking.Proximity.ProximityDevice proximityDevice; long publishedMessageId = -1; private void PublishUriButton_Click(object sender, RoutedEventArgs e) { if (proximityDevice == null) proximityDevice = ProximityDevice.GetDefault(); // Make sure NFC is supported if (proximityDevice != null) { // Stop publishing the current message. if (publishedMessageId != -1) { proximityDevice.StopPublishingMessage(publishedMessageId); } // Publish the new one publishedMessageId = proximityDevice.PublishUriMessage( new Uri("zune:navigate?appid=351decc7-ea2f-e c-00237de2db9e")); }

61

62 Контакты Andy Wigley Microsoft & andywigley.com

63 © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "Msdevcon.ru#msdevcon. Windows Phone 8 Networking Survival Kit Andy Wigley Microsoft UK."

Similar presentations


Ads by Google