在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mattstyles/koa-socket开源软件地址:https://github.com/mattstyles/koa-socket开源编程语言:JavaScript 91.7%开源软件介绍:Koa-socket
Koa-socket is now compatible with koa v2 style of middleware (where context is passed as a parameter), v0.4.0 of koa-socket is the last version to support the old style of middleware. As such, koa-socket now requires node v4.0.0 or higher although koa-socket simply attaches to the server instance so will be compatible with a koa v1 powered app. Installationnpm i -S koa-socket Exampleconst Koa = require( 'koa' )
const IO = require( 'koa-socket' )
const app = new Koa()
const io = new IO()
app.use( ... )
io.attach( app )
io.on( 'join', ( ctx, data ) => {
console.log( 'join event fired', data )
})
app.listen( process.env.PORT || 3000 ) Features
Attaching to existing projectsThe It also re-maps const Koa = require( 'koa' )
const IO = require( 'koa-socket' )
const app = new Koa()
const io = new IO()
// Attach the socket to the application
io.attach( app )
// Socket is now available as app.io if you prefer
app.io.on( event, eventHandler )
// The raw socket.io instance is attached as app._io if you need it
app._io.on( 'connection', sock => {
// ...
})
// app.listen is mapped to app.server.listen, so you can just do:
app.listen( process.env.PORT || 3000 )
// *If* you had manually attached an `app.server` yourself, you should do:
app.server.listen( process.env.PORT || 3000 ) Middleware and event handlersMiddleware can be added in much the same way as it can be added to any regular koa instance. Example with async functions (transpilation required)io.use( async ( ctx, next ) => {
let start = new Date()
await next()
console.log( `response time: ${ new Date() - start }ms` )
}) There is an example in the Example with generator functionsKoa v2 no longer supports generators so if you are using v2 then you must use const Koa = require( 'koa' )
const IO = require( 'koa-socket' )
const co = require( 'co' )
const app = new Koa()
const io = new IO()
app.use( ... )
io.use( co.wrap( function *( ctx, next ) {
let start = new Date()
yield next()
console.log( `response time: ${ new Date() - start }ms` )
}))
io.use( ... );
io.on( 'message', ( ctx, data ) => {
console.log( `message: ${ data }` )
})
io.attach( app )
app.listen( 3000 ); Plain exampleWhilst slightly unwieldy, the standalone method also works io.use( ( ctx, next ) => {
let start = new Date()
return next().then( () => {
console.log( `response time: ${ new Date() - start }ms` )
})
}) Passed Contextlet ctx = {
event: listener.event,
data: data,
socket: Socket,
acknowledge: cb
} The context passed to each socket middleware and handler begins the chain with the event that triggered the response, the data sent with that event and the socket instance that is handling the event. There is also a shorthand for firing an acknowledgement back to the client. As the context is passed to each function in the response chain it is fair game for mutation at any point along that chain, it is up to you to decide whether this is an anti-pattern or not. There was much discussion around this topic for koa v2. io.use( async ( ctx, next ) => {
ctx.process = process.pid
await next()
})
io.use( async ( ctx, next ) => {
// ctx is passed along so ctx.process is now available
console.log( ctx.process )
})
io.on( 'event', ( ctx, data ) => {
// ctx is passed all the way through to the end point
console.log( ctx.process )
}) NamespacesNamespaces can be defined simply by instantiating a new instance of const app = new Koa()
const chat = new IO({
namespace: 'chat'
})
chat.attach( app )
chat.on( 'message', ctx => {
console.log( ctx.data )
chat.broadcast( 'response', ... )
}) Namespaces also attach themselves to the const app = new Koa()
const chat = new IO({
namespace: 'chat'
})
chat.attach( app )
app.chat.use( ... )
app.chat.on( ... )
app.chat.broadcast( ... ) The attachment is configurable if you don’t want to muddy the const chat = new IO({
namespace: 'chat',
hidden: true
})
chat.use( ... )
chat.on( ... ) Namespaces are fairly ubiquitous so they get a dirty shorthand for creating them, note that if you want to add any additional options you’ll need to use the longhand object parameter to instantiate const chat = new IO( 'chat' ) API
.attach( |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论