Created
May 21, 2012 23:30
-
-
Save prabirshrestha/2765381 to your computer and use it in GitHub Desktop.
ASP.NET 4.5 WebSockets echo server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sample echo server written in asp.net 4.5. | |
This sample must be run on full IIS and requires Win8 Beta with WebSockets enabled in IIS. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head id="Head1" runat="server"> | |
<title></title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<input type="text" id="message"/> | |
<input type="button" value="send" id="send"/> | |
<div id='messages'></div> | |
</form> | |
<script type="text/javascript"> | |
var socket, | |
$txt = document.getElementById('message'), | |
$messages = document.getElementById('messages'); | |
if (typeof (WebSocket) !== 'undefined') { | |
socket = new WebSocket("ws://localhost/WebSocketHelloEchoServer/echo.ashx"); | |
} else { | |
socket = new MozWebSocket("ws://localhost/WebSocketHelloEchoServer/echo.ashx"); | |
} | |
socket.onmessage = function (msg) { | |
var $el = document.createElement('p'); | |
$el.innerHTML = msg.data; | |
$messages.appendChild($el); | |
}; | |
document.getElementById('send').onclick = function () { | |
socket.send($txt.value); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ WebHandler Language="C#" CodeBehind="echo.ashx.cs" Class="WebSocketHelloEchoServer.echo" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net.WebSockets; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.WebSockets; | |
namespace WebSocketHelloEchoServer | |
{ | |
/// <summary> | |
/// Summary description for echo | |
/// </summary> | |
/// <remarks>http://evolpin.wordpress.com/2012/02/17/html5-websockets-revolution/</remarks> | |
public class echo : IHttpHandler | |
{ | |
// list of client WebSockets that are open | |
private static readonly IList<WebSocket> Clients = new List<WebSocket>(); | |
// ensure thread-safety of the WebSocket clients | |
private static readonly ReaderWriterLockSlim Locker = new ReaderWriterLockSlim(); | |
public void ProcessRequest(HttpContext context) | |
{ | |
ProcessRequest(new HttpContextWrapper(context)); | |
} | |
public void ProcessRequest(HttpContextBase context) | |
{ | |
if (context.IsWebSocketRequest) | |
context.AcceptWebSocketRequest(ProcessSocketRequest); | |
} | |
private async Task ProcessSocketRequest(AspNetWebSocketContext context) | |
{ | |
var socket = context.WebSocket; | |
// add socket to socket list | |
Locker.EnterWriteLock(); | |
try | |
{ | |
Clients.Add(socket); | |
} | |
finally | |
{ | |
Locker.ExitWriteLock(); | |
} | |
// maintain socket | |
while (true) | |
{ | |
var buffer = new ArraySegment<byte>(new byte[1024]); | |
// async wait for a change in the socket | |
var result = await socket.ReceiveAsync(buffer, CancellationToken.None); | |
if (socket.State == WebSocketState.Open) | |
{ | |
// echo to all clients | |
foreach (var client in Clients) | |
{ | |
await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None); | |
} | |
} | |
else | |
{ | |
// client is no longer available - delete from list | |
Locker.EnterWriteLock(); | |
try | |
{ | |
Clients.Remove(socket); | |
} | |
finally | |
{ | |
Locker.ExitWriteLock(); | |
} | |
break; | |
} | |
} | |
} | |
public bool IsReusable | |
{ | |
get | |
{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, i am new to web sockets technology.
I followed your code same as it is. Am unable to get connection.
Can u help me?
I am using windows 10 and IIS 10.0
Regards,
Soumya