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
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:
Reason could be any of these:
system.webServer
section: <handlers>
<add path="/ws.ashx" verb="*" name="ws" type="namespace.ws"/>
</handlers>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.