blob: f2d0741796e3c8ec4a0cc28b4293eb372fa4a96b (
plain) (
tree)
|
|
var s;
var antiflood = false;
var is_connected = false;
var chat_num = 0;
function removeThis(e) {
e.target.parentElement.removeChild(e.target);
}
function show_chat(msg, own) {
if(msg.startsWith("LOGIN:")) msg = "LOGIN:***";
if(own)
msg = ">>> " + msg;
else
msg = "<<< " + msg;
var chatbox = document.getElementById("chatbox");
var chatbox_length = chatbox.children.length;
if(chatbox_length >= CHATBOX_MAX)
chatbox.removeChild(chatbox.firstChild);
var line = document.createElement('div');
line.textContent = msg;
line.classList.add("l" + (chat_num ^= 1));
if(own)
line.classList.add("own");
chatbox.appendChild(line);
chatbox.scrollTop = chatbox.scrollHeight;
}
function on_msg(line) {
/*var args = line.split(':');
if(args[0] == "MSG") {
var color = args[1];
var msg = args[2].replace("%3A", ":");
show_chat(msg, color, false);
} else if (args[0] == "COUNT") {
document.getElementById("viewers").textContent = "Viewers: " + args[1];
}*/
show_chat(line, false);
}
function set_status(msg) {
document.getElementById("status").textContent = msg;
}
function start() {
if(CONNECT) {
s = new WebSocket("wss://tv.bienvenidoainternet.org:6969");
s.addEventListener("message", function(e) { on_msg(e.data); });
s.addEventListener("open", function(e) { set_status("Conectado al chat."); is_connected = true; });
s.addEventListener("close", function(e) { set_status("Desconectado del chat ("+ e.code + "|" + e.reason + ")"); is_connected = false; });
}
document.getElementById("sendchat").addEventListener("click", on_send);
}
function enable_chat() {
antiflood = false;
document.getElementById("sendchat").disabled = false;
}
function on_send(e) {
e.preventDefault();
if(antiflood) return;
if(!is_connected) return;
var msg = document.getElementById("chat_msg");
var msg_str = msg.value.trim().substr(0, MAXCHAR);
var send = document.getElementById("sendchat");
if(!msg_str) return;
s.send(msg_str);
show_chat(msg_str, true);
msg.value = "";
antiflood = true;
send.disabled = true;
setTimeout(enable_chat, FLOOD_TIME);
}
window.addEventListener("load", start);
|