Add websocket demo to template

master
Garrett Mills 2 years ago
parent e2fe75ebf2
commit 2209ff685f

@ -9,7 +9,7 @@
},
"dependencies": {
"@atao60/fse-cli": "^0.1.7",
"@extollo/lib": "link:../lib",
"@extollo/lib": "^0.11.0",
"copyfiles": "^2.4.1",
"feed": "^4.2.2",
"gotify": "^1.1.0",

File diff suppressed because it is too large Load Diff

@ -17,6 +17,7 @@ import {
Queueables,
Redis,
Bus,
WebsocketServer,
} from '@extollo/lib'
import {AppUnit} from './app/AppUnit'
@ -43,4 +44,5 @@ export const Units = [
Routing,
Discovery,
HTTPServer,
WebsocketServer,
] as (typeof Unit)[]

@ -1,4 +1,4 @@
import {Controller, view, Session, Inject, Injectable, Locale, Validator} from '@extollo/lib'
import {Controller, view, Session, Inject, Injectable, Locale, Validator, WebSocketBus, JSONMessageEvent, Logging, Bus, WebSocketCloseEvent} from '@extollo/lib'
import {UserLogin} from "../../../types/UserLogin";
@Injectable()
@ -9,6 +9,9 @@ export class Home extends Controller {
@Inject()
protected readonly locale!: Locale;
@Inject()
protected readonly logging!: Logging
public welcome() {
this.session.set('app_visits', this.session.get('app_visits', 0) + 1)
@ -20,6 +23,22 @@ export class Home extends Controller {
})
}
public async socket(ws: WebSocketBus): Promise<void> {
ws.subscribe(JSONMessageEvent, event => {
this.logging.info('Got JSON message event:')
this.logging.debug(event.value)
})
const interval = setInterval(() => {
ws.push(new JSONMessageEvent({ hello: 'world!' }))
}, 5000)
await this.request.make<Bus>(Bus)
.subscribe(WebSocketCloseEvent, () => {
clearInterval(interval)
})
}
public test() {
return new Validator<UserLogin>()
}

@ -26,3 +26,6 @@ Route.group('', () => {
.pre(scope('tok'))
.handledBy(() => 'Token!')
}).pre(TokenAuthMiddleware)
Route.socket('/ws/hello')
.calls<Home>(Home, home => home.socket)

@ -22,12 +22,7 @@ export function cli(): Application {
const app = Application.getApplication()
app.forceStartupMessage = false
const units = [...Units]
units.reverse()
CommandLineApplication.setReplacement(units[0])
units[0] = CommandLineApplication
units.reverse()
const units = [...Units, CommandLineApplication]
app.scaffold(__dirname, units)
return app

Loading…
Cancel
Save