From 2742a2232b863c11978f4774fffe580ed498049d Mon Sep 17 00:00:00 2001 From: yannis-mlgrn Date: Fri, 25 Feb 2022 22:38:59 +0100 Subject: [PATCH 1/3] emit json --- front/src/service.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/front/src/service.ts b/front/src/service.ts index 5f0f3b5..65c255d 100644 --- a/front/src/service.ts +++ b/front/src/service.ts @@ -85,8 +85,8 @@ interface ServerToClientEvents { } interface ClientToServerEvents { - join: (roomId: string) => void, - login: (id: string) => void, + join: (roomId: {roomId: string} ) => void, + login: (id: {userId: string}) => void, start_writing: () => void, stop_writing: () => void, } @@ -448,11 +448,11 @@ export class ScratchyService { * * @async * @augments ScratchyService - * @param {string} id - room id , eg:60895dd62d1a706830c31f10 + * @param {string} roomid - room id , eg:60895dd62d1a706830c31f10 * @returns {Promise} - */ async join(roomId:string): Promise { - this.socket.emit('join',roomId) + this.socket.emit('join',{ roomId :roomId }); } /** @@ -463,7 +463,7 @@ export class ScratchyService { * @returns {Promise} - */ async login(id:string): Promise { - this.socket.volatile.emit("login", id); + this.socket.emit("login", { userId : id }); } /** From dd57d3110de22cfd2480f49cca24c2a18437f9ae Mon Sep 17 00:00:00 2001 From: yannis-mlgrn Date: Sat, 26 Feb 2022 10:31:33 +0100 Subject: [PATCH 2/3] Draft of add rooms in user --- front/src/App.vue | 11 +++++++---- front/src/service.ts | 11 ++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/front/src/App.vue b/front/src/App.vue index 89e83a7..c7deca9 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -38,7 +38,7 @@ import RoomList from './components/RoomList.vue'; import Login from './components/Login.vue'; // note: this assumes port 5000 isn't that bad ? -const srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk"); +let srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk"); interface RoomData { title: string, @@ -73,7 +73,9 @@ export default defineComponent({ mounted() { this.fetchRooms(); // polling - srv.socket.on("message", this.messages.push); // when the app receive a message event, it push and display this message + srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk"); + srv.socket.on("message",this.messages.push);// when the app receive a message event, it push and display this message + srv.socket.on("people", users => this.users = users); // actualise the userList component when a user is added or removed }, methods: { @@ -104,7 +106,6 @@ export default defineComponent({ }, async displayRoom(room: Room) { // called when selecting a room in RoomList this.selectedRoom = room; - srv.join(this.selectedRoom.id); // emit join event to inform the server that the client have selected a room. this.users = await Promise.all(room.users.map(id => srv.getUserByid(id))); this.messages = await srv.getAllMessagesInRoom(room); }, @@ -138,7 +139,7 @@ export default defineComponent({ // update room list await this.fetchRooms(); }, - async joinRoom(id: string) { + async joinRoom(id: string,pseudo: string) { if (this.loggedUser === null) { console.warn("trying to join room without being logged in.") return; @@ -147,11 +148,13 @@ export default defineComponent({ // add current user to the room await srv.addUserToRoom(room,this.loggedUser); this.roomEditorHidden = true; + await srv.updateUser(this.loggedUser.id,pseudo,"",[room.id]); await this.fetchRooms(); }, async onLogin(pseudo: string) { this.loggedUser = await srv.getOrCreateUser(pseudo); await this.fetchRooms(); + console.log(this.loggedUser.id) srv.login(this.loggedUser.id); // send the client id to register in backend server }, }, diff --git a/front/src/service.ts b/front/src/service.ts index 65c255d..4c88f21 100644 --- a/front/src/service.ts +++ b/front/src/service.ts @@ -270,12 +270,14 @@ export class ScratchyService { * @augments ScratchyService * @param {string} userPseudo - a user pseudo * @param {string} userProfileImage - profile image link + * @param {[string]} rooms - Array of room where the user is * @returns {Promise} - user id */ - async createUser(userPseudo: string, userProfileImage: string): Promise { + async createUser(userPseudo: string, userProfileImage: string,rooms:[string]): Promise { const response = await axios.post(this.url + this.apiNamespace + "/user", { pseudo: userPseudo, profileImage: userProfileImage, + rooms : rooms, }); this.userCache.set(response.data.id, response.data); this.userPseudoMap.set(response.data.pseudo, response.data.id); @@ -306,9 +308,10 @@ export class ScratchyService { * @param {string} userID - user id * @param {string} userPseudo - a user's pseudo * @param {string} userProfileImage - profile image link + * @param {Room} Rooms - Array of room where the user is * @returns {Promise} - user information */ - async updateUser(userID: string, userPseudo: string, userProfileImage: string): Promise { + async updateUser(userID: string, userPseudo: string, userProfileImage: string,rooms: [string]): Promise { // update cache if (this.userCache.has(userID)) { const user = this.userCache.get(userID) as User; @@ -318,11 +321,13 @@ export class ScratchyService { } user.pseudo = userPseudo; user.profileImage = userProfileImage; + user.rooms = rooms this.userCache.set(userID, user); } const response = await axios.put(this.url + this.apiNamespace + "/user/" + userID, { pseudo: userPseudo, profileImage: userProfileImage, + rooms: rooms }); return response.data; } @@ -352,7 +357,7 @@ export class ScratchyService { async getOrCreateUser(pseudo: string): Promise { let user = await this.userExistByPseudo(pseudo); if (user === undefined) { - user = await this.createUser(pseudo, ""); + user = await this.createUser(pseudo,"",[""]); } return user; } From 90dc4c48a5f80bdb99dc5bb0871b18f55f11f653 Mon Sep 17 00:00:00 2001 From: yannis-mlgrn Date: Sat, 26 Feb 2022 10:36:06 +0100 Subject: [PATCH 3/3] typos --- front/src/App.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/front/src/App.vue b/front/src/App.vue index c7deca9..11dc3af 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -73,7 +73,6 @@ export default defineComponent({ mounted() { this.fetchRooms(); // polling - srv = new ScratchyService("http://"+window.location.hostname+":5000","/api","/wsk"); srv.socket.on("message",this.messages.push);// when the app receive a message event, it push and display this message srv.socket.on("people", users => this.users = users); // actualise the userList component when a user is added or removed @@ -147,8 +146,9 @@ export default defineComponent({ const room = await srv.getRoom(id); // add current user to the room await srv.addUserToRoom(room,this.loggedUser); + const roomsUser = await srv.getUserByid(id); this.roomEditorHidden = true; - await srv.updateUser(this.loggedUser.id,pseudo,"",[room.id]); + await srv.updateUser(this.loggedUser.id,pseudo,"", [ roomsUser.rooms.push(room.id) ]; await this.fetchRooms(); }, async onLogin(pseudo: string) {