Skip to content

Instantly share code, notes, and snippets.

@m-esm
Created November 9, 2018 08:14
Show Gist options
  • Save m-esm/6e81b846367345da6b4f93dbeb9c4b96 to your computer and use it in GitHub Desktop.
Save m-esm/6e81b846367345da6b4f93dbeb9c4b96 to your computer and use it in GitHub Desktop.
serendip WebSocket service usage example
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