Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend ~ sync switch #535

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion frontend/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,82 @@ button {
.editable-text-span:empty {
padding-left: 10px;
padding-right: 10px;
}
}

/* Sync/Async connection switch */
.switch {
position: relative;
display: inline-block;
width: 90px;
height: 34px;
}

.switch input {display:none;}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ca2222;
-webkit-transition: .4s;
transition: .4s;
}

.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}

input:checked + .slider {
background-color: #2ab934;
}

input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
-webkit-transform: translateX(55px);
-ms-transform: translateX(55px);
transform: translateX(55px);
}

.on
{
display: none;
}

.on, .off
{
color: white;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
font-size: 14px;
font-family: Verdana, sans-serif;
}

input:checked+ .slider .on
{display: block;}

input:checked + .slider .off
{display: none;}

/* Rounded sliders */
.slider.round {
border-radius: 34px;
}

.slider.round:before {
border-radius: 50%;}
59 changes: 57 additions & 2 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
const [editableText, setEditableText] = useState('');
const [errors, setErrors] = useState([]);
const [loggedMessages, setLoggedMessages] = useState([]);
const [syncConnection, setSyncConnection] = useState(true);
let incomingMessageJSON= useRef({});

// After the element rendering, connect to the given websocket port
Expand Down Expand Up @@ -60,6 +61,19 @@
}
}, [incomingMessage]);

// Need a different useEffect, due to the different dependency set
useEffect(() => {
if (ws) {
ws.addEventListener('message', handleMessageWebSocket);
}

return () => {
if (ws) {
ws.removeEventListener('message', handleMessageWebSocket);
}
};
}, [syncConnection]);

function handleOpenWebSocket(){
const message = `WebSocket connection for Port ${port} established.`
setLoggedMessages(prevMessages => [...prevMessages, message]);
Expand All @@ -71,8 +85,15 @@

function handleMessageWebSocket(event){
const message = decomposeJSON(event.data);
console.log("currently in sync and waiting", port);
setIncomingMessage(message); // This will launch the useEffect dependent on IncomingMessage
if( syncConnection === true ){
// Sync mode
console.log("currently in sync and waiting", port);
setIncomingMessage(message); // This will launch the useEffect dependent on IncomingMessage
}
else {
// Async mode
acceptAsyncIncomingLog(message);
}
}

function handleErrorWebSocket(event) {
Expand Down Expand Up @@ -128,6 +149,24 @@
clearIncomingLogMessages();
};

const acceptAsyncIncomingLog = (message) => {
console.log("new message async accepted on port: ", port);

// Accept ORIGINAL Log (without modifications and automatically)
setLoggedMessages(prevMessages => [...prevMessages, message]);

// Send the response on the WebSocket
let webSocketResponse = {
"Type": "accept",
"Value": ""
};
const webSocketResponseJSON = JSON.stringify(webSocketResponse);
ws.send(webSocketResponseJSON);

// Clear the input
clearIncomingLogMessages();
};

const replaceIncomingLog = () => {
if(incomingMessage === ""){
return;
Expand Down Expand Up @@ -339,8 +378,24 @@
}
}

const changeSyncronization = () => {
console.log("changing syncronization of port: ", port);
if (syncConnection) {
// Changing to Async mode
acceptIncomingLog(); // Accept also the existing incoming log
}
setSyncConnection(!syncConnection);
}

return (
<div id="sub-screen" className="sub-screen">
<label className="switch">
<input type="checkbox" id="sync-switch-input" checked={syncConnection} onChange={changeSyncronization} />
<div className="slider round">
<span className="on">Sync</span>
<span className="off">Async</span>
</div>
</label>
<button onClick={closeConnection} className="close-button">Close Connection</button>
<p className="websocket-console-title">
WebSocket Console for Port {port}:
Expand Down
Loading