首先要說的是我遇見的問題:
<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"><br></span></span>,在其中利用WebSocket服務,目的是在網站內設計一個討論平台。 </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="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>
<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格式我想應該是不會配錯。 ######錯誤顯示如圖:############### 回傳200,儘管是請求成功了,但是結果不是我們想要的。 ############### #########請求方式也確實是websocket,錯誤卻存在了,這是為什麼呢?查詢了很多資料,但是並沒有發現真正解決這一問題的,或許是我找的方式有問題,但是百度前三頁我是看了,並沒有想要的解決方案。自己動手,做了測試,反而發現有效。 ######我在伺服器上部署的網站,用的是預設的80端口,訪問會失效,如果webSocket不用和網站綁定的同一端口呢? ######經過此測試:###### 1.本地80埠綁定網域webSocket利用域名,埠80 無效### 2.本地80埠綁定網域webSocket利用2017埠主機位址用伺服器ip位址有效### 3.本地2017埠未綁定網域名稱webSocket利用80埠無效### 4.本地2017埠未綁定網域名稱webSocket利用2017埠有效######總結80埠可能受系統限制###另一方面:網域綁定是否會影響沒有測試###### ######成功解決了外網雖返回200卻不能使用websocket服務的問題。 ######這種情況下需要注意的是,IIS伺服器上需要添加兩個網站了,一個用於正常的80端口網頁訪問,另一個做單獨的WebSocket功能,這兩個網站可以使用同一個程式版本,我就是這麼做的,沒有什麼好的想法,菜鳥級人物。 ###### 這個網站介紹了WebSocket的一些知識,以及websocket默認使用80-433端口,或許我在想,是不是我網站綁定的端口和websocket綁定相同端口時,外網請求就是此時出的錯呢,這個問題也一時半會不能深究,暫時性的功能是達到了,但是感覺這個解決方案不是很好的,只能是說滿足了需求,卻不能簡化需求過程。 ###### ######留下一個猜想,我在伺服器上訪問時,是可行的,都是使用80端口,websocket服務能夠正常使用,那是因為在伺服器上發起時,直接訪問的就是我本地服務,所以說防火牆不會阻止,但是我使用外網訪問時,入網規則或許阻擋了我的請求,點擊連接,發起調用WebSocket服務,而服務端口也使用80端口,引起防火牆識別出問題。 ###### ###
以上是關於WebSocket部署伺服器外網無法連線的解決方案的詳細內容。更多資訊請關注PHP中文網其他相關文章!