24
Building Real-time Apps Using Web Sockets Peter Himschoo t U2U Trainer MS Regional Director

Building real-time apps with WebSockets

Embed Size (px)

DESCRIPTION

WebSockets is an emerging standard that enables real-time and bidirectional communication across the Web. You will learn how HTML5 web applications can make dramatic improvements in terms of user experience and performance by taking advantage of this technology. In this session we will focus on the new WCF 4.5 and ASP.NET 4.5 APIs supporting this standard.

Citation preview

Page 1: Building real-time apps with WebSockets

Building Real-time AppsUsing Web Sockets

Peter HimschootU2U TrainerMS Regional Director

Page 2: Building real-time apps with WebSockets

About me…

Peter [email protected]

U2UTraining company based in BrusselsSpecialized in developer training For .NET, SharePoint, HTML5

www.u2u.be

Page 3: Building real-time apps with WebSockets

Session Contents• Real-time communication• Web Sockets

Page 4: Building real-time apps with WebSockets

Web Sockets

Real-time communication with

Page 5: Building real-time apps with WebSockets

Real-Time web applications

“A real pain to build”

“Real time web” is awesome!!!

Page 6: Building real-time apps with WebSockets

We need...• Full duplex communication• Using sockets

• Real-time, event driven apps• Tiny web packets

Ian Hickson

Page 7: Building real-time apps with WebSockets

Keeping data up-to-datePo

llin

g

Server

Client

Lon

gPo

llin

g

Server

Client

Polling interval

“Comet”

Page 8: Building real-time apps with WebSockets

A WebSocket is…

a socket that works anywhere across the web …even through network intermediaries

Page 9: Building real-time apps with WebSockets

We get...• Bidirectional, single TCP socket• In & out of browser• Real time performance• Simple programming model• Bandwidth savings• Scalability advantages

Page 10: Building real-time apps with WebSockets

On the Server

WebSockets

Page 11: Building real-time apps with WebSockets

Steps on the server• Add a HttpHandler• Add a SocketReceiver• Send Messages

Page 12: Building real-time apps with WebSockets

Add a HttpHandler• Check for websocket request

public class GameHttpHandler : IHttpHandler{ public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) { var player = new GamePlayer(); FourInABruGame.Join(player); context.AcceptWebSocketRequest(player.SocketReceiver); } }

Page 13: Building real-time apps with WebSockets

Add a SocketReceiver• Receive JSON messagespublic async Task SocketReceiver(AspNetWebSocketContext context){ var socket = context.WebSocket as AspNetWebSocket; var buffer = new ArraySegment<byte>(new byte[2048]); while (true) { var message = await socket.ReceiveAsync(buffer, CancellationToken.None); // Convert to JSON var json = Encoding.UTF8.GetString(buffer.Array, 0, message.Count); var test = new { type = "" }; test = JsonConvert.DeserializeAnonymousType(json, test);

Page 14: Building real-time apps with WebSockets

Send Messages• Best sent as JSON message

public async Task SendMessage(SocketMessage message){ string json = JsonConvert.SerializeObject(message); var buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(json)); await Context.WebSocket.SendAsync(buffer, WebSocketMessageType.Text, endOfMessage: true, cancellationToken: CancellationToken.None);}

Page 15: Building real-time apps with WebSockets

On the Client

WebSockets

Page 16: Building real-time apps with WebSockets

Steps on the client• Create a WebSocket• Process messages• Send messages

Page 17: Building real-time apps with WebSockets

Create a WebSocket• Note websocket uri

var ws = new WebSocket(socketUri);ws.openStateConst = WebSocket.OPEN;

“ws://servername/socket”“wss://servername/socket”

Page 18: Building real-time apps with WebSockets

Process messages• Handle open, message, close and error event

ws.onopen = function () { display('Connected');}

ws.onmessage = function (json) { processMessage(json);}

...

Page 19: Building real-time apps with WebSockets

Send messages• Create object, then send as JSON

ws.send(JSON.stringify({ type: "wins", player: player }));

Page 20: Building real-time apps with WebSockets

DemoWeb Sockets

Page 21: Building real-time apps with WebSockets

Session Summary• Web Sockets is all about...• Real time communication• Efficient

Page 22: Building real-time apps with WebSockets

Be what’s next• Find everything here

http://aka.ms/mbl-tech• Visual Studio Developer Preview Downloads

http://aka.ms/mbl-tech/devprev• MSDN HTML5 Developer Center

http://aka.ms/mbl-tech/html5

Page 23: Building real-time apps with WebSockets

Q&A

Page 24: Building real-time apps with WebSockets

© 2012 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.