WebSocket node.js服务器
const Koa = require('koa') // 路由 const route = require('koa-route') const websockify = require('koa-websocket') const app = websockify(new Koa()); let ctxs = []; app.ws. use(async (ctx, next) => { ctxs.push(ctx) return next(ctx) }) .use(route.all('/echo', async (ctx) => { ctx.websocket.on('message', (message) => { console.log(`客户端数据:${message}`) for (let i = 0; i < ctxs.length; i++) { if(ctx == ctxs[i]) continue ctxs[i].websocket.send(message) } }) })) app.listen(3000, "127.0.0.1") 来源: oschina 链接: https://my.oschina.net/wwyywg/blog/4428556