diff --git a/ch4/package-lock.json b/ch4/package-lock.json index ef6b6e6..43c43be 100644 --- a/ch4/package-lock.json +++ b/ch4/package-lock.json @@ -15,6 +15,7 @@ "http": "^0.0.1-security", "nodemon": "^2.0.22", "tslib": "^2.3.0", + "uuid": "^9.0.0", "ws": "^8.13.0" }, "devDependencies": { @@ -14129,6 +14130,15 @@ "websocket-driver": "^0.7.4" } }, + "node_modules/sockjs/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -15185,10 +15195,9 @@ } }, "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "dev": true, + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", "bin": { "uuid": "dist/bin/uuid" } diff --git a/ch4/package.json b/ch4/package.json index 27c0941..330ba20 100644 --- a/ch4/package.json +++ b/ch4/package.json @@ -17,6 +17,7 @@ "http": "^0.0.1-security", "nodemon": "^2.0.22", "tslib": "^2.3.0", + "uuid": "^9.0.0", "ws": "^8.13.0" }, "devDependencies": { diff --git a/ch4/src/assets/pages/index.html b/ch4/src/assets/pages/index.html index 4e25216..112d01d 100644 --- a/ch4/src/assets/pages/index.html +++ b/ch4/src/assets/pages/index.html @@ -1,73 +1,83 @@ - - - - - + + + + Websocket Test - - - + +

Message Test


-    
-    
-
+    
+    
 
     
-
-
-
\ No newline at end of file
+  
+
diff --git a/ch4/src/main.ts b/ch4/src/main.ts
index a431dfb..aefd31a 100644
--- a/ch4/src/main.ts
+++ b/ch4/src/main.ts
@@ -1,30 +1,49 @@
 import express from 'express';
 import { WebSocketServer } from 'ws';
 import http from 'http';
+import { v4 as uuidv4 } from 'uuid';
+import WebSocket from 'ws';
 
 const host = process.env.HOST ?? 'localhost';
 const port = process.env.PORT ? Number(process.env.PORT) : 3333;
 
 const app = express();
 const server = http.createServer(app);
-const wss = new WebSocketServer({ server })
+const wss = new WebSocketServer({ server });
 
+interface CustomWebSocket extends WebSocket {
+  dispatchEvent: (event: Event) => boolean;
+}
 
-wss.on('connection', function connection(ws) {
-    ws.on('message', function incoming(data) {
-        wss.clients.forEach(function each(client) {
-            if (client != ws && client.readyState == ws.OPEN) {
-                client.send(data)
-            }
-        })
-    });
-})  
+interface User {
+  username: string;
+  uuid: string;
+  ws: CustomWebSocket;
+}
 
+const users: User[] = [];
 
-// app.get('/', (req, res) => {
-//   res.send({ message: 'Hello API' });
-// });
+wss.on('connection', function connection(ws: CustomWebSocket) {
+  ws.on('message', function incoming(data) {
+    const message = JSON.parse(data.toString());
+    if (message.type === 'auth') {
+      const newUser: User = {
+        username: message.username,
+        uuid: uuidv4(),
+        ws: ws,
+      };
+      users.push(newUser);
+      ws.send(JSON.stringify({ type: 'auth', uuid: newUser.uuid }));
+    } else if (message.type === 'message') {
+      users.forEach(function each(user) {
+        if (user.ws != ws && user.ws.readyState == ws.OPEN) {
+          user.ws.send(JSON.stringify({ type: 'message', username: message.username, text: message.text }));
+        }
+      });
+    }
+  });
+});
 
 server.listen(port, host, () => {
   console.log(`[ ready ] http://${host}:${port}`);
-});
\ No newline at end of file
+});
diff --git a/package-lock.json b/package-lock.json
index d558707..a2f3052 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -2,5 +2,24 @@
   "name": "node_project_chatAPI",
   "lockfileVersion": 3,
   "requires": true,
-  "packages": {}
+  "packages": {
+    "": {
+      "dependencies": {
+        "v4": "^0.0.1"
+      }
+    },
+    "node_modules/lodash": {
+      "version": "3.10.1",
+      "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz",
+      "integrity": "sha512-9mDDwqVIma6OZX79ZlDACZl8sBm0TEnkf99zV3iMA4GzkIT/9hiqP5mY0HoT1iNLCrKc/R1HByV+yJfRWVJryQ=="
+    },
+    "node_modules/v4": {
+      "version": "0.0.1",
+      "resolved": "https://registry.npmjs.org/v4/-/v4-0.0.1.tgz",
+      "integrity": "sha512-UILzWfbLuyncHcGquTcLzetXtX/Kgpnug6iLn/7ruiYhgVzI5RPiNRdDJlxlpCr2qwtJ53soZ0TTNeOwB3aSBA==",
+      "dependencies": {
+        "lodash": "^3.10.0"
+      }
+    }
+  }
 }
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5b93e13
--- /dev/null
+++ b/package.json
@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "v4": "^0.0.1"
+  }
+}