Revert "Revert "Use jotai for websocket""
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This reverts commit 66d70cbfe4
.
This commit is contained in:
parent
07b883636d
commit
8c82cd81bd
|
@ -1,5 +1,5 @@
|
||||||
import { atom, useAtom } from 'jotai'
|
import { atom, useAtom } from 'jotai'
|
||||||
import { useCallback, useState } from 'react'
|
import { useCallback } from 'react'
|
||||||
|
|
||||||
const roomNameAtom = atom(getRoomNameFromUrl())
|
const roomNameAtom = atom(getRoomNameFromUrl())
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
import { useCallback, useState } from 'react'
|
import { atom, useAtom } from 'jotai'
|
||||||
|
import { useCallback, useEffect } from 'react'
|
||||||
import { WEBSOCKET_URL } from '../background/constants'
|
import { WEBSOCKET_URL } from '../background/constants'
|
||||||
import { UserInfo } from '../components/jitsi/types'
|
import { UserInfo } from '../components/jitsi/types'
|
||||||
|
|
||||||
const createWebSocketConnection = (userInfo: UserInfo) => {
|
|
||||||
const webSocket = new WebSocket(WEBSOCKET_URL)
|
|
||||||
console.log('[Rooms] createWebSocketConnection')
|
|
||||||
webSocket.addEventListener('open', (_: Event) => webSocket.send(JSON.stringify(userInfo)))
|
|
||||||
|
|
||||||
return webSocket
|
const webSocket = new WebSocket(WEBSOCKET_URL)
|
||||||
}
|
const webSocketConnectionAtom = atom(webSocket)
|
||||||
|
|
||||||
function useWebSocketConnection(userInfo: UserInfo) {
|
function useWebSocketConnection(userInfo: UserInfo) {
|
||||||
console.log('[Rooms] useWebSocketConnection')
|
console.log('[Rooms] useWebSocketConnection')
|
||||||
|
|
||||||
const [webSocketConnection] = useState<WebSocket>(() => createWebSocketConnection(userInfo))
|
const [webSocketConnection] = useAtom(webSocketConnectionAtom)
|
||||||
|
useEffect(() => {
|
||||||
|
sendMessageNowOrLater(webSocketConnection, JSON.stringify(userInfo))
|
||||||
|
}, [webSocketConnection, userInfo]);
|
||||||
|
|
||||||
const sendMessage = useCallback(
|
const sendMessage = useCallback(
|
||||||
(message: string) => {
|
(message: string) => {
|
||||||
|
@ -45,4 +45,11 @@ function useWebSocketConnection(userInfo: UserInfo) {
|
||||||
return { onMessage, cleanUpOnMessage, sendMessage, disconnect }
|
return { onMessage, cleanUpOnMessage, sendMessage, disconnect }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sendMessageNowOrLater = (webSocket: WebSocket, message: string) => {
|
||||||
|
if (webSocket.readyState !== WebSocket.OPEN) {
|
||||||
|
return webSocket.addEventListener('open', (_: Event) => webSocket.send(message))
|
||||||
|
}
|
||||||
|
webSocket.send(message)
|
||||||
|
}
|
||||||
|
|
||||||
export default useWebSocketConnection
|
export default useWebSocketConnection
|
||||||
|
|
Loading…
Reference in a new issue