I'm new in web sockets and trying to create one using asp 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
I'm new in web sockets and trying to create one using asp 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
Share Improve this question edited Mar 31, 2014 at 9:12 Marwan asked Mar 31, 2014 at 8:59 MarwanMarwan 2,4021 gold badge21 silver badges35 bronze badges 4- Have you used fiddler or similar to see what is being sent/received? – Marc Gravell Commented Mar 31, 2014 at 9:06
- no but how it will helps – Marwan Commented Mar 31, 2014 at 9:07
- actually, you might need to use the firefox tools; change to the network tab, find the web-socket connection, and look at the headers. For example, here's mine from this site (stackoverflow.): i.sstatic/goQTY.png - what do you get against your site? Also: does it work in, say, Chrome? – Marc Gravell Commented Mar 31, 2014 at 9:15
- no it works in non of the top 5 browsers :( – Marwan Commented Mar 31, 2014 at 9:34
2 Answers
Reset to default 5WebSockets 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:
- XSocket.NET: http://xsockets/
- SuperWebSocket : http://superwebsocket.codeplex./
- Alchemy Websocket: http://alchemywebsockets/
- WebSocketListener : https://github./vtortola/WebSocketListener
Reason could be any of these:
- Having IIS version < 8
- Not enabled Web Socket Protocol under windows features
- Having .NET Framework version < 4.5
- Not included the following lines in web.config's
system.webServer
section:
<handlers>
<add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/>
</handlers>