2019-06-29 23:14:32 +00:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
|
|
|
|
import { ApplicationState } from '@/state/types';
|
2019-06-29 23:57:11 +00:00
|
|
|
import { Websocket } from '@/plugins/Websocket';
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const server = useStoreState((state: State<ApplicationState>) => state.server.data);
|
|
|
|
const instance = useStoreState((state: State<ApplicationState>) => state.server.socket.instance);
|
2019-06-29 23:57:11 +00:00
|
|
|
const setServerStatus = useStoreActions((actions: Actions<ApplicationState>) => actions.server.setServerStatus);
|
|
|
|
const { setInstance, setConnectionState } = useStoreActions((actions: Actions<ApplicationState>) => actions.server.socket);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// If there is already an instance or there is no server, just exit out of this process
|
|
|
|
// since we don't need to make a new connection.
|
|
|
|
if (instance || !server) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:57:11 +00:00
|
|
|
console.log('Connecting!');
|
2019-06-29 23:14:32 +00:00
|
|
|
|
2019-06-29 23:57:11 +00:00
|
|
|
const socket = new Websocket(
|
|
|
|
`wss://wings.pterodactyl.test:8080/api/servers/${server.uuid}/ws`,
|
|
|
|
'CC8kHCuMkXPosgzGO6d37wvhNcksWxG6kTrA'
|
|
|
|
);
|
|
|
|
|
|
|
|
socket.on('SOCKET_OPEN', () => setConnectionState(true));
|
|
|
|
socket.on('SOCKET_CLOSE', () => setConnectionState(false));
|
|
|
|
socket.on('SOCKET_ERROR', () => setConnectionState(false));
|
|
|
|
socket.on('status', (status) => setServerStatus(status));
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
setInstance(socket);
|
2019-06-29 23:57:11 +00:00
|
|
|
}, [ server ]);
|
2019-06-29 23:14:32 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|