Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.74 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. javascript and C# server comunication
  2. connection.Send(Encoding.ASCII.GetBytes("HTTP/1.1 101 Switching ProtocolsrnUpgrade:  WebSocketrnConnection: UpgradernSec-WebSocket-Accept: " + aux));
  3. connection.Send(Encoding.ASCII.GetBytes("rnrn"));
  4.        
  5. public static String ComputeWebSocketHandshakeSecurityHash09(String secWebSocketKey)
  6.     {
  7.         const String MagicKEY = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  8.         String secWebSocketAccept = String.Empty;
  9.  
  10.         // 1. Combine the request Sec-WebSocket-Key with magic key.
  11.         String ret = secWebSocketKey + MagicKEY;
  12.  
  13.         Console.WriteLine("-   " + ret + "   -");
  14.  
  15.         // 2. Compute the SHA1 hash
  16.         SHA1 sha = new SHA1CryptoServiceProvider();
  17.         byte[] sha1Hash = sha.ComputeHash(Encoding.UTF8.GetBytes(ret));
  18.  
  19.         // 3. Base64 encode the hash
  20.         secWebSocketAccept = Convert.ToBase64String(sha1Hash);
  21.  
  22.         return secWebSocketAccept;
  23.     }
  24.        
  25. var ws;
  26. $(document).ready(function () {
  27.  
  28. // test if the browser supports web sockets
  29. if ("WebSocket" in window) {
  30.     debug("Browser supports web sockets!", 'success');
  31.     connect($('#host').val());
  32.     $('#console_send').removeAttr('disabled');
  33. } else {
  34.     debug("Browser does not support web sockets", 'error');
  35. };
  36.  
  37. // function to send data on the web socket
  38. function ws_send(str) {
  39.     try {
  40.         ws.send(str);
  41.     } catch (err) {
  42.         debug(err, 'error');
  43.     }
  44. }
  45.  
  46. // connect to the specified host
  47. function connect(host) {
  48.  
  49.     debug("Connecting to " + host + " ...");
  50.     try {
  51.         ws = new WebSocket(host); // create the web socket
  52.     } catch (err) {
  53.         debug(err, 'error');
  54.     }
  55.     $('#host_connect').attr('disabled', true); // disable the 'reconnect' button
  56.  
  57.     ws.onopen = function () {
  58.         debug("connected... ", 'success'); // we are in! Big Grin | :-D
  59.     };
  60.  
  61.     ws.onmessage = function (evt) {
  62.         debug(evt.data, 'response'); // we got some data - show it omg!!
  63.     };
  64.  
  65.     ws.onclose = function () {
  66.         debug("Socket closed!", 'error'); // the socket was closed (this could be an error or simply that there is no server)
  67.         $('#host_connect').attr('disabled', false); // re-enable the 'reconnect button
  68.     };
  69. };
  70.  
  71. // function to display stuff, the second parameter is the class of the <p> (used for styling)
  72. function debug(msg, type) {
  73.     $("#console").append('<p class="' + (type || '') + '">' + msg + '</p>');
  74. };
  75.  
  76. // the user clicked to 'reconnect' button
  77. $('#host_connect').click(function () {
  78.     debug("n");
  79.     connect($('#host').val());
  80. });
  81.  
  82. // the user clicked the send button
  83. $('#console_send').click(function () {
  84.     ws_send($('#console_input').val());
  85. });
  86.  
  87. $('#console_input').keyup(function (e) {
  88.     if(e.keyCode == 13) // enter is pressed
  89.         ws_send($('#console_input').val());
  90. });
  91.  
  92. });