Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: removed platform websocket #849

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage-website",
"version": "0.1.14",
"version": "0.1.15",
"license": "GPL-3.0",
"private": true,
"type": "module",
Expand Down
5 changes: 1 addition & 4 deletions src/lib/components/Global/DrawerMain.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import IconDrawerHome from '$lib/assets/icons/drawer/IconDrawerHome.svelte'
import IconDrawerVideos from '$lib/assets/icons/drawer/IconDrawerVideos.svelte'
import IconDrawerCreatorSpace from '$lib/assets/icons/drawer/IconDrawerCreatorSpace.svelte'
import IconDrawerCareers from '$lib/assets/icons/drawer/IconDrawerCareers.svelte'
import IconDrawerHelpAndLegal from '$lib/assets/icons/drawer/IconDrawerHelpAndLegal.svelte'
import IconDrawerSettings from '$lib/assets/icons/drawer/IconDrawerSettings.svelte'
import IconDrawerLogOut from '$lib/assets/icons/drawer/IconDrawerLogOut.svelte'
import IconSocialTwitter2 from '$lib/assets/icons/social/IconSocialTwitter2.svelte'
Expand All @@ -28,7 +26,6 @@
import { is_apps_modal_open, is_login_modal_open } from '$lib/stores/helperStore'
import { colorFromLevel, getNumberInThousands, levelAndBarValueFromExp } from '$lib/utils'
import { onMount } from 'svelte'
import { is_online } from '$lib/stores/userStore'
import { get } from '$lib/api'
import IconMageLogo from '$lib/assets/icons/IconMageLogo.svelte'
import IconDrawerVerification from '$lib/assets/icons/drawer/IconDrawerVerification.svelte'
Expand Down Expand Up @@ -96,7 +93,7 @@
<div class={isChannelPage ? 'md:text-center' : ''}>
<div class={isChannelPage ? '' : 'my-1'}>
<div class="flex gap-3 {isChannelPage ? 'max-w-md' : 'max-w-full'}">
<div class="avatar {$is_online ? 'online' : 'offline'}">
<div class="avatar">
<div
class="w-24 {isChannelPage ? 'md:w-12' : ''} mask {currentUser?.planDetails
?.planTier > 1
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stores/userStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writable, type Writable } from 'svelte/store'

export const user_role: Writable<any> = writable(null)
export const is_online: Writable<boolean> = writable(false)
// export const is_online: Writable<boolean> = writable(false)
4 changes: 2 additions & 2 deletions src/lib/stores/websocketStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writable, type Writable } from 'svelte/store'

export const platform_connection: Writable<string> = writable()
// export const platform_connection: Writable<string> = writable()
// export const platform_message: Writable<string> = writable()
export const channel_connection: Writable<string> = writable()
export const platform_message: Writable<string> = writable()
export const channel_message: Writable<string> = writable()
26 changes: 12 additions & 14 deletions src/lib/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { env } from '$env/dynamic/public'

let platformSocket: WebSocket

const initPlatformSocket = ({ websocketId }: { websocketId: string }) => {
platformSocket = new WebSocket(`${env.PUBLIC_WEBSOCKET_URL}/wsinit/wsid/${websocketId}/connect`)
}

const emitUserConnection = ({ userId, isOnline }: { userId: string; isOnline: boolean }) => {
platformSocket.send(
JSON.stringify({ eventName: isOnline ? 'user-connect' : 'user-disconnect', userId: userId })
)
}
// let platformSocket: WebSocket
// const initPlatformSocket = ({ websocketId }: { websocketId: string }) => {
// platformSocket = new WebSocket(`${env.PUBLIC_WEBSOCKET_URL}/wsinit/wsid/${websocketId}/connect`)
// }
// const emitUserConnection = ({ userId, isOnline }: { userId: string; isOnline: boolean }) => {
// platformSocket.send(
// JSON.stringify({ eventName: isOnline ? 'user-connect' : 'user-disconnect', userId: userId })
// )
// }

const initChannelSocket = ({ websocketId }: { websocketId: string }) => {
return new WebSocket(`${env.PUBLIC_WEBSOCKET_URL}/wsinit/channelid/${websocketId}/connect`)
Expand Down Expand Up @@ -173,10 +171,10 @@ const emitGetConnectedUsers = ({
}

export {
platformSocket,
initPlatformSocket,
// platformSocket,
// initPlatformSocket,
// emitUserConnection,
initChannelSocket,
emitUserConnection,
emitChannelUpdate,
emitChannelSubscribeByUser,
emitMessageToChannel,
Expand Down
95 changes: 46 additions & 49 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import DrawerMain from '$lib/components/Global/DrawerMain.svelte'
import { onMount } from 'svelte'
import { get, put } from '$lib/api'
import { emitUserConnection, initPlatformSocket, platformSocket } from '$lib/websocket'
import { platform_connection, platform_message } from '$lib/stores/websocketStore'
// import { emitUserConnection, initPlatformSocket, platformSocket } from '$lib/websocket'
// import { platform_connection, platform_message } from '$lib/stores/websocketStore'
import { isJsonString } from '$lib/utils'
import IconMageText from '$lib/assets/icons/IconMageText.svelte'
import { is_online } from '$lib/stores/userStore'
import { current_theme } from '$lib/stores/helperStore'
import {
is_feature_video_responses_enabled,
Expand Down Expand Up @@ -53,63 +52,61 @@
$is_feature_apps_enabled = env.PUBLIC_FEATURE_APPS === 'true'
$is_feature_merge_platforms_enabled = env.PUBLIC_FEATURE_MERGE_PLATFORMS === 'true'
$is_feature_merch_enabled = env.PUBLIC_FEATURE_MERCH === 'true'
await handleWebsocket()
if (!$category_list.length) {
$category_list = imageUrlsJson
}
getUserRole()
// await handleWebsocket()
})

const getUserRole = async () => {
//TODO: get role from backend
$user_role = 'user'
}

const handleWebsocket = async () => {
try {
if ($page.data.user?.userId) {
const platformSocketId = await get(`wsinit/wsid`)
initPlatformSocket({ websocketId: platformSocketId })
platformSocket.addEventListener('open', (data) => {
console.log('socket connection open')
console.log(data)
$platform_connection = 'open'
})
platformSocket.addEventListener('message', (data) => {
console.log('listening to messages')
console.log(data.data)
if (isJsonString(data.data)) platform_message.set(data.data)
})
platformSocket.addEventListener('error', (data) => {
console.log('socket connection error')
console.log(data)
})
platformSocket.addEventListener('close', (data) => {
console.log('socket connection close')
console.log(data)
$platform_connection = 'close'
attemptReconnect()
})
}
} catch (error) {
attemptReconnect()
}
}

const attemptReconnect = () => {
setTimeout(async () => {
console.log('Reconnecting to WebSocket...')
await handleWebsocket()
}, 4000)
}

platform_connection.subscribe(async (value: any) => {
if (!value) return
$is_online = value === 'open'
if ($page.data.user?.userId && value === 'open') {
emitUserConnection({ userId: $page.data.user?.userId, isOnline: $is_online })
}
})
// const handleWebsocket = async () => {
// try {
// if ($page.data.user?.userId) {
// const platformSocketId = await get(`wsinit/wsid`)
// initPlatformSocket({ websocketId: platformSocketId })
// platformSocket.addEventListener('open', (data) => {
// console.log('socket connection open')
// console.log(data)
// $platform_connection = 'open'
// })
// platformSocket.addEventListener('message', (data) => {
// console.log('listening to messages')
// console.log(data.data)
// if (isJsonString(data.data)) platform_message.set(data.data)
// })
// platformSocket.addEventListener('error', (data) => {
// console.log('socket connection error')
// console.log(data)
// })
// platformSocket.addEventListener('close', (data) => {
// console.log('socket connection close')
// console.log(data)
// $platform_connection = 'close'
// attemptReconnect()
// })
// }
// } catch (error) {
// attemptReconnect()
// }
// }
// const attemptReconnect = () => {
// setTimeout(async () => {
// console.log('Reconnecting to WebSocket...')
// await handleWebsocket()
// }, 4000)
// }
// platform_connection.subscribe(async (value: any) => {
// if (!value) return
// $is_online = value === 'open'
// if ($page.data.user?.userId && value === 'open') {
// emitUserConnection({ userId: $page.data.user?.userId, isOnline: $is_online })
// }
// })
</script>

<svelte:head>
Expand Down