Spaces:
Runtime error
Runtime error
Added get_user_info route
Browse files
server/.eslintrc.json
CHANGED
|
@@ -12,7 +12,6 @@
|
|
| 12 |
"plugins": ["@typescript-eslint"],
|
| 13 |
"rules": {
|
| 14 |
"semi": [2, "always"],
|
| 15 |
-
"no-unused-vars": "error",
|
| 16 |
"linebreak-style": ["error", "unix"],
|
| 17 |
"indent": ["error", 4]
|
| 18 |
}
|
|
|
|
| 12 |
"plugins": ["@typescript-eslint"],
|
| 13 |
"rules": {
|
| 14 |
"semi": [2, "always"],
|
|
|
|
| 15 |
"linebreak-style": ["error", "unix"],
|
| 16 |
"indent": ["error", 4]
|
| 17 |
}
|
server/src/socket/hapticLinkServer.ts
CHANGED
|
@@ -21,6 +21,7 @@ export class User {
|
|
| 21 |
constructor(socket: WebSocketInterface) {
|
| 22 |
this.id = generateSessionToken(32);
|
| 23 |
this.socket = socket;
|
|
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
|
|
|
|
| 21 |
constructor(socket: WebSocketInterface) {
|
| 22 |
this.id = generateSessionToken(32);
|
| 23 |
this.socket = socket;
|
| 24 |
+
this.username = "Unknown";
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
server/src/socket/routes.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import { HapticLinkServer } from "./hapticLinkServer";
|
|
|
|
| 2 |
import { JoinRoomHandler, JoinRoomSchema } from "./routes/join_room";
|
| 3 |
import { LeaveRoomHandler, LeaveRoomSchema } from "./routes/leave_room";
|
| 4 |
import { SendVibrationHandler, SendVibrationSchema } from "./routes/send_touch";
|
|
@@ -11,4 +12,5 @@ export function registerRoutes(router: HapticLinkServer) {
|
|
| 11 |
router.addRoute("leave_room", LeaveRoomSchema, LeaveRoomHandler);
|
| 12 |
router.addRoute("send_touch", SendVibrationSchema, SendVibrationHandler);
|
| 13 |
router.addRoute("set_username", SetUsernameSchema, SetUsernameHandler);
|
|
|
|
| 14 |
}
|
|
|
|
| 1 |
import { HapticLinkServer } from "./hapticLinkServer";
|
| 2 |
+
import { GetUserInfoHandler, GetUserInfoSchema } from "./routes/get_user_info";
|
| 3 |
import { JoinRoomHandler, JoinRoomSchema } from "./routes/join_room";
|
| 4 |
import { LeaveRoomHandler, LeaveRoomSchema } from "./routes/leave_room";
|
| 5 |
import { SendVibrationHandler, SendVibrationSchema } from "./routes/send_touch";
|
|
|
|
| 12 |
router.addRoute("leave_room", LeaveRoomSchema, LeaveRoomHandler);
|
| 13 |
router.addRoute("send_touch", SendVibrationSchema, SendVibrationHandler);
|
| 14 |
router.addRoute("set_username", SetUsernameSchema, SetUsernameHandler);
|
| 15 |
+
router.addRoute("get_user_info", GetUserInfoSchema, GetUserInfoHandler);
|
| 16 |
}
|
server/src/socket/routes/get_user_info.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { z } from "zod";
|
| 2 |
+
import { Context } from "../hapticLinkServer";
|
| 3 |
+
|
| 4 |
+
export interface GetUserInfoPayload { }
|
| 5 |
+
export const GetUserInfoSchema = z.object({ });
|
| 6 |
+
|
| 7 |
+
export function GetUserInfoHandler(ctx: Context<GetUserInfoPayload>) {
|
| 8 |
+
return ctx.ws.send(
|
| 9 |
+
JSON.stringify({
|
| 10 |
+
message: "get_user_info_response",
|
| 11 |
+
status: "success",
|
| 12 |
+
user: {
|
| 13 |
+
id: ctx.user.id,
|
| 14 |
+
username: ctx.user.username,
|
| 15 |
+
currentRoom: ctx.user.currentRoom,
|
| 16 |
+
},
|
| 17 |
+
})
|
| 18 |
+
);
|
| 19 |
+
}
|
server/test/socket/routes/get_user_info.spec.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { expect } from "chai";
|
| 2 |
+
import { Context, HapticLinkServer, User } from "../../../src/socket/hapticLinkServer";
|
| 3 |
+
import { GetUserInfoHandler, GetUserInfoPayload } from "../../../src/socket/routes/get_user_info";
|
| 4 |
+
import { WebSocketWrapper } from "../hapticLinkServer.spec";
|
| 5 |
+
|
| 6 |
+
describe("Get User Info", () => {
|
| 7 |
+
let server: HapticLinkServer;
|
| 8 |
+
let ws: WebSocketWrapper;
|
| 9 |
+
let user: User;
|
| 10 |
+
let ctx: Context<GetUserInfoPayload>;
|
| 11 |
+
|
| 12 |
+
beforeEach(() => {
|
| 13 |
+
server = new HapticLinkServer();
|
| 14 |
+
ws = new WebSocketWrapper();
|
| 15 |
+
user = new User(ws);
|
| 16 |
+
user.username = "testname";
|
| 17 |
+
ctx = {
|
| 18 |
+
ws: ws,
|
| 19 |
+
user: user,
|
| 20 |
+
payload: {},
|
| 21 |
+
server: server,
|
| 22 |
+
};
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
it("should get user info", (done) => {
|
| 26 |
+
GetUserInfoHandler(ctx);
|
| 27 |
+
const res = JSON.parse(ws.sendData as string);
|
| 28 |
+
expect(res.message).to.equal("get_user_info_response", "incorrect message");
|
| 29 |
+
expect(res.status).to.equal("success", "incorrect status");
|
| 30 |
+
expect(res.user.username).to.equal("testname", "incorrect username");
|
| 31 |
+
expect(res.user.currentRoom).to.equal(undefined, "incorrect current room");
|
| 32 |
+
expect(res.user.id.length).to.gt(4, "incorrect id");
|
| 33 |
+
done();
|
| 34 |
+
});
|
| 35 |
+
});
|