Reputation: 2402
I'm new in web sockets and trying to create one using asp.net Generic Handler and JavaScript WebSocket Class
JavaScript
<script type="text/javascript">
window.onload= function () {
var name = prompt('what is your name?:');
var url = 'ws://localhost:5707/ws.ashx?name=' + name;
var ws = new WebSocket(url);
ws.onopen = function () {
alert('Connection Opened');
};
ws.onmessage = function (e) {
};
ws.onclose = function () {
alert('Connection Close');
};
ws.onerror = function (e) {
alert('Error')
};
}
</script>
C# Generic Handler Called ws.ashx
public class ws : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.IsWebSocketRequest)
context.AcceptWebSocketRequest(new TestWebSocketHandler());
}
public bool IsReusable
{
get
{
return false;
}
}
}
Class TestWebSocketHandler which is inherits from WebSocketHandler
public class TestWebSocketHandler : WebSocketHandler
{
private static WebSocketCollection clients = new WebSocketCollection();
private string name;
public override void OnOpen()
{
this.name = this.WebSocketContext.QueryString["name"];
clients.Add(this);
clients.Broadcast(name + " has connected.");
}
public override void OnMessage(string message)
{
clients.Broadcast(string.Format("{0} said: {1}", name, message));
}
public override void OnClose()
{
clients.Remove(this);
clients.Broadcast(string.Format("{0} has gone away.", name));
}
}
My Problem is
when the websocket intend to open i noticed that when went to the handler the
context.IsWebSocketRequest // returns false
and then it fires Error on the client says
Firefox can't establish a connection to the server at ws://localhost:5707/ws.ashx?name=j
and then close the connection instantiation
i need to know where is the problem ? kindly
i'm using vs 2013 under windows 7 and i think its IIS 6 which i work on
Upvotes: 5
Views: 6517
Reputation: 59
Reason could be any of these:
system.webServer
section:<handlers>
<add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/>
</handlers>
Upvotes: 2
Reputation: 35905
WebSockets will only work on ASP.NET applications running on Windows 8 or Windows 2012 I am afraid. Despite of the API been included on .NET 4.5.1, it won't work if you are not using those operating system. Actually, if you try to use the ClientWebSocket
it will throw a PlatformNotSupportedException
.
If you cannot get any of those operating systems, you can check alternatives:
Upvotes: 5