Daily Archives: March 10, 2021

WebSocket node.js ws 註解整理 – 1

這裡 註解 整理在Node.js中,WebSocket client/server常用的實現 ws 套件的程式碼 https://github.com/websockets/ws 關於WebSocket protocol,可參考另一篇文章的整理 index.js 所有的implementation在lib資料夾,大約3600行,算是一個輕量的實現,可先從index.js export的WebSocket開始追起,這也是一般js WebSocket client使用的class,WebSocket client API在實現上也是參考 browser使用的API,所以https://developer.mozilla.org/en-US/docs/Web/API/WebSocket 也要一併參考 以下逐行整理註解lib/websocket.js lib/websocket.js 上面可以看到支援的兩個version 8、13 8主要是對應https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08 開始的版本到 -12 ,13主要是對應 https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-13 開始的版本,不過實際上支援的程度或相容性仍要看實作細節 繼承 EventEmitter,可參考 Nodejs Events整理的說明 constructor的部分: 接下來從constructor內的initAsClient往下追 以下設置http req,透過http.get function,可參考: https://nodejs.org/docs/latest-v12.x/api/http.html#http_http_request_options_callback 上面的error event … Continue reading

Posted in nodejs | Leave a comment