看WEBRTC教程時使用到WebSocket來傳輸信令,node端使用了ws庫來實現,但在瀏覽器端http沒法獲取本地媒體,必須使用https,使用https後webSocket 不能使用ws協議了,必須使用wss協議。node
網上看到的教程裏使用的SSL證書都是適用於nginx下的兩個證書,但我使用時老是碰到問題,webSocket鏈接時都發生段錯誤,因此我使用了不一樣的證書:適用於IIS的兩個證書:youtdomain.pfx
keystorePass.txt
。nginx
// 需安裝ws模塊 npm install ws let WebSocketServer = require('ws').Server; let https = require("https"); let fs = require("fs"); let pfxpath = __dirname + '/test.com.pfx'; // let passpath = __dirname + '/testkey.txt'; let options = { pfx: fs.readFileSync(pfxpath), passphrase: fs.readFileSync(passpath), }; let server = https.createServer(options, (req, res) => { res.writeHead(200); res.end("this is a websocket server \n"); }).listen(8888); let wss = new WebSocketServer({ server: server }); wss.on( "connection", connection => { console.log("has user to connected"); } );
運行wss的服務器必須是SSL證書域名解析到的服務器。不然會出現錯誤,當本地測試時可使用ws協議。web