집 >백엔드 개발 >C#.Net 튜토리얼 >WebSocket 배포 서버가 외부 네트워크에 연결되지 않는 문제 해결
먼저 말씀드리고 싶은 것은 제가 직면한 문제입니다:
<span class="typ">WebSocket<span class="pln"> <span class="str">'에 대한 연결 ws:/ /www.xxxx.com/xxx/xx'<span class="pln"> 실패<span class="pun">:<span class="pln"> <span class="typ">오류WebSocket<span class="pln"> 핸드셰이크 중 <span class="pun">:<span class="pln"> <span class="typ">예기치 않은<span class="pln">응답 코드<span class="pun">:<span class="pln"> <span class="lit">200<br>웹사이트가 서버에 바인딩되어 있으며 webSocket 서비스는 가능하지만 외부 네트워크를 사용하여 액세스할 때 이 오류가 가장 큰 적입니다. 이전에 몇 가지 작은 도둑을 만났기 때문에 더 이상 쉽게 해결할 수 없습니다. 상사와 직접 대면<br> <br>이틀 동안 해결책을 찾았지만 찾을 수 없었습니다. 다양한 해결책이 있지만 .NET을 사용하여 이 문제를 직접 설명하는 사람은 많지 않습니다. 나는이 문제에 직면했습니다. 친구들이 많이 있습니다. <br>브라우저 문제와는 관련이 없습니다. Firefox, Google, IE에서 테스트해 보았는데 예상했던 문제가 여전히 발생합니다. <br>개발 플랫폼: .NET, ASP.NET MVC를 사용하여 웹사이트 구축<code><span class="typ">WebSocket<span class="pln"> connection to <span class="str">'ws://www.xxxx.com/xxx/xx'<span class="pln"> failed<span class="pun">:<span class="pln"> <span class="typ">Error<span class="pln"> during <span class="typ">WebSocket<span class="pln"> handshake<span class="pun">:<span class="pln"> <span class="typ">Unexpected<span class="pln"> response code<span class="pun">:<span class="pln"> <span class="lit">200<br>网站绑定到了服务器,直接在服务器上访问webSocket服务是可行的,但是当我用外网访问时,这个错误就是我最大的敌人,在这之前还遇到过几个小毛贼,可以轻松解决,不再提,直面boss吧<br><br>花费两个下午的时间寻找解决方案,但是并没有找到,各种解决方案,但是直接来描述.NET下使用这一问题的,却是不多,然而面对了这一问题的朋友,大把的有。<br>这个无关浏览器问题,我有火狐,谷歌,IE测试过,该出的问题还是会有。<br>开发平台:.NET,利用ASP.NET MVC搭建网站</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit">,在其中利用WebSocket服务,目的是在网站内设计一个讨论平台。<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"><span style="font-size: 15px"><code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"><span style="font-size: 15px"><code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"> 具体来说一说:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class=" typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pun"><span class="pln"><span class="lit">. <br>🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜🎜🎜<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit">🎜<code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln "><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"> <span class="lit"> 🎜🎜🎜🎜🎜🎜🎜🎜</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜🎜🎜<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span><span class="lit">🎜<code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span><span class="pln"><span class="typ "><span class="pln"><span class="pun"><span class="pln"><span class=" lit">구체적으로 말하면: 🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜 🎜🎜🎜🎜🎜🎜🎜</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜 🎜🎜🎜🎜🎜🎜🎜 🎜🎜<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class=" pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"><code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"><span style="font-size: 15px"><code><span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit">首先,看下我的网页代码</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit">:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class=" typ"><span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pun"><span class="pln"><span class="lit"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
코드>
<span class="typ"><span class="pln"><span class=" str"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="lit">首先,看下我的网页代码<p><br></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜<span class="pln"><span class="str"><span class="pln"><span class="pun"><span class="pln"><span><span class="pln"><span class="typ"><span class="pln"><span class="pun"><span class="pln"><span class=" typ"><span class="pln"><span class="pun"><span class="pln"><span class="lit">:🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜 🎜🎜🎜</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
🎜🎜🎜🎜🎜<script type="text/javascript"> $(function () { var url = "ws://xxxx:xx"+"@Url.Action("RequestProcessCenter")"; var ws; function connect() { ws = new WebSocket(url); $("#TopicContent").append("正在连接\n"); ws.onopen = function () { $("#TopicContent").append("已经连接\n"); }; ws.onmessage = function (evt) { console.log(evt.data); $("#TopicContent").append(evt.data); }; ws.onclose = function () { $("#TopicContent").append("已经关闭\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("连接已经关闭"); } }); $("#open").click(function () { connect(); }); $("#close").click(function () { ws.close(); }); }); </script>🎜后台代码:🎜🎜🎜🎜🎜
public void RequestProcessCenter() { if (HttpContext.IsWebSocketRequest) { var currUser = GetCurrUser(); this._UserName = currUser.LoginName; HttpContext.AcceptWebSocketRequest(ProcessTopic); } else { HttpContext.Response.Write("请求失败哟"); } }🎜webSocket所要求的url格式我想应该是不会配错。🎜🎜错误显示如图:🎜🎜🎜🎜 ㅋㅋ是websocket, 错误却存가 了, 这是为什么呢?查询了很多资料, 但是并没有发现真正解决这一问题的, 或许是我找的方式有问题,但是百島前三页我是看了,并没有想要的解决方案。自己动手,做了个测试,反而发现有效。🎜🎜 나는 현재 80%를 사용하고 있으며 webSocket을 사용하지 않으며 80%를 사용하고 있습니다. 🎜 1.本地80端仑绑定域name webSocket 사용域name ,端口80 无效🎜 2.本地80端口绑定域name webSocket利用2017端口 主机地址用服务器ip地址 有效🎜 3.本地2017端口 未绑定域name webSocket利用8 0端구 无效🎜 4.本地2017端구 未绑定域name webSocket利用 2017端口 有效🎜🎜总结 80端口可能受系统限 같았어요🎜另一方face:域name绑定是否会影响 没有测试🎜🎜 🎜🎜成功解决了外网虽返回200却不能使는 websocket을 사용하지 않습니다.🎜🎜这种情况下需要注의미있는是,IIS服务器上需要添加两个网站了,一个用于正常的80端网页访问,另一个做单独的WebSocket功能,这两个网站可以使用同一个程序版本,我就是这么做的,没有什么好的想法,菜鸟级人想。🎜🎜 这个网站介绍了WebSocket的一些知识,以及websocket默认使用80-433端口,或许我想, 나는 웹소켓을 사용하는 것을 거부합니다.但是感这个解决方案不是很好的,只能是说满足了需求,却不能简化需求过程。🎜🎜 🎜🎜留下一个猜想,我在服务器访问时,是可行的,tour是使用80端口,websocket服务能够正常使用,那是因为에서 服务器上发起时,直接访问的就是我本地服务,所以说防fire墙不会阻止,但是我使时,入网规则或许阻挡了我请求,点击连接,发起调 용 WebSocket 服务 , 而服务端 也 使 用 80端 , 引起防 화 墙识别 出问题。🎜🎜 🎜
위 내용은 WebSocket 배포 서버가 외부 네트워크에 연결되지 않는 문제 해결의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!