Skip to content

Commit dac4641

Browse files
lareiilostf1sh
andcommitted
feat: websocket connection for live presence
Co-authored-by: lostf1sh <slorixbaba@outlook.com>
1 parent 2578578 commit dac4641

File tree

1 file changed

+79
-29
lines changed

1 file changed

+79
-29
lines changed

src/components/Header.vue

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,101 @@
11
<script setup>
2-
import { ref, onMounted } from 'vue';
2+
import { ref, onMounted, onUnmounted } from "vue";
33
4-
const status = ref('text-gruvbox-gray');
4+
const ws = ref(null);
5+
const status = ref("text-gruvbox-gray");
56
const spotify = ref(null);
67
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";
1529
break;
16-
case 'idle':
17-
status.value = 'text-gruvbox-yellow';
30+
case "idle":
31+
status.value = "text-gruvbox-yellow";
1832
break;
19-
case 'dnd':
20-
status.value = 'text-gruvbox-red';
33+
case "dnd":
34+
status.value = "text-gruvbox-red";
2135
break;
2236
}
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+
});
2858
</script>
2959

3060
<template>
3161
<div class="font-sans font-black text-5xl">
3262
babaoglu<span :class="['text-2xl', status]">.dev</span>
3363
</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>
3574
<div class="flex gap-2 text-sm text-gruvbox-gray">
3675
<font-awesome-icon :icon="['fab', 'spotify']" class="mt-[3px]" />
3776
<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+
>.
4384
</div>
85+
<div v-else>i'm not listening to anything right now.</div>
4486
</div>
4587
<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>
50100
</div>
51101
</template>

0 commit comments

Comments
 (0)