简体   繁体   中英

How do I get HTTPs w/ Express working with websockets on the same port using node?

I have the following code that works...

const app = express();
const server = http.createServer(app);
server.on('error', (err) => console.error(err));
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
    ...
})

This works great so I try to convert to https...

const app = express();
const server = https.createServer({
    key: fs.readFileSync(process.env.KEY_FILE),
    cert: fs.readFileSync(process.env.CERT_FILE)
}, app);
server.on('error', (err) => console.error(err));
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
    ...
})

I can now connect via my express routes but not my websocket. What am I missing?

糟糕,使用https可以使wss://安全,因此您需要使用wss://地址而不是ws://

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM