使用koa-websocket

使用这玩意,如果是前端请求的是ws连接,使用ip+端口,是可用的,

但是wss不行,怎么解决呢?

const fs = require('fs')

let options = {

  key: fs.readFileSync('./ssl/a.key', 'utf8'),

  cert: fs.readFileSync('./ssl/b.crt', 'utf8')

}

const Koa = require('koa')

const websockify = require('koa-websocket')

const app = websockify(new Koa(), {}, options)

然后,别用require('https')这个,

直接app.listen(443)即可,因为这个中间件已经弄好了相关配置(官方说的)

The WebSocket options object just get passed right through to the new WebSocketServer(options) call.

The optional HTTPS options object gets passed right into https.createServer(options). If the HTTPS options are passed in, koa-websocket will use the built-in Node HTTPS server to provide support for the wss:// protocol.

你可能感兴趣的:(使用koa-websocket)