simple-web-game/static/js/game.js

40 lines
953 B
JavaScript

var conn;
var me = {
"position": {
"x": 0,
"y": 0,
}
};
$(function() {
$("#game").hide();
$("#login-input").keypress(function (event) {
if (event.which == 13) {
me.name = $("#login-input").val();
$("#myname").text(me.name);
$("#game").toggle();
$("#login").toggle();
connect();
document.onkeydown = handleKeys;
}
});
});
function connect() {
console.log("connecting");
conn = new WebSocket(wsuri("/game/"));
conn.onclose = function(event) {
console.log("closed");
$("#console").text("connection lost");
}
conn.onmessage = function(event) {
var obj = JSON.parse(event.data);
$("#console").text(JSON.stringify(obj));
console.log(JSON.stringify(obj));
}
conn.onopen = function(event) {
console.log("got open event");
conn.send(me.name);
}
};