c# - Cannot invoke websocket from ASP.NET Client Side -
i have created asp.net web form. wanna invoke websocket called myhandler.ashx web form's client side clickng javascript button called "run websocket". myhandler.ashx in project called "myhandlerproject". here codes of web form's client side:
<%@ page language="c#" autoeventwireup="true" codebehind="mywebform.aspx.cs" inherits="mywebformtestingwebsocket.mywebform" %> <!doctype html> <html> <head> <script type="text/javascript"> function websockettest() { if ("websocket" in window) { alert("websocket supported browser!"); // let open web socket var ws = new websocket("ws://localhost:2281/myhandler"); ws.onopen = function() { // web socket connected, send data using send() ws.send("message send"); alert("message sent..."); }; ws.onmessage = function (evt) { var received_msg = evt.data; alert("message received..."); }; ws.onclose = function() { // websocket closed. alert("connection closed..."); }; } else { // browser doesn't support websocket alert("websocket not supported browser!"); } } </script>
<div id="sse"> <a href="javascript:websockettest()">run websocket</a> </div>
here codes in myhandler.ashx:
public void processrequest(httpcontext context) { if (context.iswebsocketrequest) { context.acceptwebsocketrequest(new clsmywebsocketclass()); } } myhandler processrequest method call clsmywebsocketclass class.
here codes of clsmywebsocketclass:
using microsoft.web.websockets; namespace myhandlerproject { public class clsmywebsocketclass : websockethandler { private static websocketcollection clients = new websocketcollection(); public override void onopen() { } public override void onmessage(string message) { clients.broadcast("hello web socket server working"); } } }
the problem after clicking "run websocket" button webform's client side, websocket "myhandler" not invoked. btw run myhandler.ashx in browser ensure webform client side can access url ws://localhost:2281/myhandler. url correct? can please tell me wrong code? thank :)
Comments
Post a Comment