1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

F OpenNebula/one#5422: Add sockets temporal

This commit is contained in:
Sergio Betanzos 2021-06-22 09:20:58 +02:00
parent 301da35182
commit 15d5c9eebd
No known key found for this signature in database
GPG Key ID: E3E704F097737136

View File

@ -1,11 +1,36 @@
import { useContext, useCallback } from 'react'
import socketIO from 'socket.io-client'
import { SOCKETS } from 'client/constants'
import { APP_URL, SOCKETS } from 'client/constants'
import { SocketContext } from 'client/providers/socketProvider'
import { useAuth } from 'client/features/Auth'
import { useGeneral } from 'client/features/General'
const createWebsocket = (path, query) => socketIO({ path: `${APP_URL}/${path}`, query })
export default function useSocket () {
const { jwt } = useAuth()
const { zone } = useGeneral()
const { socket, isConnected } = useContext(SocketContext)
const getHooksSocketTemporal = useCallback(({ resource, id }) => {
const socket = createWebsocket(
SOCKETS.hooks,
{ token: jwt, zone, resource, id }
)
return {
connect: callback => {
socket.on(SOCKETS.hooks, callback)
},
disconnect: () => {
socket.disconnect()
}
}
}, [jwt, zone])
const getHooksSocket = useCallback(func => ({
on: () => isConnected && socket.on(SOCKETS.hooks, func),
off: () => isConnected && socket.off(SOCKETS.hooks, func)
@ -16,5 +41,10 @@ export default function useSocket () {
off: () => isConnected && socket.off(SOCKETS.provision, func)
}), [socket, isConnected])
return { isConnected, getHooksSocket, getProvisionSocket }
return {
isConnected,
getHooksSocket,
getProvisionSocket,
getHooksSocketTemporal
}
}