From 744117cae8842152b95941b0e66107b73a05a05f Mon Sep 17 00:00:00 2001 From: StayFlex Date: Thu, 5 Dec 2024 20:39:33 +0100 Subject: [PATCH] Socket message --- .idea/.gitignore | 8 +++ .idea/Project.iml | 12 ++++ .idea/codeStyles/Project.xml | 63 ++++++++++++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 ++ .idea/inspectionProfiles/Project_Default.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/prettier.xml | 6 ++ .idea/vcs.xml | 4 ++ src/routes/chats/[id]/+page.svelte | 13 +++- vite.config.ts | 4 ++ 10 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Project.iml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/prettier.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Project.iml b/.idea/Project.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/Project.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..5055868 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..74f1bcb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/prettier.xml b/.idea/prettier.xml new file mode 100644 index 0000000..b0c1c68 --- /dev/null +++ b/.idea/prettier.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..d843f34 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/routes/chats/[id]/+page.svelte b/src/routes/chats/[id]/+page.svelte index dbed15f..2b7b5f9 100644 --- a/src/routes/chats/[id]/+page.svelte +++ b/src/routes/chats/[id]/+page.svelte @@ -5,6 +5,7 @@ import Message from "$lib/components/Message.svelte"; import UserChat from '$lib/components/ui/UserChat.svelte'; import { onMount, tick } from 'svelte'; + import { initSocket } from '$lib/stores/socket'; export let data; export let messages = data.messages.messages; @@ -12,6 +13,13 @@ let messageText = ''; + let socket = initSocket(); // Initialiser le socket + + socket.on("new-message", (message) => { + console.log('Recu du nouveau message', message); + messages = [...messages , message ]; + console.log(messages); + }); async function sendMessage() { // Appel API pour envoyer le message const response = await fetch(`/api/channels/${data.channelId}/messages`, { @@ -23,9 +31,12 @@ }); if (response.ok) { - messageText = ''; + let newMessage =await response.json(); + // Envoyer le message avec les sockets (à implémenter) + socket.emit('new-message', newMessage); console.log('Message envoyé avec succès'); + messageText = ''; }else{ console.log('Erreur lors de l\'envoi du message'); } diff --git a/vite.config.ts b/vite.config.ts index 042f8a7..83cdb8e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,6 +14,10 @@ const webSocketServer = { socket.on('new-channel', (channel) => { io.emit('new-channel', channel) }); + socket.on('new-message', (message) => { + console.log('Diffudion du nouveau message', message,"<= ici"); + io.emit('new-message', message) + }); }); } }