Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created May 21, 2012 23:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save prabirshrestha/2765381 to your computer and use it in GitHub Desktop.
Save prabirshrestha/2765381 to your computer and use it in GitHub Desktop.
ASP.NET 4.5 WebSockets echo server
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.
<%@ 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>
<%@ WebHandler Language="C#" CodeBehind="echo.ashx.cs" Class="WebSocketHelloEchoServer.echo" %>
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;
}
}
}
}
@soumyaessl
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment