Spaces:
Runtime error
Runtime error
Removed multiroom thing and added send_vibration
Browse files- server/README.md +1 -1
- server/src/socket/hapticLinkServer.ts +3 -6
- server/src/socket/room.ts +1 -1
- server/src/socket/routes.ts +2 -2
- server/src/socket/routes/get_rooms.ts +0 -30
- server/src/socket/routes/join_room.ts +2 -7
- server/src/socket/routes/leave_room.ts +1 -6
- server/src/socket/routes/send_vibration.ts +53 -0
server/README.md
CHANGED
|
@@ -154,7 +154,7 @@ Room messages are sent to everyone in a room
|
|
| 154 |
## Send Touch
|
| 155 |
|
| 156 |
**Type:** `client -> server`
|
| 157 |
-
**Description:** Used to send touch/vibration data to room. Client assigns a number for an ID. That ID is then used to update the vibration. Re-use `send_touch` to update previous vibration. Use `"type": "disable"` to disable the vibration. If an update isn't sent within 1 second of
|
| 158 |
**Payload:**
|
| 159 |
|
| 160 |
```typescript
|
|
|
|
| 154 |
## Send Touch
|
| 155 |
|
| 156 |
**Type:** `client -> server`
|
| 157 |
+
**Description:** Used to send touch/vibration data to room. Client assigns a number for an ID. That ID is then used to update the vibration. Re-use `send_touch` to update previous vibration. Use `"type": "disable"` to disable the vibration. If an update isn't sent within 1 second of creation/last update it will automatically be disabled.
|
| 158 |
**Payload:**
|
| 159 |
|
| 160 |
```typescript
|
server/src/socket/hapticLinkServer.ts
CHANGED
|
@@ -16,12 +16,10 @@ export class User {
|
|
| 16 |
username?: string;
|
| 17 |
socket: WebSocket;
|
| 18 |
currentRoom?: Room;
|
| 19 |
-
rooms: Room[];
|
| 20 |
|
| 21 |
constructor(socket: WebSocket) {
|
| 22 |
this.id = generateSessionToken(16);
|
| 23 |
this.socket = socket;
|
| 24 |
-
this.rooms = [];
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
|
@@ -46,10 +44,9 @@ export class HapticLinkServer {
|
|
| 46 |
removeUser(ws: WebSocket): boolean {
|
| 47 |
const user = this.users.get(ws);
|
| 48 |
if (!user) return false;
|
| 49 |
-
user.
|
| 50 |
-
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
return true;
|
| 54 |
}
|
| 55 |
|
|
|
|
| 16 |
username?: string;
|
| 17 |
socket: WebSocket;
|
| 18 |
currentRoom?: Room;
|
|
|
|
| 19 |
|
| 20 |
constructor(socket: WebSocket) {
|
| 21 |
this.id = generateSessionToken(16);
|
| 22 |
this.socket = socket;
|
|
|
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
|
|
|
| 44 |
removeUser(ws: WebSocket): boolean {
|
| 45 |
const user = this.users.get(ws);
|
| 46 |
if (!user) return false;
|
| 47 |
+
if (user.currentRoom) {
|
| 48 |
+
user.currentRoom.removeUserById(user.id);
|
| 49 |
+
}
|
|
|
|
| 50 |
return true;
|
| 51 |
}
|
| 52 |
|
server/src/socket/room.ts
CHANGED
|
@@ -17,8 +17,8 @@ export class Room {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
addUser(user: User) {
|
|
|
|
| 20 |
this.users.push(user);
|
| 21 |
-
user.rooms.push(this);
|
| 22 |
this.updateRoom();
|
| 23 |
}
|
| 24 |
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
addUser(user: User) {
|
| 20 |
+
// Generate random roomId if one wasn't created.
|
| 21 |
this.users.push(user);
|
|
|
|
| 22 |
this.updateRoom();
|
| 23 |
}
|
| 24 |
|
server/src/socket/routes.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import { HapticLinkServer } from "./hapticLinkServer";
|
| 2 |
-
import GetRoomsRoute from "./routes/get_rooms";
|
| 3 |
import JoinRoomRoute from "./routes/join_room";
|
| 4 |
import LeaveRoomRoute from "./routes/leave_room";
|
|
|
|
| 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(
|
| 12 |
}
|
|
|
|
| 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 |
}
|
server/src/socket/routes/get_rooms.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
import { z } from "zod";
|
| 2 |
-
import { Context, Route } from "../hapticLinkServer";
|
| 3 |
-
|
| 4 |
-
interface GetRoomsPayload {}
|
| 5 |
-
const GetRoomsSchema = z.object({})
|
| 6 |
-
|
| 7 |
-
interface RoomData {
|
| 8 |
-
id: string;
|
| 9 |
-
userCount: number;
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
const GetRoomsRoute: Route<GetRoomsPayload> = {
|
| 13 |
-
name: "get_rooms",
|
| 14 |
-
handler: function(ctx: Context<GetRoomsPayload>) {
|
| 15 |
-
let rooms: RoomData[] = [];
|
| 16 |
-
ctx.user.rooms.forEach((room) => {
|
| 17 |
-
rooms.push({
|
| 18 |
-
id: room.id,
|
| 19 |
-
userCount: room.users.length
|
| 20 |
-
})
|
| 21 |
-
})
|
| 22 |
-
return ctx.ws.send(JSON.stringify({
|
| 23 |
-
"message": "get_rooms_response",
|
| 24 |
-
"rooms": rooms,
|
| 25 |
-
}))
|
| 26 |
-
},
|
| 27 |
-
schema: GetRoomsSchema,
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
export default GetRoomsRoute;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server/src/socket/routes/join_room.ts
CHANGED
|
@@ -20,13 +20,6 @@ const JoinRoomRoute: Route<JoinRoomPayload> = {
|
|
| 20 |
ctx.user.username = ctx.payload.username;
|
| 21 |
}
|
| 22 |
|
| 23 |
-
if (ctx.user.rooms.length > 4) {
|
| 24 |
-
return ctx.ws.send(JSON.stringify({
|
| 25 |
-
message: "join_room_response",
|
| 26 |
-
status: "you are already part of 5 groups. leave one to join another."
|
| 27 |
-
}))
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
let room: Room;
|
| 31 |
|
| 32 |
if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
|
|
@@ -40,6 +33,8 @@ const JoinRoomRoute: Route<JoinRoomPayload> = {
|
|
| 40 |
room = ctx.server.rooms[ctx.payload.roomId]
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
| 43 |
// Broadcasts message to room
|
| 44 |
room.addUser(ctx.user);
|
| 45 |
},
|
|
|
|
| 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]) {
|
|
|
|
| 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 |
},
|
server/src/socket/routes/leave_room.ts
CHANGED
|
@@ -12,9 +12,7 @@ const LeaveRoomSchema = z.object({
|
|
| 12 |
const LeaveRoomRoute: Route<LeaveRoomPayload> = {
|
| 13 |
name: "leave_room",
|
| 14 |
handler: function(ctx: Context<LeaveRoomPayload>) {
|
| 15 |
-
const room = ctx.user.
|
| 16 |
-
return ctx.payload.roomId == r.id;
|
| 17 |
-
})
|
| 18 |
|
| 19 |
if (!room) {
|
| 20 |
return ctx.ws.send(JSON.stringify({
|
|
@@ -23,9 +21,6 @@ const LeaveRoomRoute: Route<LeaveRoomPayload> = {
|
|
| 23 |
}))
|
| 24 |
} else {
|
| 25 |
room.removeUserById(ctx.user.id);
|
| 26 |
-
ctx.user.rooms = ctx.user.rooms.filter((r) => {
|
| 27 |
-
return r != room
|
| 28 |
-
})
|
| 29 |
return ctx.ws.send(JSON.stringify({
|
| 30 |
message: "leave_room_response",
|
| 31 |
status: "room left"
|
|
|
|
| 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({
|
|
|
|
| 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"
|
server/src/socket/routes/send_vibration.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { z } from "zod";
|
| 2 |
+
import { Context, Route } from "../hapticLinkServer";
|
| 3 |
+
|
| 4 |
+
interface SendVibrationPayload {
|
| 5 |
+
id: number;
|
| 6 |
+
type: "enabled" | "disabled";
|
| 7 |
+
position: {
|
| 8 |
+
x: number,
|
| 9 |
+
y: number,
|
| 10 |
+
};
|
| 11 |
+
color?: string;
|
| 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({
|
| 19 |
+
x: z.number(),
|
| 20 |
+
y: z.number(),
|
| 21 |
+
}),
|
| 22 |
+
color: z.string().optional(),
|
| 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;
|