.NET web socket returns error 200

This simple web socket example returns error 200.

Edit: I am retelling the code in C # in the hope that more people will be able to advise me why I have such a problem.

I run VS2012 Express, on my local IIS machine the project is configured for framework 4.5.1, and I imported the Microsoft.Websockets Nuget package.

The three code snippets that I included below are only three pieces of code in the project, and I did not make any changes to the rest of the project.

There is no gap before an unexpected error; it does not interrupt when opening or in a message from both sides. 200 appears as an error in the chrome console, but there is no preview of the response.

Here is the client (index.htm):

<!doctype html> <html> <head> <title></title> <script src="Scripts/jquery-1.8.1.js" type="text/javascript"></script> <script src="test.js" type="text/javascript"></script> </head> <body> <input id="txtMessage" /> <input id="cmdSend" type="button" value="Send" /> <input id="cmdLeave" type="button" value="Leave" /> <br /> <div id="chatMessages" /> </body> </html> 

and client script (test.js):

 $(document).ready(function () { var name = prompt('what is your name?:'); var url = 'ws://' + window.location.hostname + window.location.pathname.replace('index.htm', 'ws.ashx') + '?name=' + name; alert('Connecting to: ' + url); var ws = new WebSocket(url); ws.onopen = function () { $('#messages').prepend('Connected <br/>'); $('#cmdSend').click(function () { ws.send($('#txtMessage').val()); $('#txtMessage').val(''); }); }; ws.onmessage = function (e) { $('#chatMessages').prepend(e.data + '<br/>'); }; $('#cmdLeave').click(function () { ws.close(); }); ws.onclose = function () { $('#chatMessages').prepend('Closed <br/>'); }; ws.onerror = function (e) { $('#chatMessages').prepend('Oops something went wrong<br/>'); }; }); 

Here is the generic handler (ws.ashx):

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Web.WebSockets; namespace WebSockets { public class ws : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.IsWebSocketRequest) context.AcceptWebSocketRequest(new TestWebSocketHandler()); } public bool IsReusable { get { return false; } } } } 

Here is the class (TestWebSocketHandler):

 using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using Microsoft.Web.WebSockets; namespace WebSockets { 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)); } } } 
+4
source share
4 answers

There was no error as a result of this problem, but I found an error with which Chrome was unable to return with my 200 response.

I just had to enable the WebSocket protocol in Control Panel -> Programs -> Enable or Disable Windows -> IIS -> Application Development Features -> WebSocket Protocol.

At least I added a simple VB.NET WebSocket to the site.

Just make sure you have IIS 7+, and start Windows 8 in the 4.5 Framework ... and enable the WebSocket function above.

+2
source

Code 200 is not an error, it is the HTTP OK response you want. Check the answer using your browser, is there an answer in it? Try adding a breakpoint to your ws.onmessage to see if it fires.

0
source

just just check the web socket protocol form add or remove programs

go to control panel


then click add or remove programs


then Turn Windows features on or off


then IIS


then Application Development Features


then check the WebSocket protocol option


i tested it on a Microsoft Windows 2012 server

0
source

I found this on Wikipedia:

200 OK Standard response for successful HTTP requests. The actual answer will depend on the request method used. In the GET request, the response will contain an object corresponding to the requested resource. In the POST request, the response will contain an object that describes or contains the result of the action.

-3
source

All Articles