Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Added session UI for tab close #68

Merged
merged 10 commits into from
Nov 7, 2019
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ venv.bak/
.mypy_cache/

# Project Folders
log_data/
log_data/
.vscode/
43 changes: 30 additions & 13 deletions cast/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var sessions = [];
var clickCount = 0;
var TID = 0;
const MAX_LINES = 9999999;
const status = document.getElementById("status");
let socket;

const generateSSID = () => (
Expand All @@ -28,12 +29,16 @@ const termSetup = (term, ssid, newCurrentSession) => {
term.open(document.getElementById(ssid));
term.fit();
term.setOption("scrollback", MAX_LINES);
term.writeln("Welcome to cast.sh! - https://github.com/hericlesme/cast-sh - Press [Enter] to Start");
term.writeln("Welcome to cast.sh! - https://github.com/pod-cast/cast-sh - Press [Enter] to Start");

term.on("key", (key, ev) => {
// 'currentSsid' is global
console.log(`client-input:: from: ${currentSsid})}`);
socket.emit("client-input", { input: key, session_id: currentSsid });
getTabBySSID(currentSsid).session.term.write(key);
if(key.charCodeAt(0) == 13){
getTabBySSID(currentSsid).session.term.write('\n');
};
});

getTabBySSID(ssid).session = newCurrentSession;
Expand Down Expand Up @@ -74,29 +79,35 @@ const focusStyle = (tid) => {
tablink.style.color = "white";
logger.style.display = "block";
document.getElementById(tab.ssid).style.display = "none";
status.innerHTML =
'<span class="disconnected">tab disconnected</span>';
}
else {
tablink.classList.add('active');
logger.style.display = "none";
document.getElementById(tab.ssid).style.display = "block";
status.innerHTML =
'<span class="connected">connected</span>';
}
console.log("Session ID: " + getTabByTID(tid).ssid);
}

const openSession = (tid) => {
let tab = getTabByTID(tid);
focusStyle(tid);
console.log(tab.ssid);
// currentSession = tab.session;
currentSsid = tab.session.ssid;
// document.getElementById(tab.ssid).style.display = "block";
console.log(`openSession:: ${JSON.stringify(currentSsid)}`)
if (socket) {
// To register new session on WebSocket server
socket.emit("new-session", { session_id: currentSsid });

// To mark current tab as the current session on WebSocket server
socket.emit("client-input", { input: '', session_id: currentSsid })
if(closedTabs.includes(tid)){
console.log(socket);
console.log("This session is closed");
} else {
console.log(`openSession:: ${JSON.stringify(currentSsid)}`)
if (socket) {
// To register new session on WebSocket server
socket.emit("new-session", { session_id: currentSsid });

// To mark current tab as the current session on WebSocket server
socket.emit("client-input", { input: '', session_id: currentSsid })
}
}
}

Expand Down Expand Up @@ -242,7 +253,7 @@ currentSsid = createTab();

function downloadLog(ssid = currentSsid) {
console.log("Downloading log for " + ssid);
fetch('/download/' + ssid + '.log')
fetch('/download/log_' + ssid + '.log')
.then(function (response) {
if (!response.ok) {
throw Error(response.statusText);
Expand Down Expand Up @@ -272,7 +283,6 @@ function downloadLog(ssid = currentSsid) {


socket = io.connect("/cast", { query: `session_id=${currentSsid}` });
const status = document.getElementById("status");

socket.on("client-output", (data) => {
let ssid = data.ssid;
Expand All @@ -281,13 +291,20 @@ socket.on("client-output", (data) => {
});

socket.on("connect", () => {
var addTab = document.getElementById("create-tab");
addTab.style.display = "block";
status.innerHTML =
'<span class="connected">connected</span>';
});

socket.on("disconnect", () => {
var addTab = document.getElementById("create-tab");
addTab.style.display = "none";
status.innerHTML =
'<span class="disconnected">disconnected</span>';
for(var i=0; i<tabs.length;i++){
closeSession(String(i));
};
});


Expand Down
24 changes: 16 additions & 8 deletions cast/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def read_and_forward_pty_output(session_id):
max_read_bytes = 1024 * 20
max_read_bytes = 1024 * 2
app.config["current_session"] = session_id

while True:
Expand All @@ -39,12 +39,20 @@ def read_and_forward_pty_output(session_id):
timeout_sec = 0
(data_ready, _, _) = select.select([file_desc], [], [], timeout_sec)
if data_ready:
output = os.read(file_desc, max_read_bytes).decode()
socketio.emit(
"client-output",
{"output": output, "ssid": app.config["current_session"]},
namespace="/cast",
)
try:
output = os.read(file_desc, max_read_bytes).decode()
if len(output) > 5 or output == "\b":
socketio.emit(
"client-output",
{
"output": output,
"ssid": app.config["current_session"],
},
namespace="/cast",
)
except OSError:
socketio.emit("disconnect", namespace="/cast")
sys.exit(0)


@app.route("/")
Expand Down Expand Up @@ -184,7 +192,7 @@ def create_parser():
parser = argparse.ArgumentParser(
description=(
"An adorable instance of your terminal in your browser."
"https://github.com/hericlesme/cast-sh"
"https://github.com/pod-cast/cast-sh"
),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
Expand Down
2 changes: 1 addition & 1 deletion cast/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Logging:

def __init__(self, session_id):
self.folder = r"cast/log_data"
self.file_location = r"cast/log_data/" + str(session_id) + r".log"
self.file_location = r"cast/log_data/log_" + str(session_id) + r".log"

def write_log(self, data_value):
# Streams the data into a file for the Logging
Expand Down