annotate www/m/js/nodescore-client.js @ 38:ea8d61c851c0

added chat server and more work on interface
author tzara <rc-web@kiben.net>
date Wed, 22 Aug 2012 12:51:28 +0000
parents c4719d1b7633
children 3ba24da53406
rev   line source
rc-web@12 1 /////////////////////////////////////////////////
rc-web@12 2 // connect to websocket
rc-web@12 3
rc-web@12 4 var socket = io.connect();
rc-web@12 5
rc-web@12 6 //socket.on("metroPulse", metronomeTick);
rc-web@12 7 /////////////////////////////////////////////////
rc-web@12 8 socket.on("metroPulse", pulseInClient);
rc-web@12 9 function pulseInClient(pulse,groupID,metrobeat){
rc-web@12 10 var groupPage=document.getElementById('group').value
rc-web@12 11 if (groupID == groupPage) {
rc-web@12 12 metronomeTick(1000, groupID, metrobeat);
rc-web@12 13 }
rc-web@12 14 }
rc-web@12 15 /////////////////////////////////////////////////
rc-web@12 16 function metroCss(beat, beatcolor,text){
rc-web@12 17 var color = beatcolor;
rc-web@12 18
rc-web@12 19 $(".metrocase > div").each(function(){$(this).stop()});
rc-web@12 20 $(".metrocase > div").each(function(){$(this).css('background-color', beatcolor)});
rc-web@12 21 $(".metrocase > div").each(function(){$(this).text(" ")});
rc-web@38 22 $(".metrocase > div").each(function(){$(this).animate({opacity: 1},
rc-web@12 23 50,
rc-web@12 24 function() { $(this).animate({opacity:0.0}); }
rc-web@12 25 );
rc-web@12 26 })
rc-web@12 27 }
rc-web@12 28
rc-web@12 29 function metronomeTick(pulse, voice,metrobeat) {
rc-web@12 30 var color = "gray"
rc-web@12 31 metroCss(0, "red", "4")
rc-web@12 32 };
rc-web@12 33
rc-web@12 34 /////////////////////////////////////////////////
rc-web@12 35
rc-web@12 36 // update the stopwatch value on the client page in line with server
rc-web@12 37 socket.on("chronFromServer", function(chron) {
rc-web@12 38 $("div#client_chronometer").text(chron);
rc-web@12 39 });
rc-web@12 40
rc-web@20 41
rc-web@22 42 // server time
rc-web@22 43
rc-web@22 44 //var groupPage=document.getElementById('group').value
rc-web@22 45 //if (groupID == groupPage) {
rc-web@22 46 socket.on("dateTime", function(datetime) {
rc-web@22 47 // console.log(datetime)
rc-web@23 48 $("div#datetime").text("Server Time: " +datetime);
rc-web@22 49
rc-web@22 50 });
rc-web@20 51
rc-web@20 52
rc-web@20 53 /////////////////////////////////////////////////
rc-web@20 54 // countdown to change
rc-web@20 55
rc-web@20 56 socket.on("countinFromServer", countinClient);
rc-web@20 57 function countinClient(groupID, currentseconds,mm,text,colour,background){
rc-web@20 58 var groupPage=document.getElementById('group').value
rc-web@20 59 if (groupID == groupPage) {
rc-web@38 60 document.getElementById("count").style.visibility="visible";
rc-web@38 61 document.getElementById("count").style.visibility="visible";
rc-web@38 62
rc-web@38 63 $("#counttitle").text(text);
rc-web@38 64 $("#count").text(currentseconds);
rc-web@38 65 //$("#cnt").text(currentseconds);
rc-web@38 66
rc-web@38 67 $("#count").css('background-color', background);
rc-web@20 68
rc-web@38 69 document.getElementById("count").style.color=colour;
rc-web@20 70
rc-web@38 71 // if ( currentseconds == 0) {
rc-web@38 72 //document.getElementById("midcomms").style.visibility='hidden';
rc-web@38 73 // }
rc-web@38 74 }}
rc-web@38 75 /////////////////////////////////////////////////
rc-web@38 76
rc-web@38 77 /////////////////////////////////////////////////
rc-web@38 78 // countdown to change
rc-web@38 79
rc-web@38 80 socket.on("counterText", counterText);
rc-web@38 81 function counterText(groupID, currentseconds,text){
rc-web@38 82 var groupPage=document.getElementById('group').value
rc-web@38 83 if (groupID == groupPage) {
rc-web@38 84 console.log()
rc-web@38 85 $("#totalcountdown").text(text);
rc-web@20 86 }}
rc-web@20 87
rc-web@38 88 ///////////////////////////////////////
rc-web@12 89
rc-web@12 90 function pageFlip(unit) {
rc-web@12 91 console.log("flipping page:"+ unit);
rc-web@12 92 $('#sections').trigger('goto', [parseFloat(unit)]);
rc-web@12 93 }
rc-web@12 94
rc-web@16 95
rc-web@12 96 /////////////////////////////////////////////////
rc-web@16 97
rc-web@12 98 // call the fancy jquery functions
rc-web@12 99
rc-web@12 100 function slideTo (target) { $('#sections').trigger('goto', [target]); }
rc-web@12 101 function pad2(number) { return (number < 10 ? '0' : '') + number }
rc-web@12 102
rc-web@12 103 /////////////////////////////////////////////////
rc-web@12 104
rc-web@12 105 socket.on("pageFlipfromserver", pageTurn);
rc-web@12 106 function pageTurn (group,unit,time,mm) {
rc-web@12 107 var groupPage=document.getElementById('group').value;
rc-web@12 108 if (group == groupPage) {
rc-web@12 109 var g= pad2(group);
rc-web@12 110 var p= pad2(unit);
rc-web@16 111 // pageFlip(unit);
rc-web@20 112 pageTurnB(unit)
rc-web@16 113
rc-web@16 114 }
rc-web@12 115 else {
rc-web@12 116 console.log("not for this group... ignoring... for group:" + group );
rc-web@12 117 }
rc-web@12 118 }
rc-web@12 119
rc-web@16 120 function pageTurnB(unit) {
rc-web@21 121 var units=6;
rc-web@20 122 var next=(((unit+1)%units)+units)%units
rc-web@20 123 console.log("HOP TURN" + unit+ "next:" + next);
rc-web@26 124 $("#live").load("music.html #"+unit +" *");
rc-web@32 125 $("#preview").html("<h3 style='background:transparent; position: absolute; top:14px; right:14px;';> n e x t : </h3><img src='thumbs/"+next + ".png" + "' width='400'>")
rc-web@16 126 }
rc-web@16 127
rc-web@18 128 ////////////////////////////////////////////////
rc-web@20 129
rc-web@38 130 /* commented out for now as interferes with chat
rc-web@38 131 need to introduce metakey...manyana... not so usefull anyway really...
rc-web@20 132
rc-web@20 133 keyboard controls
rc-web@20 134 +++++++++++++++++++++++++
rc-web@20 135
rc-web@20 136 SPACE to toggle visibilty of preview
rc-web@20 137 m to toggle visibilty of metronome
rc-web@20 138 s to toggle visibilty of stopwatch
rc-web@20 139 h to hide all above
rc-web@20 140
rc-web@38 141
rc-web@20 142
rc-web@18 143 function toggle_visibility(id) {
rc-web@18 144 var e = document.getElementById(id);
rc-web@18 145 if(e.style.display == 'block')
rc-web@18 146 e.style.display = 'none';
rc-web@18 147 else
rc-web@18 148 e.style.display = 'block';
rc-web@18 149 }
rc-web@18 150
rc-web@18 151 $(document).keypress(function(e){
rc-web@19 152 // "space bar" for next unit preview
rc-web@18 153 var checkWebkitandIE=(e.which==32 ? 1 : 0);
rc-web@18 154 var checkMoz=(e.which==32 ? 1 : 0);
rc-web@19 155 // "m" button for metronome
rc-web@19 156 var mcheckWebkitandIE=(e.which==109 ? 1 : 0);
rc-web@19 157 var mcheckMoz=(e.which==109 ? 1 : 0);
rc-web@19 158 // "s" button for stopwatch/chronometer
rc-web@19 159 var ccheckWebkitandIE=(e.which==115 ? 1 : 0);
rc-web@19 160 var ccheckMoz=(e.which==115 ? 1 : 0);
rc-web@19 161
rc-web@19 162 // "h" button for hideall
rc-web@19 163 var hcheckWebkitandIE=(e.which==104 ? 1 : 0);
rc-web@19 164 var hcheckMoz=(e.which==104 ? 1 : 0);
rc-web@19 165
rc-web@24 166 // console.log(e.which);
rc-web@19 167 if (checkWebkitandIE || checkMoz) { toggle_visibility('preview') }
rc-web@19 168 if (mcheckWebkitandIE || mcheckMoz) { toggle_visibility('metro') }
rc-web@19 169 if (ccheckWebkitandIE || ccheckMoz) { toggle_visibility('client_chronometer') }
rc-web@19 170 if (hcheckWebkitandIE || hcheckMoz) {
rc-web@19 171 toggle_visibility('client_chronometer')
rc-web@19 172 toggle_visibility('metro')
rc-web@19 173 toggle_visibility('preview')
rc-web@19 174 }
rc-web@18 175 });
rc-web@18 176
rc-web@38 177 */
rc-web@38 178
rc-web@18 179 ////////////////////////////////////////////////
rc-web@23 180 // this needs to have a variable to define the websocket
rc-web@23 181 // otherwise we will pings from all sockets connected
rc-web@23 182 // no! the server broadcasts the ping and the clients emit the pong!
rc-web@23 183 // ah but then the time reported back from the server needs to be targeted
rc-web@23 184 // to specific client..
rc-web@23 185 //////////////////////////////////////////////
rc-web@23 186 // Latency "Pong"
rc-web@23 187
rc-web@23 188 socket.on("timeFromServer", function(n) {
rc-web@23 189 socket.emit("clientTimeResponse",n);
rc-web@23 190 });
rc-web@23 191 socket.on("latencyFromServer", function(latency) {
rc-web@23 192 $("#client_latency").text("Latency: "+latency+"ms.")
rc-web@24 193 //console.log(latency)
rc-web@23 194 });
rc-web@23 195
rc-web@23 196 function getLatencies(x) { socket.emit("getLatencies", x); }
rc-web@23 197
rc-web@38 198 //////////////////////////////////////////////
rc-web@38 199
rc-web@38 200
rc-web@38 201 socket.on('connect', function () {
rc-web@38 202 $('#chat').addClass('connected');
rc-web@38 203 });
rc-web@38 204
rc-web@38 205 socket.on('announcement', function (msg) {
rc-web@38 206 $('#lines').append($('<p>').append($('<em>').text(msg)));
rc-web@38 207 });
rc-web@38 208
rc-web@38 209 socket.on('nicknames', function (nicknames) {
rc-web@38 210 $('#nicknames').empty().append($('<span>Online: </span>'));
rc-web@38 211 for (var i in nicknames) {
rc-web@38 212 $('#nicknames').append($('<b>').text(nicknames[i]));
rc-web@38 213 }
rc-web@38 214 });
rc-web@38 215
rc-web@38 216 socket.on('user message', message);
rc-web@38 217 socket.on('reconnect', function () {
rc-web@38 218 $('#lines').remove();
rc-web@38 219 message('System', 'Reconnected to the server');
rc-web@38 220 });
rc-web@38 221
rc-web@38 222 socket.on('reconnecting', function () {
rc-web@38 223 message('System', 'Attempting to re-connect to the server');
rc-web@38 224 });
rc-web@38 225
rc-web@38 226 socket.on('error', function (e) {
rc-web@38 227 message('System', e ? e : 'A unknown error occurred');
rc-web@38 228 });
rc-web@38 229
rc-web@38 230 function message (from, msg) {
rc-web@38 231 $('#lines').append($('<p>').append($('<b>').text(from), msg));
rc-web@38 232 }
rc-web@38 233
rc-web@38 234 // dom manipulation
rc-web@38 235 $(function () {
rc-web@38 236 $('#set-nickname').submit(function (ev) {
rc-web@38 237 socket.emit('nickname', $('#nick').val(), function (set) {
rc-web@38 238 if (!set) {
rc-web@38 239 clear();
rc-web@38 240 return $('#chat').addClass('nickname-set');
rc-web@38 241 }
rc-web@38 242 $('#nickname-err').css('visibility', 'visible');
rc-web@38 243 });
rc-web@38 244 return false;
rc-web@38 245 });
rc-web@38 246
rc-web@38 247 $('#send-message').submit(function () {
rc-web@38 248 message('me', $('#message').val());
rc-web@38 249 socket.emit('user message', $('#message').val());
rc-web@38 250 clear();
rc-web@38 251 $('#lines').get(0).scrollTop = 10000000;
rc-web@38 252 return false;
rc-web@38 253 });
rc-web@38 254
rc-web@38 255 function clear () {
rc-web@38 256 $('#message').val('').focus();
rc-web@38 257 };
rc-web@38 258 });