Anuj-Panthri commited on
Commit
58fc009
·
1 Parent(s): 95427c6

added get_pong route in server

Browse files
server/README.md CHANGED
@@ -28,6 +28,27 @@ Types can be `client -> server`, `server -> client <private | room>`.
28
  Private messages are sent directly to the client.
29
  Room messages are sent to everyone in a room.
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  ## Get User Info
32
 
33
  **Type:** `client -> server`
@@ -96,7 +117,7 @@ Room messages are sent to everyone in a room.
96
 
97
  ```typescript
98
  {
99
- "route": "leave_room_response",
100
  "status": string,
101
  }
102
  ```
 
28
  Private messages are sent directly to the client.
29
  Room messages are sent to everyone in a room.
30
 
31
+ ## Get Pong
32
+
33
+ **Type:** `client -> server`
34
+ **Description:** Used to get pong message from server when requested. This can be used to detect when a client is disconnected from the server.
35
+ **Payload:**
36
+
37
+ ```typescript
38
+ {
39
+ "route": "get_pong",
40
+ }
41
+ ```
42
+
43
+ **Type:** `server -> client <private>`
44
+ **Payload:**
45
+
46
+ ```typescript
47
+ {
48
+ "message": "pong",
49
+ }
50
+ ```
51
+
52
  ## Get User Info
53
 
54
  **Type:** `client -> server`
 
117
 
118
  ```typescript
119
  {
120
+ "message": "leave_room_response",
121
  "status": string,
122
  }
123
  ```
server/src/index.ts CHANGED
@@ -86,6 +86,12 @@ function main(logging: boolean = true) {
86
  logger.info("received pong");
87
  });
88
 
 
 
 
 
 
 
89
  // When a user disconnects, their account is removed, and they are removed from all groups
90
  ws.on("close", () => {
91
  hapticLink.removeUser(wsw);
 
86
  logger.info("received pong");
87
  });
88
 
89
+ // Added by Anuj
90
+ // When receive ping from a client this is triggered
91
+ ws.on('ping', () => {
92
+ logger.info("received ping");
93
+ });
94
+
95
  // When a user disconnects, their account is removed, and they are removed from all groups
96
  ws.on("close", () => {
97
  hapticLink.removeUser(wsw);
server/src/socket/routes.ts CHANGED
@@ -5,13 +5,14 @@ import { LeaveRoomHandler, LeaveRoomSchema } from "./routes/leave_room";
5
  import { SendVibrationHandler, SendVibrationSchema } from "./routes/send_touch";
6
  import { SetUsernameHandler, SetUsernameSchema } from "./routes/set_username";
7
  import { TestConnnectionSchema, TestConnectionHandler } from "./routes/test_connection";
8
-
9
  /**
10
  * Registers all the routes for Haptic Link
11
  * @param {HapticLinkServer} router
12
  */
13
  export function registerRoutes(router: HapticLinkServer) {
14
  router.addRoute("test_connection", TestConnnectionSchema, TestConnectionHandler);
 
15
  router.addRoute("join_room", JoinRoomSchema, JoinRoomHandler);
16
  router.addRoute("leave_room", LeaveRoomSchema, LeaveRoomHandler);
17
  router.addRoute("send_touch", SendVibrationSchema, SendVibrationHandler);
 
5
  import { SendVibrationHandler, SendVibrationSchema } from "./routes/send_touch";
6
  import { SetUsernameHandler, SetUsernameSchema } from "./routes/set_username";
7
  import { TestConnnectionSchema, TestConnectionHandler } from "./routes/test_connection";
8
+ import { GetPongSchema, GetPongHandler } from "./routes/get_pong";
9
  /**
10
  * Registers all the routes for Haptic Link
11
  * @param {HapticLinkServer} router
12
  */
13
  export function registerRoutes(router: HapticLinkServer) {
14
  router.addRoute("test_connection", TestConnnectionSchema, TestConnectionHandler);
15
+ router.addRoute("get_pong",GetPongSchema,GetPongHandler);
16
  router.addRoute("join_room", JoinRoomSchema, JoinRoomHandler);
17
  router.addRoute("leave_room", LeaveRoomSchema, LeaveRoomHandler);
18
  router.addRoute("send_touch", SendVibrationSchema, SendVibrationHandler);
server/src/socket/routes/get_pong.ts ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { z } from "zod";
2
+ import { Context } from "../hapticLinkServer";
3
+
4
+ export interface GetPongPayload { }
5
+ export const GetPongSchema = z.object({ });
6
+
7
+ export function GetPongHandler(ctx: Context<GetPongPayload>) {
8
+ return ctx.ws.send(
9
+ JSON.stringify({
10
+ message: "pong",
11
+ })
12
+ );
13
+ }