Spaces:
Paused
Paused
Merge pull request #18 from AnujPanthri/ws_server
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/README.md
CHANGED
|
@@ -31,24 +31,38 @@ Room messages are sent to everyone in a room
|
|
| 31 |
## Test Connection
|
| 32 |
|
| 33 |
**Type:** `client -> server`
|
| 34 |
-
**Payload:**
|
| 35 |
|
| 36 |
```typescript
|
| 37 |
{
|
| 38 |
"route": "test_connection"
|
| 39 |
}
|
| 40 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
## Set Username
|
|
|
|
| 42 |
**Type:** `client -> server`
|
| 43 |
-
**Payload:**
|
|
|
|
| 44 |
```typescript
|
| 45 |
{
|
| 46 |
"route": "set_username",
|
| 47 |
"username": string,
|
| 48 |
}
|
| 49 |
```
|
|
|
|
| 50 |
**Type:** `server -> client <private>`
|
| 51 |
-
**Payload:**
|
|
|
|
| 52 |
```typescript
|
| 53 |
{
|
| 54 |
"message": "set_username_response",
|
|
@@ -56,12 +70,29 @@ Room messages are sent to everyone in a room
|
|
| 56 |
}
|
| 57 |
```
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
**Type:** `server -> client <private>`
|
| 60 |
-
**
|
| 61 |
|
| 62 |
```typescript
|
| 63 |
{
|
| 64 |
-
"message": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
```
|
| 67 |
|
|
@@ -69,7 +100,7 @@ Room messages are sent to everyone in a room
|
|
| 69 |
|
| 70 |
**Type:** `client -> server`
|
| 71 |
**Description:** Used to enter a room. Join an existing room by including roomId, or if room doesn't exist, create one with that roomId
|
| 72 |
-
**Payload:**
|
| 73 |
|
| 74 |
```typescript
|
| 75 |
{
|
|
@@ -80,7 +111,7 @@ Room messages are sent to everyone in a room
|
|
| 80 |
```
|
| 81 |
|
| 82 |
**Type:** `server -> client <private>`
|
| 83 |
-
**Payload (If error):**
|
| 84 |
|
| 85 |
```typescript
|
| 86 |
{
|
|
@@ -93,7 +124,7 @@ Room messages are sent to everyone in a room
|
|
| 93 |
|
| 94 |
**Type:** `client -> server`
|
| 95 |
**Description:** Used to enter a room.
|
| 96 |
-
**Payload:**
|
| 97 |
|
| 98 |
```typescript
|
| 99 |
{
|
|
@@ -103,7 +134,7 @@ Room messages are sent to everyone in a room
|
|
| 103 |
```
|
| 104 |
|
| 105 |
**Type:** `server -> client <private>`
|
| 106 |
-
**Payload:**
|
| 107 |
|
| 108 |
```typescript
|
| 109 |
{
|
|
@@ -116,7 +147,8 @@ Room messages are sent to everyone in a room
|
|
| 116 |
|
| 117 |
**Type:** `client -> server`
|
| 118 |
**Description:** Used to get list of joined rooms
|
| 119 |
-
**Payload:**
|
|
|
|
| 120 |
```typescript
|
| 121 |
{
|
| 122 |
"route": "get_rooms"
|
|
@@ -124,7 +156,8 @@ Room messages are sent to everyone in a room
|
|
| 124 |
```
|
| 125 |
|
| 126 |
**Type:** `server -> client <private>`
|
| 127 |
-
**Payload:**
|
|
|
|
| 128 |
```typescript
|
| 129 |
{
|
| 130 |
"message": "get_rooms_response",
|
|
@@ -136,7 +169,7 @@ Room messages are sent to everyone in a room
|
|
| 136 |
|
| 137 |
**Type:** `server -> client <room>`
|
| 138 |
**Description:** Server message that is sent when the room gets updated such as someone joins or leaves
|
| 139 |
-
**Payload:**
|
| 140 |
|
| 141 |
```typescript
|
| 142 |
{
|
|
@@ -155,7 +188,7 @@ Room messages are sent to everyone in a room
|
|
| 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
|
| 161 |
{
|
|
@@ -175,7 +208,7 @@ Room messages are sent to everyone in a room
|
|
| 175 |
|
| 176 |
**Type:** `server -> client <room>`
|
| 177 |
**Description:**
|
| 178 |
-
**Payload:**
|
| 179 |
|
| 180 |
```typescript
|
| 181 |
{
|
|
|
|
| 31 |
## Test Connection
|
| 32 |
|
| 33 |
**Type:** `client -> server`
|
| 34 |
+
**Payload:**
|
| 35 |
|
| 36 |
```typescript
|
| 37 |
{
|
| 38 |
"route": "test_connection"
|
| 39 |
}
|
| 40 |
```
|
| 41 |
+
|
| 42 |
+
**Type:** `server -> client <private>`
|
| 43 |
+
**Response:**
|
| 44 |
+
|
| 45 |
+
```typescript
|
| 46 |
+
{
|
| 47 |
+
"message": "test_connection_response",
|
| 48 |
+
}
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
## Set Username
|
| 52 |
+
|
| 53 |
**Type:** `client -> server`
|
| 54 |
+
**Payload:**
|
| 55 |
+
|
| 56 |
```typescript
|
| 57 |
{
|
| 58 |
"route": "set_username",
|
| 59 |
"username": string,
|
| 60 |
}
|
| 61 |
```
|
| 62 |
+
|
| 63 |
**Type:** `server -> client <private>`
|
| 64 |
+
**Payload:**
|
| 65 |
+
|
| 66 |
```typescript
|
| 67 |
{
|
| 68 |
"message": "set_username_response",
|
|
|
|
| 70 |
}
|
| 71 |
```
|
| 72 |
|
| 73 |
+
## Get User Info
|
| 74 |
+
|
| 75 |
+
**Type:** `client -> server`
|
| 76 |
+
**Payload:**
|
| 77 |
+
|
| 78 |
+
```typescript
|
| 79 |
+
{
|
| 80 |
+
"route": "get_user_info",
|
| 81 |
+
}
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
**Type:** `server -> client <private>`
|
| 85 |
+
**Payload:**
|
| 86 |
|
| 87 |
```typescript
|
| 88 |
{
|
| 89 |
+
"message": "set_username_response",
|
| 90 |
+
"status": string,
|
| 91 |
+
"user": {
|
| 92 |
+
"username": string,
|
| 93 |
+
"id": string,
|
| 94 |
+
"currentRoom": string,
|
| 95 |
+
}
|
| 96 |
}
|
| 97 |
```
|
| 98 |
|
|
|
|
| 100 |
|
| 101 |
**Type:** `client -> server`
|
| 102 |
**Description:** Used to enter a room. Join an existing room by including roomId, or if room doesn't exist, create one with that roomId
|
| 103 |
+
**Payload:**
|
| 104 |
|
| 105 |
```typescript
|
| 106 |
{
|
|
|
|
| 111 |
```
|
| 112 |
|
| 113 |
**Type:** `server -> client <private>`
|
| 114 |
+
**Payload (If error):**
|
| 115 |
|
| 116 |
```typescript
|
| 117 |
{
|
|
|
|
| 124 |
|
| 125 |
**Type:** `client -> server`
|
| 126 |
**Description:** Used to enter a room.
|
| 127 |
+
**Payload:**
|
| 128 |
|
| 129 |
```typescript
|
| 130 |
{
|
|
|
|
| 134 |
```
|
| 135 |
|
| 136 |
**Type:** `server -> client <private>`
|
| 137 |
+
**Payload:**
|
| 138 |
|
| 139 |
```typescript
|
| 140 |
{
|
|
|
|
| 147 |
|
| 148 |
**Type:** `client -> server`
|
| 149 |
**Description:** Used to get list of joined rooms
|
| 150 |
+
**Payload:**
|
| 151 |
+
|
| 152 |
```typescript
|
| 153 |
{
|
| 154 |
"route": "get_rooms"
|
|
|
|
| 156 |
```
|
| 157 |
|
| 158 |
**Type:** `server -> client <private>`
|
| 159 |
+
**Payload:**
|
| 160 |
+
|
| 161 |
```typescript
|
| 162 |
{
|
| 163 |
"message": "get_rooms_response",
|
|
|
|
| 169 |
|
| 170 |
**Type:** `server -> client <room>`
|
| 171 |
**Description:** Server message that is sent when the room gets updated such as someone joins or leaves
|
| 172 |
+
**Payload:**
|
| 173 |
|
| 174 |
```typescript
|
| 175 |
{
|
|
|
|
| 188 |
|
| 189 |
**Type:** `client -> server`
|
| 190 |
**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.
|
| 191 |
+
**Payload:**
|
| 192 |
|
| 193 |
```typescript
|
| 194 |
{
|
|
|
|
| 208 |
|
| 209 |
**Type:** `server -> client <room>`
|
| 210 |
**Description:**
|
| 211 |
+
**Payload:**
|
| 212 |
|
| 213 |
```typescript
|
| 214 |
{
|
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 |
+
});
|