|
1 | 1 | <script setup> |
2 | | -import { ref, onMounted } from 'vue'; |
| 2 | +import { ref, onMounted, onUnmounted } from "vue"; |
3 | 3 |
|
4 | | -const status = ref('text-gruvbox-gray'); |
| 4 | +const ws = ref(null); |
| 5 | +const status = ref("text-gruvbox-gray"); |
5 | 6 | const spotify = ref(null); |
6 | 7 |
|
7 | | -onMounted(async () => { |
8 | | - await fetch('https://api.lanyard.rest/v1/users/748539900793716887') |
9 | | - .then(response => response.json()) |
10 | | - .then(data => { |
11 | | - spotify.value = data.data.spotify; |
12 | | - switch (data.data.discord_status) { |
13 | | - case 'online': |
14 | | - status.value = 'text-gruvbox-green'; |
| 8 | +const connectWebSocket = () => { |
| 9 | + ws.value = new WebSocket("wss://api.lanyard.rest/socket"); |
| 10 | + ws.value.onopen = () => { |
| 11 | + console.log("WebSocket connection established."); |
| 12 | + ws.value.send( |
| 13 | + JSON.stringify({ |
| 14 | + op: 2, |
| 15 | + d: { subscribe_to_id: "748539900793716887" }, |
| 16 | + }), |
| 17 | + ); |
| 18 | + }; |
| 19 | +
|
| 20 | + ws.value.onmessage = (event) => { |
| 21 | + const data = JSON.parse(event.data); |
| 22 | + if (data.t === "INIT_STATE" || data.t === "PRESENCE_UPDATE") { |
| 23 | + const presence = data.d; |
| 24 | + spotify.value = presence.spotify; |
| 25 | +
|
| 26 | + switch (presence.discord_status) { |
| 27 | + case "online": |
| 28 | + status.value = "text-gruvbox-green"; |
15 | 29 | break; |
16 | | - case 'idle': |
17 | | - status.value = 'text-gruvbox-yellow'; |
| 30 | + case "idle": |
| 31 | + status.value = "text-gruvbox-yellow"; |
18 | 32 | break; |
19 | | - case 'dnd': |
20 | | - status.value = 'text-gruvbox-red'; |
| 33 | + case "dnd": |
| 34 | + status.value = "text-gruvbox-red"; |
21 | 35 | break; |
22 | 36 | } |
23 | | - }) |
24 | | - .catch(() => { |
25 | | - return; |
26 | | - }); |
27 | | -}) |
| 37 | +
|
| 38 | + ws.value.onerror = (error) => { |
| 39 | + console.error("WebSocket error: ", error); |
| 40 | + }; |
| 41 | +
|
| 42 | + ws.value.onclose = () => { |
| 43 | + console.log("WebSocket connection closed."); |
| 44 | + }; |
| 45 | + } |
| 46 | + }; |
| 47 | +}; |
| 48 | +
|
| 49 | +onMounted(async () => { |
| 50 | + connectWebSocket(); |
| 51 | +}); |
| 52 | +
|
| 53 | +onUnmounted(() => { |
| 54 | + if (ws.value) { |
| 55 | + ws.value.close(); |
| 56 | + } |
| 57 | +}); |
28 | 58 | </script> |
29 | 59 |
|
30 | 60 | <template> |
31 | 61 | <div class="font-sans font-black text-5xl"> |
32 | 62 | babaoglu<span :class="['text-2xl', status]">.dev</span> |
33 | 63 | </div> |
34 | | - <div>emirhan (aka larei), 18 years-old. self-taught developer, <a href="https://www.youtube.com/watch?v=9sJUDx7iEJw" target="_blank" class="underline">open-source</a> enthusiast. programming, music, math.</div> |
| 64 | + <div> |
| 65 | + emirhan (aka larei), 18 years-old. self-taught developer, |
| 66 | + <a |
| 67 | + href="https://www.youtube.com/watch?v=9sJUDx7iEJw" |
| 68 | + target="_blank" |
| 69 | + class="underline" |
| 70 | + >open-source</a |
| 71 | + > |
| 72 | + enthusiast. programming, music, math. |
| 73 | + </div> |
35 | 74 | <div class="flex gap-2 text-sm text-gruvbox-gray"> |
36 | 75 | <font-awesome-icon :icon="['fab', 'spotify']" class="mt-[3px]" /> |
37 | 76 | <div v-if="spotify"> |
38 | | - i'm currently listening to <a :href="`https://open.spotify.com/track/${spotify.track_id}`" target="_blank" |
39 | | - class="font-black underline">{{ spotify.song }} - {{ spotify.artist }}</a>. |
40 | | - </div> |
41 | | - <div v-else> |
42 | | - i'm not listening to anything right now. |
| 77 | + i'm currently listening to |
| 78 | + <a |
| 79 | + :href="`https://open.spotify.com/track/${spotify.track_id}`" |
| 80 | + target="_blank" |
| 81 | + class="font-black underline" |
| 82 | + >{{ spotify.song }} - {{ spotify.artist }}</a |
| 83 | + >. |
43 | 84 | </div> |
| 85 | + <div v-else>i'm not listening to anything right now.</div> |
44 | 86 | </div> |
45 | 87 | <div class="flex gap-10 mt-5 text-xl"> |
46 | | - <a href="https://github.com/lareii/" target="_blank"><font-awesome-icon :icon="['fab', 'github']" /></a> |
47 | | - <a href="https://www.linkedin.com/in/larei/" target="_blank"><font-awesome-icon :icon="['fab', 'linkedin']" /></a> |
48 | | - <a href="https://discord.gg/taMRRAHb6y" target="_blank"><font-awesome-icon :icon="['fab', 'discord']" /></a> |
49 | | - <a href="https://mastodon.com.tr/@larei" target="_blank" rel="me"><font-awesome-icon :icon="['fab', 'mastodon']" /></a> |
| 88 | + <a href="https://github.com/lareii/" target="_blank" |
| 89 | + ><font-awesome-icon :icon="['fab', 'github']" |
| 90 | + /></a> |
| 91 | + <a href="https://www.linkedin.com/in/larei/" target="_blank" |
| 92 | + ><font-awesome-icon :icon="['fab', 'linkedin']" |
| 93 | + /></a> |
| 94 | + <a href="https://discord.gg/taMRRAHb6y" target="_blank" |
| 95 | + ><font-awesome-icon :icon="['fab', 'discord']" |
| 96 | + /></a> |
| 97 | + <a href="https://mastodon.com.tr/@larei" target="_blank" rel="me" |
| 98 | + ><font-awesome-icon :icon="['fab', 'mastodon']" |
| 99 | + /></a> |
50 | 100 | </div> |
51 | 101 | </template> |
0 commit comments