-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketHandler.tsx
More file actions
109 lines (88 loc) · 3.34 KB
/
SocketHandler.tsx
File metadata and controls
109 lines (88 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { SOCKET_BASE_URL } from 'app/../config/config';
import 'app/styles/ReactToastNotifications.css';
import * as SocketHandlerInterfaces from 'app/types/SocketHandler';
import * as React from 'react';
import { useToasts } from 'react-toast-notifications';
import * as io from 'socket.io-client';
// tslint:disable-next-line: variable-name
export const SocketHandler: React.FunctionComponent<SocketHandlerInterfaces.Props> = (
props: SocketHandlerInterfaces.Props,
) => {
const { addToast } = useToasts();
const socket: SocketIOClient.Socket = io.connect(SOCKET_BASE_URL, {
reconnection: true,
reconnectionDelay: 1000,
transports: ['websocket'],
});
React.useEffect(() => {
const {
sendCompileError,
sendCompileSuccess,
sendExecuteError,
sendExecuteSuccess,
sendDebugRunSuccess,
sendDebugRunError,
} = props;
socket.on('Info', (message: string) => {
addToast(message, { appearance: 'info', autoDismiss: true });
});
socket.on('Success', (message: string) => {
addToast(message, { appearance: 'success', autoDismiss: true });
});
socket.on('Error', (message: string) => {
addToast(message, { appearance: 'error', autoDismiss: true });
});
socket.on('connect', () => {
addToast('Connected to Server!', { appearance: 'success', autoDismiss: true });
});
socket.on('Compile Info', (message: string) => {
addToast(message, { appearance: 'info', autoDismiss: true });
});
socket.on('Compile Success', () => {
addToast('Compiled Successfully!', { appearance: 'success', autoDismiss: true });
sendCompileSuccess();
});
socket.on('Compile Error', (message: string) => {
addToast(`Compile Error: ${message}`, { appearance: 'error', autoDismiss: true }),
sendCompileError('');
});
socket.on('Compile Error Log', (log: string) => {
addToast('Compile Error', { appearance: 'error', autoDismiss: true });
sendCompileError(log);
});
socket.on('Match Info', (message: string) => {
addToast(message, { appearance: 'info', autoDismiss: true });
});
socket.on('Match Error', (message: string) => {
addToast(message, { appearance: 'error', autoDismiss: true });
sendExecuteError(message);
});
socket.on('Match Result Success', (result: string) => {
addToast(result, { appearance: 'success', autoDismiss: true });
});
socket.on('Match Result Error', (result: string) => {
addToast(result, { appearance: 'error', autoDismiss: true });
});
socket.on('Match Success', (matchLogs: string) => {
addToast('Match Executed Successfully!', { appearance: 'success', autoDismiss: true });
sendExecuteSuccess(matchLogs);
});
socket.on('Debug Run Info', (message: string) => {
addToast(message, { appearance: 'info', autoDismiss: true });
});
socket.on('Debug Run Success', (stackTrace: string) => {
sendDebugRunSuccess(stackTrace);
});
socket.on('Debug Run Error', (message: string) => {
addToast(`Debug Run Error: ${message}`, { appearance: 'error', autoDismiss: true }),
sendDebugRunError();
});
socket.on('disconnect', () => {
addToast('Disconnected', { appearance: 'error', autoDismiss: true });
});
return () => {
socket.disconnect();
};
}, []);
return null;
};