123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <html>
- <head>
- <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
- </head>
- <body>
- <input id="msg" type="text">
- <button id="send">Send</button>
- <div id="logs">
- </div>
- <script>
- $(document).ready(function(){
- $("#send").click(function(){
- var msg = $("#msg").val();
- console.log(msg);
- if (msg.length > 0)
- $.post("/send", msg);
- $("#msg").val("");
- });
- $("#msg").keyup(function(event){
- if(event.keyCode == 13){
- $("#send").click();
- }
- });
- var lastLog = 0;
- var updateLog;
- updateLog = function(data)
- {
- console.log("recv ");
- console.log(data);
- var lastLog = data.last*1;
- console.log("lastLog: " + lastLog);
- var s = "";
- function htmlEncode(s)
- {
- return s.replace(/&(?!\w+([;\s]|$))/g, "&")
- .replace(/</g, "<").replace(/>/g, ">");
- }
- for(var x in data.msgs)
- {
- s = htmlEncode(data.msgs[x]) + "<BR>" + s;
- }
- $("#logs").html(s+$("#logs").html());
- var failFunction;
- failFunction = function(){
- $.getJSON("/logs/"+lastLog, updateLog).fail(failFunction);
- };
- $.getJSON("/logs/"+lastLog, updateLog).fail(failFunction);
- }
- $.getJSON("/logs", updateLog);
- });
- </script>
- </body>
- </html>
|