亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Websocket服務器:永遠不會調用Web套接字上的onopen函數

Websocket服務器:永遠不會調用Web套接字上的onopen函數

C#
狐的傳說 2019-11-06 10:01:38
我正在嘗試實現C#Web套接字服務器,但它給我帶來了一些麻煩。我正在運行Web服務器(ASP.NET),以使用javascript托管頁面,并且Web套接字服務器已實現為C??刂婆_應用程序。我能夠檢測到來自客戶端的連接嘗試(運行javascript的Chrome瀏覽器),還可以從客戶端檢索握手。但是客戶端似乎不接受我發回的握手信號(onopen從不調用Web套接字上的函數)。我一直在閱讀Web套接字協議,但看不到我在做什么錯。以下是服務器代碼:Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8181);listener.Bind(ep);listener.Listen(100);Console.WriteLine("Wainting for connection...");Socket socketForClient = listener.Accept();if (socketForClient.Connected){    Console.WriteLine("Client connected");    NetworkStream networkStream = new NetworkStream(socketForClient);    System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);    System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);    //read handshake from client:    Console.WriteLine("HANDSHAKING...");    char[] shake = new char[255];    streamReader.Read(shake, 0, 255);    string handshake =       "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +       "Upgrade: WebSocket\r\n" +       "Connection: Upgrade\r\n" +       "WebSocket-Origin: http://localhost:8080\r\n" +       "WebSocket-Location: ws://localhost:8181\r\n" +       "\r\n";    streamWriter.Write(handshake);    streamWriter.Flush();我正在本地主機上運行端口8080上的Web服務器和端口8181上的Web套接字服務器。我嘗試過以不同的編碼(ASCII,字節和十六進制)發送握手,但這似乎沒有什么區別。連接永遠不會完全建立。javascript看起來像這樣:var ws;var host = 'ws://localhost:8181';debug("Connecting to " + host + " ...");try { ws = new WebSocket(host);} catch (err) { debug(err, 'error');}ws.onopen = function () { debug("connected...", 'success');};ws.onclose = function () { debug("Socket closed!", 'error');};ws.onmessage = function (evt) { debug('response: ' + evt, 'response');};我猜測錯誤是C#服務器出現的,因為chrome正在按其應有的方式發送信息,但是正如所說的那樣onopen,從未調用過該函數。簡而言之,我的問題是: 你們中的任何人都做到過嗎?如果是,您是如何做到的?原因:在代碼中是否看到任何明顯的錯誤(希望不要問太多)
查看完整描述

3 回答

  • 3 回答
  • 0 關注
  • 1779 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號