vue에서 connection 연결하고 알림 받는법
<script setup>
import { ref} from 'vue'
const data = new EventSource("<http://localhost:9090/notifications/connect>");
data.addEventListener('connect', (e) => {
console.log('SSE connet: ',e); // "connected!"
})
data.addEventListener('notification', (e) => {
console.log('SSE notification: ',e); // "connected!"
})
const notification = ref({
title:"",
content:"",
member:{
id:0
}
});
const send = () => {
console.log(notification.value)
fetch("<http://localhost:9090/notifications>" , {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(notification.value)
})
.then(response => response.json())
.then(data => console.log(data))
}
const push = () => {
console.log("push")
fetch("<http://localhost:9090/notifications/push/kbumk123>")
.then(response => response.json())
.then(data => console.log(data))
}
</script>
<template>
<form action="" @submit.prevent>
<input type="text" placeholder="title" v-model="notification.title"> <br>
<input type="text" placeholder="content" v-model="notification.content"> <br>
<input type="number" placeholder="memberId" v-model="notification.member.id"> <br>
<input type="button" @click="send" value="알림전송">
<input type="button" @click="push" value="push">
</form>
</template>
<style scoped>
</style>