Spaces:
Sleeping
Sleeping
Changed addRoute method to be easier to understand
Browse files- server/src/socket/hapticLinkServer.ts +7 -3
- server/src/socket/routes.ts +9 -8
- server/src/socket/routes/join_room.ts +21 -27
- server/src/socket/routes/leave_room.ts +17 -23
- server/src/socket/routes/send_vibration.ts +21 -27
- server/src/socket/routes/set_username.ts +8 -14
- server/src/socket/routes/test_connection.ts +11 -16
server/src/socket/hapticLinkServer.ts
CHANGED
|
@@ -50,12 +50,16 @@ export class HapticLinkServer {
|
|
| 50 |
return true;
|
| 51 |
}
|
| 52 |
|
| 53 |
-
addRoute(
|
| 54 |
-
if (
|
| 55 |
return false
|
| 56 |
}
|
| 57 |
|
| 58 |
-
this.routes[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
export function registerRoutes(router: HapticLinkServer) {
|
| 8 |
-
router.addRoute(
|
| 9 |
-
router.addRoute(
|
| 10 |
-
router.addRoute(
|
| 11 |
-
router.addRoute(
|
|
|
|
| 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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
ctx.user.username = ctx.payload.username;
|
| 21 |
-
}
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 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 |
-
|
|
|
|
| 9 |
roomId: z.string(),
|
| 10 |
})
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
handler: function(ctx: Context<LeaveRoomPayload>) {
|
| 15 |
-
const room = ctx.user.currentRoom;
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
}))
|
| 34 |
-
}
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 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 |
-
|
| 12 |
-
|
| 13 |
-
handler: function(ctx: Context<SetUsernamePayload>) {
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
| 8 |
-
|
| 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":
|
|
|
|
| 20 |
}))
|
| 21 |
-
}
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|