support using ws
in phecda-server
npm i phecda-server-ws
create a controller to expose
import { Arg, Ctx } from 'phecda-server'
import { On, Ws, type WsContext } from 'phecda-server-ws'
@Ws()
export class TestWs {
@Ctx
context: WsContext
@On
add(@Arg data: { name: string }) {
this.context.broadcast('test', data)
}
}
bind ws and start server
import { WSGenerator, bind } from 'phecda-server-ws'
import { Factory } from 'phecda-server'
import { WebSocketServer } from 'ws'
import { TestWs } from './test.ws'
const data = await Factory([TestWs], {
generators: [new WSGenerator()],
})
const server = new WebSocketServer({ port: 3001 })
bind(server, data)
if you use ps
runtime , update the config file(ps.json
)
{
"resolve": [
{
"source": "ws",
"importer": "client",
"path": ".ps/ws.js"
}
],
"moduleFile": ["ws"]
}