1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00
cowyo/static/js/websockets.js
Zack Scholl 61bf3ac1a6 Renamed things
Former-commit-id: c1ac3dff7cd98b431815a6867393683c04567470 [formerly 06230779a9c4f9fa0070a26062b43f787f258dce] [formerly a9a7f5db2062c986fa7c3a44f6c5cdaf1853083c [formerly 268f550d1d71847a35352780d53d0d4418661009 [formerly 7e1bd2081b]]]
Former-commit-id: b2fccfab3d9c08df25b7c5f00a01e003db82fe11 [formerly b1c31dbba1fecbd40ba7c8521101db5af70ff7d3]
Former-commit-id: 546f2ac1bf93f0306b18c93ff6ec89d583ef1520
Former-commit-id: 1bb73acaa8
2016-02-10 09:09:33 -05:00

61 lines
1.8 KiB
JavaScript

$(document).ready(function() {
var isTyping = false;
var typingTimer; //timer identifier
var updateInterval;
var doneTypingInterval = 500; //time in ms, 5 second for example
var pollToGetNewestCopyInterval = 10000;
//on keyup, start the countdown
$('#emit').keyup(function() {
clearTimeout(typingTimer);
clearInterval(updateInterval);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$('#emit').keydown(function() {
clearTimeout(typingTimer);
clearInterval(updateInterval);
document.title = "[UNSAVED] " + title_name;
});
//user is "finished typing," do something
function doneTyping() {
payload = JSON.stringify({ TextData: $('#emit_data').val(), Title: title_name, UpdateServer: true, UpdateClient: false })
send(payload)
console.log("Done typing")
updateInterval = setInterval(updateText, pollToGetNewestCopyInterval);
document.title = "[SAVED] " + title_name;
}
function updateText() {
console.log("Getting server's latest copy")
payload = JSON.stringify({ TextData: $('#emit_data').val(), Title: title_name, UpdateServer: false, UpdateClient: true })
send(payload)
}
// websockets
url = 'ws://'+external_ip+'/ws';
c = new WebSocket(url);
send = function(data){
console.log("Sending: " + data)
c.send(data)
}
c.onmessage = function(msg){
console.log(msg)
data = JSON.parse(msg.data);
if (data.UpdateClient == true) {
console.log("Updating...")
$('#emit_data').val(data.TextData)
document.title = "[LOADED] " + title_name;
}
console.log(data)
}
c.onopen = function(){
// updateText();
updateInterval = setInterval(updateText, pollToGetNewestCopyInterval);
}
});