/* Evennia ajax webclient (javascript component) The client is composed of several parts: templates/webclient.html - the main page webclient/views.py - the django view serving the template (based on urls.py pattern) src/server/webclient.py - the server component receiving requests from the client this file - the javascript component handling dynamic ajax content This implements an ajax mud client for use with Evennia, using jQuery for simplicity. It communicates with the Twisted server on the address /webclientdata through POST requests. Each request must at least contain the 'mode' of the request to be handled by the protocol: mode 'receive' - tell the server that we are ready to receive data. This is a long-polling (comet-style) request since the server will not reply until it actually has data available. The returned data object has two variables 'msg' and 'data' where msg should be output and 'data' is an arbitrary piece of data the server and client understands (not used in default client). mode 'input' - the user has input data on some form. The POST request should also contain variables 'msg' and 'data' where the 'msg' is a string and 'data' is an arbitrary piece of data from the client that the server knows how to deal with (not used in this example client). mode 'init' - starts the connection. All setup the server is requered to do should happen at this point. The server returns a data object with the 'msg' property containing the server address. mode 'close' - closes the connection. The server closes the session and does cleanup at this point. */ // jQuery must be imported by the calling html page before this script // There are plenty of help on using the jQuery library on http://jquery.com/ // Server communications var CLIENT_HASH = '0'; // variable holding the client id function webclient_receive(){ // This starts an asynchronous long-polling request. It will either timeout // or receive data from the 'webclientdata' url. In both cases a new request will // immediately be started. $.ajax({ type: "POST", url: "/webclientdata", async: true, // Turns off browser loading indicator cache: false, // Forces browser reload independent of cache timeout:30000, // Timeout in ms. After this time a new long-poll will be started. dataType:"json", data: {mode:'receive', 'suid':CLIENT_HASH}, // callback methods success: function(data){ // called when request to waitreceive completes msg_display("out", data.msg); // Add response to the message area webclient_receive(); // immediately start a new request }, error: function(XMLHttpRequest, textStatus, errorThrown){ webclient_receive(); // A possible timeout. Resend request immediately }, }); }; function webclient_input(arg){ // Send an input from the player to the server var outmsg = typeof(arg) != 'undefined' ? arg : $("#inputfield").val(); $.ajax({ type: "POST", url: "/webclientdata", async: true, cache: false, timeout: 30000, data: {mode:'input', msg:outmsg, data:'NoData', 'suid':CLIENT_HASH}, //callback methods success: function(data){ //if (outmsg.length > 0 ) msg_display("inp", outmsg) // echo input on command line history_add(outmsg); HISTORY_POS = 0; $('#inputform')[0].reset(); // clear input field }, error: function(XMLHttpRequest, textStatus, errorThrown){ msg_display("err", "Error: Server returned an error or timed out. Try resending or reloading the page."); }, }) } function webclient_init(){ // Start the connection by making sure the server is ready $.ajax({ type: "POST", url: "/webclientdata", async: true, cache: false, timeout: 50000, dataType:"json", data: {mode:'init', 'suid':CLIENT_HASH}, // callback methods success: function(data){ // called when request to initdata completes $("#connecting").remove() // remove the "connecting ..." message. CLIENT_HASH = data.suid // unique id hash given from server // A small timeout to stop 'loading' indicator in Chrome setTimeout(function () { $("#playercount").fadeOut('slow', webclient_set_sizes); }, 10000); // Report success msg_display('sys',"Connected to " + data.msg + "."); // Wait for input webclient_receive(); }, error: function(XMLHttpRequest, textStatus, errorThrown){ msg_display("err", "Connection error ..." + " (" + errorThrown + ")"); setTimeout('webclient_receive()', 15000); // try again after 15 seconds }, }); } function webclient_close(){ // Kill the connection and do house cleaning on the server. $.ajax({ type: "POST", url: "/webclientdata", async: false, cache: false, timeout: 50000, dataType: "json", data: {mode: 'close', 'suid': CLIENT_HASH}, success: function(data){ CLIENT_HASH = '0'; alert("Mud client connection was closed cleanly."); }, error: function(XMLHttpRequest, textStatus, errorThrown){ CLIENT_HASH = '0'; } }); } // Display messages function msg_display(type, msg){ // Add a div to the message window. // type gives the class of div to use. $("#messagewindow").append( "