Handle connecting to websocket instance

Very beta code for handling sockets
This commit is contained in:
Dane Everitt 2019-06-29 16:14:32 -07:00
parent 6618a124e7
commit f0ca8bc3a3
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
15 changed files with 297 additions and 30 deletions

View file

@ -0,0 +1,34 @@
import React, { useEffect } from 'react';
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
import { ApplicationState } from '@/state/types';
import Sockette from 'sockette';
export default () => {
const server = useStoreState((state: State<ApplicationState>) => state.server.data);
const instance = useStoreState((state: State<ApplicationState>) => state.server.socket.instance);
const setInstance = useStoreActions((actions: Actions<ApplicationState>) => actions.server.socket.setInstance);
const setConnectionState = useStoreActions((actions: Actions<ApplicationState>) => actions.server.socket.setConnectionState);
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;
}
console.log('need to connect to instance');
const socket = new Sockette(`wss://wings.pterodactyl.test:8080/api/servers/${server.uuid}/ws`, {
protocols: 'CC8kHCuMkXPosgzGO6d37wvhNcksWxG6kTrA',
// onmessage: (ev) => console.log(ev),
onopen: () => setConnectionState(true),
onclose: () => setConnectionState(false),
onerror: () => setConnectionState(false),
});
console.log('Setting instance!');
setInstance(socket);
}, [server]);
return null;
};