Binding a function to event
Listening to events become simple with decorators
SugoiJS provide single line binding solution for binding a class method to socket event
Listening to socket events become simple with @SocketOn decorator
@SocketOn(event: string, namespace: string = "/", ...middlewares:Array<ISocketMiddleware>)
event
- The event name
namespace
- The related namespace which the socket should be related to.
middlewares
- 0...n arguments of middleware functions which should occur before the event callback function.Another signature of this decorator is:
@SocketOn(event: string, ...middlewares:Array<ISocketMiddleware>)
type ISocketMiddleware = (socket:SocketIOStatic.Socket,data:any)=>Promise<any>|any
This decorator registers the function as a callback for an event.
In case middlewares functions were passed, those methods will occur right before the callback function.
@SocketOn("message","/",(socket,data)=>{
console.log("log message data: %s from socket id: %s",data,socket.id);
})
function messageHandler(socket,data){
socket.to(data.room).emit('message',data.message)
}
You can bind one function to multiple events by use multiple
@SocketOn
decorators.Listening to Socket events become simple with @SocketServerOn decorator
@SocketServerOn(event: string, namespace: string = "/", ...middlewares:Array<ISocketMiddleware>)
event
- The event name
namespace
- The related namespace which the socket should be related to.
middlewares
- 0...n arguments of middleware functions which should occur before the event callback function.Another signature of this decorator is:
@SocketServerOn(event: string, ...middlewares:Array<ISocketMiddleware>)
type ISocketMiddleware = (socket:SocketIOStatic.Socket,data:any)=>Promise<any>|any
This decorator registers the function as a callback for an event.
In case middlewares functions were passed, those methods will occur right before the callback function.
@SocketSererOn("connection","/",(socket,data)=>{
console.log("log message data: %s from socket id: %s",data,socket.id);
})
function messageHandler(socket){
socket.emit('connected',{msg:"welcome"})
}
You can bind one function to multiple events by use multiple
@SocketServerOn
decorators.Last modified 4yr ago