Permalink
Browse files

First WebSockets tries.

  • Loading branch information...
1 parent a0fc581 commit df183719a89488267f5171b516ed00e47ed21347 @Bobris committed Jul 31, 2013
Showing with 108 additions and 1 deletion.
  1. +50 −0 BenchmarkClients/websockets.html
  2. +58 −1 SampleOwinApp/Sample.cs
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>WebSocket Test</title>
+ </head>
+ <body>
+ <h2>WebSocket Test</h2>
+ <button id="connect" type="button">Connect</button>
+ <div id="output"></div>
+
+ <script language="javascript" type="text/javascript">
+ var wsUri = "ws://localhost:8888/echo";
+ var output;
+ function testWebSocket()
+ {
+ writeToScreen("CONNECTING");
+ websocket = new WebSocket(wsUri);
+ websocket.onopen = function(evt) { onOpen(evt) };
+ websocket.onclose = function(evt) { onClose(evt) };
+ websocket.onmessage = function(evt) { onMessage(evt) };
+ websocket.onerror = function(evt) { onError(evt) };
+ }
+
+ function onOpen(evt) { writeToScreen("CONNECTED"); doSend("WebSocket rocks"); }
+ function onClose(evt) { writeToScreen("DISCONNECTED"); }
+ function onMessage(evt) { writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); websocket.close(); }
+ function onError(evt)
+ {
+ writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
+ }
+ function doSend(message) { writeToScreen("SENT: " + message); websocket.send(message); }
+ function writeToScreen(message)
+ {
+ var pre = document.createElement("p");
+ pre.style.wordWrap = "break-word";
+ pre.innerHTML = message; output.appendChild(pre);
+ }
+
+ function init()
+ {
+ output = document.getElementById("output");
+ document.getElementById("connect").addEventListener("click", testWebSocket, false);
+ }
+
+ window.addEventListener("load", init, false);
+ </script>
+
+ </body>
+</html>
@@ -1,10 +1,41 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.IO;
+using System.Net.WebSockets;
+using System.Threading;
using System.Threading.Tasks;
using Microsoft.Owin;
namespace SampleOwinApp
{
+ using WebSocketAccept = Action<IDictionary<string, object>, Func<IDictionary<string, object>, Task>>;
+ using WebSocketCloseAsync =
+ Func<int /* closeStatus */,
+ string /* closeDescription */,
+ CancellationToken /* cancel */,
+ Task>;
+ using WebSocketReceiveAsync =
+ Func<ArraySegment<byte> /* data */,
+ CancellationToken /* cancel */,
+ Task<Tuple<int /* messageType */,
+ bool /* endOfMessage */,
+ int /* count */>>>;
+ using WebSocketSendAsync =
+ Func<ArraySegment<byte> /* data */,
+ int /* messageType */,
+ bool /* endOfMessage */,
+ CancellationToken /* cancel */,
+ Task>;
+
+ internal static class DictionaryExtensions
+ {
+ internal static T Get<T>(this IDictionary<string, object> dictionary, string key)
+ {
+ object value;
+ return dictionary.TryGetValue(key, out value) ? (T) value : default(T);
+ }
+ }
+
public class Sample
{
public static Task App(IDictionary<string, object> arg)
@@ -18,6 +49,32 @@ public static Task App(IDictionary<string, object> arg)
resp.Write("Hello World!");
return Task.Delay(0);
}
+ if (req.Path == "/echo")
+ {
+ var accept = arg.Get<WebSocketAccept>("websocket.Accept");
+ if (accept == null)
+ {
+ resp.StatusCode = 500;
+ return Task.Delay(0);
+ }
+ accept(
+ null,
+ async wsEnv =>
+ {
+ var sendAsync = wsEnv.Get<WebSocketSendAsync>("websocket.SendAsync");
+ var receiveAsync = wsEnv.Get<WebSocketReceiveAsync>("websocket.ReceiveAsync");
+ var closeAsync = wsEnv.Get<WebSocketCloseAsync>("websocket.CloseAsync");
+
+ var buffer = new ArraySegment<byte>(new byte[1000]);
+ var serverReceive = await receiveAsync(buffer, CancellationToken.None);
+ await sendAsync(new ArraySegment<byte>(buffer.Array, 0, serverReceive.Item3),
+ serverReceive.Item1, serverReceive.Item2, CancellationToken.None);
+ await closeAsync((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
+ });
+
+ return Task.Delay(0);
+
+ }
var p = Path.Combine(@"c:\Research\SampleWebPage", req.Path.Substring(1));
if (File.Exists(p))
{

0 comments on commit df18371

Please sign in to comment.