avallef commited on
Commit
9dffdbf
·
1 Parent(s): 7fb25b6

Changed addRoute method to be easier to understand

Browse files
server/src/socket/hapticLinkServer.ts CHANGED
@@ -50,12 +50,16 @@ export class HapticLinkServer {
50
  return true;
51
  }
52
 
53
- addRoute(route: Route<any>): boolean {
54
- if (route.name in this.routes) {
55
  return false
56
  }
57
 
58
- this.routes[route.name] = route;
 
 
 
 
59
 
60
  return true;
61
  }
 
50
  return true;
51
  }
52
 
53
+ addRoute<T>(name: string, schema: ZodSchema<T>, handler: RouteHandler<T>): boolean {
54
+ if (name in this.routes) {
55
  return false
56
  }
57
 
58
+ this.routes[name] = {
59
+ name,
60
+ schema,
61
+ handler
62
+ }
63
 
64
  return true;
65
  }
server/src/socket/routes.ts CHANGED
@@ -1,12 +1,13 @@
1
  import { HapticLinkServer } from "./hapticLinkServer";
2
- import JoinRoomRoute from "./routes/join_room";
3
- import LeaveRoomRoute from "./routes/leave_room";
4
- import SendVibrationRoute from "./routes/send_vibration";
5
- import TestConnectionRoute from "./routes/test_connection";
6
 
7
  export function registerRoutes(router: HapticLinkServer) {
8
- router.addRoute(TestConnectionRoute);
9
- router.addRoute(JoinRoomRoute);
10
- router.addRoute(LeaveRoomRoute);
11
- router.addRoute(SendVibrationRoute);
 
12
  }
 
1
  import { HapticLinkServer } from "./hapticLinkServer";
2
+ import { JoinRoomHandler, JoinRoomSchema } from "./routes/join_room";
3
+ import { LeaveRoomHandler, LeaveRoomSchema } from "./routes/leave_room";
4
+ import { SetUsernameHandler, SetUsernameSchema } from "./routes/set_username";
5
+ import { TestConnnectionSchema, TestConnectionHandler } from "./routes/test_connection";
6
 
7
  export function registerRoutes(router: HapticLinkServer) {
8
+ router.addRoute("test_connection", TestConnnectionSchema, TestConnectionHandler);
9
+ router.addRoute("join_room", JoinRoomSchema, JoinRoomHandler);
10
+ router.addRoute("leave_room", LeaveRoomSchema, LeaveRoomHandler);
11
+ router.addRoute("send_touch", LeaveRoomSchema, LeaveRoomHandler);
12
+ router.addRoute("set_username", SetUsernameSchema, SetUsernameHandler);
13
  }
server/src/socket/routes/join_room.ts CHANGED
@@ -3,42 +3,36 @@ import { Context, Route } from "../hapticLinkServer";
3
  import { generateSessionToken } from "../../helpers";
4
  import { Room } from "../room";
5
 
6
- interface JoinRoomPayload {
7
  roomId?: string;
8
  username?: string;
9
  }
10
- const JoinRoomSchema = z.object({
11
  roomId: z.string().optional(),
12
  username: z.string().optional(),
13
  })
14
 
15
- const JoinRoomRoute: Route<JoinRoomPayload> = {
16
- name: "join_room",
17
- handler: function(ctx: Context<JoinRoomPayload>) {
18
- // Set username if payload has it
19
- if (ctx.payload.username) {
20
- ctx.user.username = ctx.payload.username;
21
- }
22
 
23
- let room: Room;
24
 
25
- if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
26
- room = new Room(ctx.payload.roomId);
27
- ctx.server.rooms[room.id] = room;
28
- } else if (!ctx.payload.roomId) {
29
- ctx.payload.roomId = generateSessionToken(12);
30
- room = new Room(ctx.payload.roomId);
31
- ctx.server.rooms[room.id] = room;
32
- } else {
33
- room = ctx.server.rooms[ctx.payload.roomId]
34
- }
35
 
36
- ctx.user.currentRoom = room;
37
 
38
- // Broadcasts message to room
39
- room.addUser(ctx.user);
40
- },
41
- schema: JoinRoomSchema,
42
  }
43
-
44
- export default JoinRoomRoute;
 
3
  import { generateSessionToken } from "../../helpers";
4
  import { Room } from "../room";
5
 
6
+ export interface JoinRoomPayload {
7
  roomId?: string;
8
  username?: string;
9
  }
10
+ export const JoinRoomSchema = z.object({
11
  roomId: z.string().optional(),
12
  username: z.string().optional(),
13
  })
14
 
15
+ export function JoinRoomHandler(ctx: Context<JoinRoomPayload>) {
16
+ // Set username if payload has it
17
+ if (ctx.payload.username) {
18
+ ctx.user.username = ctx.payload.username;
19
+ }
 
 
20
 
21
+ let room: Room;
22
 
23
+ if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
24
+ room = new Room(ctx.payload.roomId);
25
+ ctx.server.rooms[room.id] = room;
26
+ } else if (!ctx.payload.roomId) {
27
+ ctx.payload.roomId = generateSessionToken(12);
28
+ room = new Room(ctx.payload.roomId);
29
+ ctx.server.rooms[room.id] = room;
30
+ } else {
31
+ room = ctx.server.rooms[ctx.payload.roomId]
32
+ }
33
 
34
+ ctx.user.currentRoom = room;
35
 
36
+ // Broadcasts message to room
37
+ room.addUser(ctx.user);
 
 
38
  }
 
 
server/src/socket/routes/leave_room.ts CHANGED
@@ -1,33 +1,27 @@
1
-
2
  import { z } from "zod";
3
  import { Context, Route } from "../hapticLinkServer";
4
 
5
- interface LeaveRoomPayload {
6
  roomId: string;
7
  }
8
- const LeaveRoomSchema = z.object({
 
9
  roomId: z.string(),
10
  })
11
 
12
- const LeaveRoomRoute: Route<LeaveRoomPayload> = {
13
- name: "leave_room",
14
- handler: function(ctx: Context<LeaveRoomPayload>) {
15
- const room = ctx.user.currentRoom;
16
 
17
- if (!room) {
18
- return ctx.ws.send(JSON.stringify({
19
- message: "leave_room_response",
20
- status: "you are not part of that room"
21
- }))
22
- } else {
23
- room.removeUserById(ctx.user.id);
24
- return ctx.ws.send(JSON.stringify({
25
- message: "leave_room_response",
26
- status: "room left"
27
- }))
28
- }
29
- },
30
- schema: LeaveRoomSchema,
31
  }
32
-
33
- export default LeaveRoomRoute;
 
 
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
+ export interface LeaveRoomPayload {
5
  roomId: string;
6
  }
7
+
8
+ export const LeaveRoomSchema = z.object({
9
  roomId: z.string(),
10
  })
11
 
12
+ export function LeaveRoomHandler(ctx: Context<LeaveRoomPayload>) {
13
+ const room = ctx.user.currentRoom;
 
 
14
 
15
+ if (!room) {
16
+ return ctx.ws.send(JSON.stringify({
17
+ message: "leave_room_response",
18
+ status: "you are not part of that room"
19
+ }))
20
+ } else {
21
+ room.removeUserById(ctx.user.id);
22
+ return ctx.ws.send(JSON.stringify({
23
+ message: "leave_room_response",
24
+ status: "room left"
25
+ }))
26
+ }
 
 
27
  }
 
 
server/src/socket/routes/send_vibration.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
- interface SendVibrationPayload {
5
  id: number;
6
  type: "enabled" | "disabled";
7
  position: {
@@ -12,7 +12,7 @@ interface SendVibrationPayload {
12
  intensity?: number;
13
  }
14
 
15
- const SendVibrationSchema = z.object({
16
  id: z.number(),
17
  type: z.union([z.literal("enabled"), z.literal("disabled")]),
18
  position: z.object({
@@ -23,31 +23,25 @@ const SendVibrationSchema = z.object({
23
  intensity: z.number().optional(),
24
  });
25
 
26
- const SendVibrationRoute: Route<SendVibrationPayload> = {
27
- name: "send_touch",
28
- handler: function(ctx: Context<SendVibrationPayload>) {
29
- if (!ctx.user.currentRoom) {
30
- return ctx.ws.send(JSON.stringify({
31
- "message": "send_vibration_response",
32
- "status": "not part of a room",
33
- }))
34
- }
35
 
36
- const p = ctx.payload;
37
 
38
- ctx.user.currentRoom.broadcast(JSON.stringify({
39
- message: "receieve_touch",
40
- id: ctx.user.id + "_" + p.id,
41
- type: p.type,
42
- user: {
43
- username: ctx.user.username,
44
- id: ctx.user.id,
45
- },
46
- color: p.color || "#FF0000",
47
- intensity: p.intensity ?? 1,
48
- }))
49
- },
50
- schema: SendVibrationSchema,
51
  }
52
-
53
- export default SendVibrationRoute;
 
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
+ export interface SendVibrationPayload {
5
  id: number;
6
  type: "enabled" | "disabled";
7
  position: {
 
12
  intensity?: number;
13
  }
14
 
15
+ export const SendVibrationSchema = z.object({
16
  id: z.number(),
17
  type: z.union([z.literal("enabled"), z.literal("disabled")]),
18
  position: z.object({
 
23
  intensity: z.number().optional(),
24
  });
25
 
26
+ export function SendVibrationHandler(ctx: Context<SendVibrationPayload>) {
27
+ if (!ctx.user.currentRoom) {
28
+ return ctx.ws.send(JSON.stringify({
29
+ "message": "send_vibration_response",
30
+ "status": "not part of a room",
31
+ }))
32
+ }
 
 
33
 
34
+ const p = ctx.payload;
35
 
36
+ ctx.user.currentRoom.broadcast(JSON.stringify({
37
+ message: "receieve_touch",
38
+ id: ctx.user.id + "_" + p.id,
39
+ type: p.type,
40
+ user: {
41
+ username: ctx.user.username,
42
+ id: ctx.user.id,
43
+ },
44
+ color: p.color || "#FF0000",
45
+ intensity: p.intensity ?? 1,
46
+ }))
 
 
47
  }
 
 
server/src/socket/routes/set_username.ts CHANGED
@@ -1,25 +1,19 @@
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
- interface SetUsernamePayload {
5
  username: string;
6
  }
7
- const SetUsernameSchema = z.object({
8
  username: z.string(),
9
  })
10
 
11
- const SetUsernameRoute: Route<SetUsernamePayload> = {
12
- name: "set_username",
13
- handler: function(ctx: Context<SetUsernamePayload>) {
14
 
15
- ctx.user.username = ctx.payload.username;
16
-
17
- return ctx.ws.send(JSON.stringify({
18
- "message": "set_username_response",
19
- "status": "success"
20
- }))
21
- },
22
- schema: SetUsernameSchema,
23
  }
24
 
25
- export default SetUsernameRoute;
 
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
+ export interface SetUsernamePayload {
5
  username: string;
6
  }
7
+ export const SetUsernameSchema = z.object({
8
  username: z.string(),
9
  })
10
 
11
+ export function SetUsernameHandler(ctx: Context<SetUsernamePayload>) {
12
+ ctx.user.username = ctx.payload.username;
 
13
 
14
+ return ctx.ws.send(JSON.stringify({
15
+ "message": "set_username_response",
16
+ "status": "success"
17
+ }))
 
 
 
 
18
  }
19
 
 
server/src/socket/routes/test_connection.ts CHANGED
@@ -1,25 +1,20 @@
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
- interface TestConnectionPayload { }
5
- const TestConnnectionSchema = z.object({})
6
 
7
- const TestConnectionRoute: Route<TestConnectionPayload> = {
8
- name: "test_connection",
9
- handler: function (ctx: Context<TestConnectionPayload>) {
10
- if (ctx.user) {
11
- return ctx.ws.send(JSON.stringify({
12
- "message": "test_connection_response",
13
- "authenticated": true,
14
- "username": ctx.user.username
15
- }))
16
- }
17
  return ctx.ws.send(JSON.stringify({
18
  "message": "test_connection_response",
19
- "authenticated": false,
 
20
  }))
21
- },
22
- schema: TestConnnectionSchema,
 
 
 
23
  }
24
 
25
- export default TestConnectionRoute;
 
1
  import { z } from "zod";
2
  import { Context, Route } from "../hapticLinkServer";
3
 
4
+ export interface TestConnectionPayload { }
5
+ export const TestConnnectionSchema = z.object({})
6
 
7
+ export function TestConnectionHandler(ctx: Context<TestConnectionPayload>) {
8
+ if (ctx.user) {
 
 
 
 
 
 
 
 
9
  return ctx.ws.send(JSON.stringify({
10
  "message": "test_connection_response",
11
+ "authenticated": true,
12
+ "username": ctx.user.username
13
  }))
14
+ }
15
+ return ctx.ws.send(JSON.stringify({
16
+ "message": "test_connection_response",
17
+ "authenticated": false,
18
+ }))
19
  }
20