var socket; var conn_div; var num_msgs; var mode; var wshost = 'ws://127.0.0.1:12345/phudbase/server/game.php' $(document).ready(function(){ conn_div = document.getElementById('connection'); num_msgs = 0; if ("WebSocket" in window) { mode = "websocket"; socket = new WebSocket(wshost); socket.onmessage = function(evt) { handle_read(evt.data); } socket.onclose = function(evt) { conn_div.style.background = "red"; conn_div.innerHTML = "<p style='font-size: 1.25em; font-weight: bold; color: #fff;'>DISCONNECTED</p>"; } socket.onopen = function(evt) { conn_div.style.background = "green"; conn_div.innerHTML = "<p style='font-size: 1.5em; font-weight: bold; color: #fff;'>CONNECTED</p>"; ow_Write("<div id='login_area'>Type a name for your Character and press Enter.<br><br><form onsubmit='sendDirect(document.getElementById(\"login\").value); postLogin(); return false;'><input id='login' type='text'></form></div>"); } } else { alert("Your browser doesn't support WebSockets! Try Google Chrome to play."); } }); function postLogin() { $("#data_form").fadeIn(500); $("#look_button").fadeIn(500); $("#login_area").remove(); } var sendDirect = function(data) { socket.send(data); } var send = function() { s = document.getElementById("data").value; print(s, "#999"); socket.send(s); document.getElementById("data").value = ""; } var print = function(s) { var color = "#ccc"; if (typeof(arguments[1]) != undefined) color = arguments[1]; ow_Write("<span style='color:"+color+ "'>→ " + s + "</span>", true); } function handle_read(s) { //eval(s); //ow_Write(s); data = eval("(" + s + ")"); if (data.message) ow_Write(data.message); if (data.mapdata) map_Write(data.mapdata); if (data.oldroom) document.getElementById(data.oldroom).style.border = "2px solid #000"; if (data.newroom) document.getElementById(data.newroom).style.border = "2px solid #ff0000"; if (data.rcdata) rc_Write(data.rcdata); if (data.server_status) ss_Write(data.server_status); } function login() { uname = document.getElementById('username'); upass = document.getElementById('password'); var lstring = "LOGIN " + uname.value + ":" + upass.value; socket.send(lstring); $('#login').remove(); document.getElementById("login_area").innerHTML = "<span style='color: #cbd3ff'>Please wait while your existence in the world of dragons is verified....</span><br><br>"; } function ow_Write(text,nodelay) { var objDiv = window.top.document.getElementById("output"); var nID = "msg" + num_msgs; //if (nodelay == true) //{ objDiv.innerHTML += "<div id='" + nID + "' style='margin-bottom: 10px;'>" + text + "</div>"; //} else { // objDiv.innerHTML += "<div id='" + nID + "' class='msg' style='display: none; margin-bottom: 10px;'>" + text + "</div>"; // $(("#msg"+num_msgs)).fadeIn(50); //} num_msgs++; objDiv.scrollTop = objDiv.scrollHeight; } function rc_Write(text) { window.top.document.getElementById("room_content").innerHTML = text; } function map_Write(text) { window.top.document.getElementById("map").innerHTML = text; } function ss_Write(text) { window.top.document.getElementById("server_status").innerHTML = text; } function expandHeight(eid) { document.getElementById(eid).style.visibility = 'visible'; document.getElementById(eid).style.position = "relative"; document.getElementById(eid).style.width = "99%"; document.getElementById(eid).style.overflow = "visible"; } function shrinkHeight(eid) { document.getElementById(eid).style.visibility = 'hidden'; document.getElementById(eid).style.position = "absolute" document.getElementById(eid).style.width = "0"; document.getElementById(eid).style.overflow = "hidden"; } function setDD(tdid) { document.getElementById(tdid).style.background = "#cbd3ff"; } function unsetDD(tdid) { document.getElementById(tdid).style.background = "#98a7ff"; }