The first thing to say is the question I met:
WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpected response code: 200
网站绑定到了服务器,直接在服务器上访问webSocket服务是可行的,但是当我用外网访问时,这个错误就是我最大的敌人,在这之前还遇到过几个小毛贼,可以轻松解决,不再提,直面boss吧
花费两个下午的时间寻找解决方案,但是并没有找到,各种解决方案,但是直接来描述.NET下使用这一问题的,却是不多,然而面对了这一问题的朋友,大把的有。
这个无关浏览器问题,我有火狐,谷歌,IE测试过,该出的问题还是会有。
开发平台:.NET,利用ASP.NET MVC搭建网站
/span>
/span>
specifically say:
/span>
首先,看下我的网页代码
:
<script type= "Text/javascript" > $ (function () {var url = "Ws://xxxx:xx" + "@Url. Action (" Req Uestprocesscenter ")"; var ws; function connect () {ws = new WebSocket (URL); $ ("#TopicContent"). Append ("Connecting \ n"); Ws.onopen = function () {$ ("#TopicContent"). Append ("Already connected \ n"); }; Ws.onmessage = function (evt) {console.log (evt.data); $ ("#TopicContent"). Append (Evt.data); }; Ws.onclose = function () {$ ("#TopicContent"). Append ("already closed \ n"); }; Ws.onerror = function (evt) {console.log (evt.data); $ ("#TopicContent"). Append (Evt.data); }; }; $ ("#send"). Click (function () {console.log (1); Console.log (ws.readystate); Console.log (Websocket.open); if (ws.readystate = = Websocket.open) {ws.send ($ ("#content"). Val ()); $ (this). Val (""); } else {$ ("#TextMessge"). Append ("Connection closed"); } }); $ ("#open"). Click (function () {connect (); }); $ ("#close"). Click (function () {ws.close (); }); }); </script>
Background code:
public void Requestprocesscenter () { if (httpcontext.iswebsocketrequest) { var curruser = Getcurruser (); This._username = Curruser.loginname; Httpcontext.acceptwebsocketrequest (processtopic); } else { HttpContext.Response.Write ("request failed Yo"); } }
The URL format required by WebSocket I think it's not the right match.
Error display
Returns 200, despite the success of the request, but the result is not what we want.
The request way is also really websocket, the error exists, this is why? Query a lot of information, but did not find a real solution to this problem, perhaps I find the way there is a problem, but the first three pages of Baidu I saw, and did not want the solution. Self-made, did a test, but found effective.
I deployed on the server site, using the default 80 port, Access will expire, if the websocket does not have the same port as the site binding?
After this test:
1. Local 80-Port bound domain name websocket using domain name, port 80 is invalid
2. Local 80-Port bound domain name WebSocket using the 2017 port host address with the server IP address valid
3. Local 2017 port unbound domain name WebSocket using 80 port is invalid
4. Local 2017-Port unbound domain name websocket with 2017 port valid
Summary 80 ports may be subject to system limitations
On the other hand: does domain binding affect no test
Successfully solved the problem that the external network returned 200 but could not use the WebSocket service.
In this case, it should be noted that the IIS server needs to add two sites, one for the normal 80 port Web Access, the other to do a separate websocket function, the two sites can use the same program version, I do so, there is no good idea, rookie-level characters.
This site introduces some knowledge of WebSocket, and websocket default to use 80-433 port, perhaps I am thinking, is not my site bound port and WebSocket binding the same port, the external network request is at this time out of the wrong, this problem also 1:30 will not be able to delve into, Temporary functionality is achieved, but the feeling that the solution is not very good, can only be said to meet the needs, but can not simplify the requirements process.
Leave a conjecture, when I visit on the server, is feasible, all use 80 port, WebSocket service is able to use normally, that is because when the server is initiated, the direct access is my local service, so the firewall does not block, but when I use the extranet access, the network rules may block my request , click Connect, initiate a call to the WebSocket service, and the service port uses port 80, causing the firewall to recognize the problem.