在此example中,websocket连接被打开到外部服务器。假设“外部”服务器只是https.createServer
,其中https
是必需的软件包,那么该“外部”服务器到底有多精确?
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const server = new https.createServer({
cert: fs.readFileSync('/path/to/cert.pem'),
key: fs.readFileSync('/path/to/key.pem')
});
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
server.listen(8080);
基于外部服务器的实现会是什么样子?例如,如果供应商X声称Websockets的功能强大,那么我该如何将ws
包挂接到这样的服务器上?