This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(koa): Add websocket support via WebsocketHook
- Loading branch information
1 parent
85832b2
commit 34b51eb
Showing
7 changed files
with
308 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import Server from './server' | ||
import ContextHook from './context' | ||
import WebsocketHook from './websocket' | ||
|
||
export { | ||
Server, | ||
ContextHook, | ||
WebsocketHook, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Hook from '@atlas.js/hook' | ||
import websocket from 'koa-websocket' | ||
import middleware from './middleware' | ||
|
||
class WebsocketHook extends Hook { | ||
static requires = [ | ||
'service:koa', | ||
] | ||
|
||
static defaults = { | ||
middleware: { | ||
module: 'websocket/middleware', | ||
config: {}, | ||
}, | ||
listen: null, | ||
} | ||
|
||
afterPrepare() { | ||
const koa = this.component('service:koa') | ||
const config = this.config | ||
|
||
websocket(koa) | ||
this.log.debug('websocket:attach') | ||
|
||
// Apply websocket middleware | ||
if (config.middleware) { | ||
middleware(koa.ws, this.atlas.require(config.middleware.module), config.middleware.config) | ||
} | ||
} | ||
|
||
afterStart() { | ||
const koa = this.component('service:koa') | ||
|
||
koa.ws.listen({ | ||
...this.config.listen, | ||
server: koa.server, | ||
}) | ||
|
||
this.log.info({ addrinfo: koa.server.address() }, 'listening') | ||
} | ||
|
||
async beforeStop() { | ||
this.log.info('websocket:close') | ||
|
||
await new Promise((resolve, reject) => | ||
this.component('service:koa').ws.server.close(err => | ||
err ? reject(err) : resolve())) | ||
} | ||
} | ||
|
||
export default WebsocketHook |
Oops, something went wrong.