Created
November 9, 2018 08:14
-
-
Save m-esm/6e81b846367345da6b4f93dbeb9c4b96 to your computer and use it in GitHub Desktop.
serendip WebSocket service usage example
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
import { | |
Server, | |
AuthService, | |
WebSocketService, | |
WebSocketInterface, | |
DbService | |
} from "serendip"; | |
export class DashboardService { | |
static dependencies = ["AuthService", "DbService", "WebSocketService"]; | |
private dbService: DbService; | |
private authService: AuthService; | |
private wsService: WebSocketService; | |
constructor() { | |
this.dbService = Server.services["DbService"]; | |
this.authService = Server.services["AuthService"]; | |
this.wsService = Server.services["WebSocketService"]; | |
} | |
async start() { | |
this.wsService.messageEmitter.on( | |
"/dashboard", | |
(input: string, ws: WebSocketInterface) => { | |
var msg: { command: "sync_grid"; data: any } = JSON.parse(input); | |
console.log(msg); | |
if (msg.command == "sync_grid") { | |
this.wsService.sendToUser( | |
ws.token.userId, | |
"/dashboard", | |
JSON.stringify({ command: "change_grid", data: msg.data }) | |
); | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment