Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building real-time apps with WebSockets


Published on

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.

Published in: Technology, News & Politics
  • Hi,
    Could I program a video chat via web socket? I'm trying to look for some way, but up to now, i haven't been able to do it.
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Building real-time apps with WebSockets

  1. 1. Peter Himschootpeter@u2u.netU2U
  2. 2. “Real time web” is awesome!!!“A real pain to build”
  3. 3. Ian Hickson
  4. 4. Server Client Server“Comet” Client
  5. 5. a socket that works anywhere across the web…even through network intermediaries
  6. 6. public class GameHttpHandler : IHttpHandler{ public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) { var player = new GamePlayer(); FourInABruGame.Join(player); context.AcceptWebSocketRequest(player.SocketReceiver); } }
  7. 7. public 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);
  8. 8. 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);}
  9. 9. “ws://servername/socket” “wss://servername/socket”var ws = new WebSocket(socketUri);ws.openStateConst = WebSocket.OPEN;
  10. 10. ws.onopen = function () { display(Connected);}ws.onmessage = function (json) { processMessage(json);}...
  11. 11. ws.send(JSON.stringify({ type: "wins", player: player }));
  12. 12. Demo
  13. 13.