File size: 5,721 Bytes
cfd4c95
 
2c59077
 
 
0f62e62
cfd4c95
 
 
 
 
 
 
 
 
 
 
 
 
2c59077
cfd4c95
 
 
2c59077
0f62e62
2c59077
0f62e62
2c59077
 
0f62e62
58fc009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c59077
0f62e62
81ad08a
0fef087
0f62e62
 
 
2c59077
0f62e62
 
0fef087
 
2c59077
0fef087
 
 
2c59077
 
 
 
 
 
 
0fef087
 
 
2c59077
0fef087
81ad08a
2c59077
0fef087
 
61ffaad
 
2c59077
 
 
61ffaad
 
0fef087
81ad08a
2c59077
0fef087
61ffaad
 
2c59077
61ffaad
 
81ad08a
0f62e62
2c59077
0fef087
 
2c59077
0fef087
 
 
 
2c59077
 
0fef087
 
 
81ad08a
0fef087
0f62e62
 
 
58fc009
2c59077
0f62e62
 
 
2c59077
0f62e62
2c59077
 
0fef087
0f62e62
 
 
2c59077
 
 
 
 
 
 
 
 
 
 
 
 
0f62e62
 
 
2c59077
0f62e62
81ad08a
2c59077
0fef087
0f62e62
 
 
2c59077
 
 
 
 
 
 
 
 
0f62e62
 
 
81ad08a
0fef087
0f62e62
 
 
2c59077
 
0f62e62
 
2c59077
 
ba51620
81ad08a
0fef087
 
ba51620
 
2c59077
ba51620
 
 
81ad08a
2c59077
0fef087
ba51620
 
2c59077
ba51620
 
 
2c59077
0e0a112
0f62e62
81ad08a
 
0fef087
0f62e62
 
 
38937c3
0f62e62
ba51620
0f62e62
 
 
 
ba51620
0f62e62
 
 
0e0a112
0f62e62
81ad08a
307d8b2
0fef087
0f62e62
 
 
e6f6400
0f62e62
 
 
 
 
 
 
 
 
 
 
2c59077
0f62e62
2c59077
 
0fef087
0f62e62
 
 
2c59077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f62e62
 
 
 
 
81ad08a
cfd4c95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# HapticTouch Server

The HapticTouch server is a NodeJS/TypeScript WebSocket and Express server used to manage HapticTouch.

# Instructions

```
git clone https://github.com/AnujPanthri/HapticLink
cd HapticLink/server
npm install
npm run dev
```

To build use:
`npm run build`

To run use:
`npm start`

# Documentation

Both HTTP and WebSocket server are running on the same port

# WebSocket API

WebSockets payload will be in JSON and require atleast a `route` property.
Types can be `client -> server`, `server -> client <private | room>`.
Private messages are sent directly to the client.
Room messages are sent to everyone in a room.

## Get Pong

**Type:** `client -> server`  
**Description:** Used to get pong message from server when requested. This can be used to detect when a client is disconnected from the server.
**Payload:**

```typescript
{
    "route": "get_pong",
}
```

**Type:** `server -> client <private>`  
**Payload:**

```typescript
{
    "message": "pong",
}
```

## Get User Info

**Type:** `client -> server`  
**Payload:**

```typescript
{
    "route": "get_user_info",
}
```

**Type:** `server -> client <private>`  
**Payload:**

```typescript
{
    "message": "set_username_response",
    "status": string,
    "user": {
        "username": string,
        "id": string,
        "currentRoom": string,
    }
}
```

## Join Room

**Type:** `client -> server`  
**Description:** Used to enter a room. Join an existing room by including roomId, or if room doesn't exist, create one with a random ID  
**Payload:**

```typescript
{
    "route": "join_room",
    "roomId"?: string, // Room Id, optional
    "username"?: string,
}
```

**Type:** `server -> client <private>`  
**Payload (only sent if there's an error, else room_update is sent):**

```typescript
{
    "message": "join_room_response",
    "status": string,
}
```

## Leave Room

**Type:** `client -> server`  
**Description:** Used to leave a room.  
**Payload:**

```typescript
{
    "route": "leave_room",
    "roomId": string; // Room Id, optional
}
```

**Type:** `server -> client <private>`  
**Payload:**

```typescript
{
	"message": "leave_room_response",
	"status": string,
}
```

## Receive Touch

**Type:** `server -> client <room>`  
**Description:** Broadcasted to all members of a room when any user sends a successful send_touch request  
**Payload:**

```typescript
{
    "message": "receive_touch",
    "id": string, // format: [userID]_[id]
    "type": "enable" | "disable", // Whether the vibration is active or not.
    "user": {
        username: string,
        id: string,
    },
    "position": {
        x: number,
        y: number,
    },
    "color": string, // Hex value.
    "intensity": number, // Vibration intensity.
}
```

## Send Touch

**Type:** `client -> server`  
**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 should automatically be disabled.  
**Payload:**

```typescript
{
    "route": "send_touch",
    "id": number, // Used to indentify vibrations for updating or disabling them, generated by client. Doesn't need to be secure.
    "type": "enable" | "disable", // Whether the vibration is active or not.
    "position": {
        x: number,
        y: number,
    },
    "color"?: string, // Hex value. Default: random
    "intensity"?: number, // Vibration intensity. Default: 1
}
```

**Type:** `server -> client <private>`  
**Payload:**

```typescript
{
    "message": "set_username_response",
    "status": string,
}

```
## Test Connection

**Type:** `client -> server`  
**Payload:**

```typescript
{
    "route": "test_connection"
}
```

**Type:** `server -> client <private>`  
**Response:**

```typescript
{
    "message": "test_connection_response",
}
```


## Room Update

**Type:** `server -> client <room>`  
**Description:** Server message that is sent when the room gets updated such as someone joins or leaves  
**Payload:**

```typescript
{
    "message": "room_update",
    "roomId": string,
    "users": [{
        username: string,
        id: string,
        online: boolean,
        lastOnline: number, // ms epoch time
    }],
}
```

## Send Touch

**Type:** `client -> server`  
**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.  
**Payload:**

```typescript
{
    "route": "send_touch",
    "id": number, // Used to indentify vibrations for updating or disabling them
    "type": "enable" | "disable", // Whether the vibration is active or not.
    "position": {
        x: number,
        y: number,
    },
    "color"?: string, // Hex value. Default: random
    "intensity"?: number, // Vibration intensity. Default: 1
}
```

## Set Username

**Type:** `client -> server`  
**Description**: Used to set username.
**Payload:**

```typescript
{
    "route": "set_username",
    "username": string,
}
```

## Get Rooms
*WARNING: not implemented yet*

**Type:** `client -> server`  
**Description:** Used to get list of joined rooms  
**Payload:**

```typescript
{
    "route": "get_rooms"
}
```

**Type:** `server -> client <private>`  
**Payload:**

```typescript
{
    "message": "get_rooms_response",
    "rooms": [{id: string, userCount: number}]
}
```

## API

**GET** `/`  
Responds with homepage.