在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:b3nsn0w/koa-easy-ws开源软件地址:https://github.com/b3nsn0w/koa-easy-ws开源编程语言:JavaScript 100.0%开源软件介绍:Simple, easy to use, composable middleware for websocket handling in Koa Usageconst Koa = require('koa')
const websocket = require('koa-easy-ws')
const app = new Koa()
app.use(websocket())
app.use(async (ctx, next) => {
// check if the current request is websocket
if (ctx.ws) {
const ws = await ctx.ws() // retrieve socket
// now you have a ws instance, you can use it as you see fit
return ws.send('hello there')
}
// we're back to regular old http here
ctx.body = 'general kenobi'
}) Note: you will also need to install the First, you need to pass the koa-easy-ws middleware before the one handling your request. Remember to call it as a function, The middleware adds the Features
Examples and advanced configurationYou can easily compose koa-easy-ws with a routing library: const Koa = require('koa')
const Router = require('koa-router')
const websocket = require('koa-easy-ws')
const app = new Koa()
const router = new Router()
app
.use(websocket())
.use(router.routes())
.use(router.allowedMethods())
router.get('/pow/obi', async (ctx, next) => {
if (ctx.ws) {
const ws = await ctx.ws()
ws.send('chancellor palpatine is evil')
}
})
router.get('/pow/ani', async (ctx, next) => {
if (ctx.ws) {
const ws = await ctx.ws()
ws.send('the jedi are evil')
ws.send('404')
}
}) If const Koa = require('koa')
const websocket = require('koa-easy-ws')
const app = new Koa()
const websocketMiddleware = websocket()
const websocketServer = websocketMiddleware.server // this is where the fun begins
app.use(websocketMiddleware) // we already have the instance here
// <insert rest of the app> This gives you access to the ws server object, allowing to pass down custom listeners, connection validators, etc. Alternatively, you can pass options to the underlying ws server as part of the options object: app.use(websocket('ws', {
wsOptions: {
clientTracking: false,
maxPayload: 69420
}
})) The In case const Koa = require('koa')
const websocket = require('koa-easy-ws')
const app = new Koa()
app.use(websocket('sidious')) // we just renamed ctx.ws to ctx.sidious
app.use(websocket('maul')) // attach another one for no good reason
app.use(async (ctx, next) => {
// the first middleware detected an upgrade request
if (ctx.sidious) {
const socket = await ctx.sidious()
return socket.send('this is getting out of hand')
}
// the second middleware detected the same upgrade request
if (ctx.maul) {
const socket = await ctx.maul()
return socket.send('now there are two of them')
}
}) Note: in this example If needed, you can also expose the websocket server on a context property, which can itself be renamed: const Koa = require('koa')
const websocket = require('koa-easy-ws')
const app = new Koa()
app.use(websocket('ws', { exposeServerOn: 'wss' }))
app.use(async (ctx, next) => {
if (ctx.ws) {
console.log('found the server', ctx.wss)
}
}) In the above example, From here, the sky is the limit, unless you work for SpaceX. Version 2 changelogIn version 2, On top of that, the peer dependency is for ws@8, not ws@7 which koa-easy-ws previously used, so refer to ws@8's breaking changes for that (but if you're fine with a warning ws@7 should continue to work). Special usage for Node 9 or earlierNode's HTTP server doesn't send upgrade requests through the normal callback (and thus your Koa middleware chain) prior to version 10, preventing koa-easy-ws from handling them. Because of this, if you target Node 9 or earlier, you must pass your HTTP server to the middleware which handles the workaround: const server = http.createServer(app.callback())
app.use(websocket('ws', server))
// alternatively, you can pass it as part of the options object:
app.use(websocket('ws2', {
server: server
}))
server.listen(process.env.PORT) // use this function instead of your app.listen() call koa-easy-ws then automatically feeds any upgrade request into your regular middleware chain. If you wish to opt out and do this yourself, use the app.use(websocket('ws', {
noServerWorkaround: true
})) ContributingPull requests are welcome. As always, be respectful towards each other and maybe run or create tests, as appropriate. It's on koa-easy-ws uses the MIT license. Was considering the WTFPL, but I like the "no warranty" clause. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论