Newer
Older
Intro_Quiz_2018 / intro.js
function intro() {
    var team = document.getElementById("tm"),
	button = document.getElementById("push"),
	warn = document.getElementById("warn"),
	info = document.getElementById("info");
    var conn, PORT=4946, server = location.hostname;
    function initConn() {
	try {
	    conn = new WebSocket('ws://' + server + ':' + PORT + '/');
	    conn.onopen = function() {};		// Nothing special
	    conn.onerror = function(err) {
		info.textContent = 'WebSocket failure: ' + err
	    };
	    warn.textContent = null;
	    conn.onmessage = function(ev) {
		if (ev.data) {
		    var j = JSON.parse(ev.data);
		    info.textContent = j.info;
		    warn.textContent = j.warn;
		    if (j.rank == 1)
			button.setAttribute("class", "win");
		    else
			button.setAttribute("class", null);
		}
	    };
	    conn.onclose = function(ev) {
		info.textContent = "接続断: PUSHで参加";
		conn = null;
	    };
	    info.textContent = "Ready...";
	} catch (err) {
	    alert("Socket Creation Error\n\
Firefoxですか? URLウィンドウに about:config と入れて\n\
Search: 窓に websocket と入れて、\n\
network..websocket.allowInsecureFromHTTP\n\
の行をダブルクリックして true に変えてください。\n" + err);
	}
    }
    function push(ev) {
	if (!conn) initConn();
	conn.send(team.value);
    }
    button.addEventListener("mousedown", push, false);
    initConn();
}
document.addEventListener("DOMContentLoaded", intro, false);