index.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <html>
  2. <head>
  3. <script src="/lws-common.js" nonce="lwscaro"></script>
  4. <script src="lwsgs.js" nonce="lwscaro"></script>
  5. <style>
  6. .body { font-size: 12 }
  7. .gstitle { font-size: 18 }
  8. .group1 { vertical-align:middle;text-align:center;background:#f0f0e0;
  9. padding:12px; -webkit-border-radius:10px;
  10. -moz-border-radius:10px;border-radius:10px; }
  11. .group2 { vertical-align:middle; font-size: 22;text-align:center;
  12. margin:auto; align:center;
  13. background-color: rgba(255, 255, 255, 0.8); padding:12px;
  14. display:inline-block; -webkit-border-radius:10px;
  15. -moz-border-radius:10px; border-radius:10px; }
  16. </style>
  17. </head>
  18. <body style="background-image:url(seats.jpg)">
  19. <table style="width:100%;height:100%;transition: max-height 2s;">
  20. <tr>
  21. <td style="vertical-align:top;text-align:left;width=200px">
  22. <img src="lwsgs-logo.png">
  23. </td>
  24. <td style="vertical-align:top;float:right">
  25. <div id=lwsgs style="zIndex: 1000;text-align:right;background-color: rgba(255, 255, 255, 0.8);"></div>
  26. </td>
  27. </tr>
  28. <tr><td colspan=2 style="height:99%;vertical-align:middle;">
  29. <table style="text-align:center;width:100%"><tr>
  30. <td style="margin:auto;align:center">
  31. <span id="nolog" class="group2" style="display:none;">
  32. This is a demo application for lws generic-sessions.<br><br>
  33. It's a simple messageboard.<br><br>
  34. What's interesting about it is there is <b>no serverside scripting</b>,<br>
  35. instead client js makes a wss:// connection back to the server<br>
  36. and then reacts to JSON from the ws protocol. Sessions stuff is <br>
  37. handled by lws generic sessions, making the <a href="https://github.com/warmcat/libwebsockets/blob/master/plugins/generic-sessions/protocol_lws_messageboard.c">actual<br>
  38. test application</a> <a href="https://github.com/warmcat/libwebsockets/blob/master/plugins/generic-sessions/index.html">very small</a>.<br><br>
  39. And because it's natively websocket, it's naturally connected<br>
  40. for dynamic events and easy to maintain.
  41. <br><br>
  42. Register / Login at the top right to see and create new messages.
  43. </span>
  44. <span id="logged" class="group2" style="display:none">
  45. <div id="newmsg">
  46. <form action="msg" method="post" target="hidden">
  47. New message<br>
  48. <textarea id="msg" placeholder="type your message here" cols="40" rows="5" name="msg"></textarea><br>
  49. <input type="submit" id="send" name="send" disabled=1>
  50. </form>
  51. </div>
  52. </span>
  53. <div id="dmessages">
  54. <span id="messages" ></span>
  55. </div>
  56. <span id="debug" class="group2"></span>
  57. </td></tr></table>
  58. </td></tr>
  59. </table>
  60. </form>
  61. <iframe name="hidden" style="display:none"></iframe>
  62. <script nonce="lwscaro">lwsgs_initial();
  63. document.getElementById("nolog").style.display = !!lwsgs_user ? "none" : "inline-block";
  64. document.getElementById("logged").style.display = !lwsgs_user ? "none" : "inline-block";
  65. document.getElementById("msg").onkeyup = mupd;
  66. document.getElementById("msg").onchange = mupd;
  67. var ws;
  68. function mb_format(s)
  69. {
  70. var r = "", n, wos = 0;
  71. for (n = 0; n < s.length; n++) {
  72. if (s[n] == ' ')
  73. wos = 0;
  74. else {
  75. wos++;
  76. if (wos == 40) {
  77. wos = 0;
  78. r = r + ' ';
  79. }
  80. }
  81. if (s[n] == '<') {
  82. r = r + "&lt;";
  83. continue;
  84. }
  85. if (s[n] == '\n') {
  86. r = r + "<br>";
  87. continue;
  88. }
  89. r = r + s[n];
  90. }
  91. return r;
  92. }
  93. function add_div(n, m)
  94. {
  95. var q = document.getElementById(n);
  96. var d = new Date(m.time * 1000);
  97. q.innerHTML = "<br><div style=\"margin:2px\" class=\"group2\"><table style=\"table-layout: fixed;\"><tr><td>" +
  98. "<img src=\"https://www.gravatar.com/avatar/" + md5(m.email) +
  99. "?d=identicon\"><br>" +
  100. "<b>" + lwsgs_san(m.username) + "</b><br>" +
  101. "<span style=\"font-size:8pt\">" + d.toDateString() +
  102. "<br>" + d.toTimeString() + "</span><br>" +
  103. "IP: " + lwsgs_san(m.ip) +
  104. "</td><td style=\"display:inline-block;vertical-align:top;word-wrap:break-word;\"><span>" +
  105. mb_format(m.content) +
  106. "</span></td></tr></table></div><br>" + q.innerHTML;
  107. }
  108. function get_appropriate_ws_url()
  109. {
  110. var pcol;
  111. var u = document.URL;
  112. if (u.substring(0, 5) == "https") {
  113. pcol = "wss://";
  114. u = u.substr(8);
  115. } else {
  116. pcol = "ws://";
  117. if (u.substring(0, 4) == "http")
  118. u = u.substr(7);
  119. }
  120. u = u.split('/');
  121. return pcol + u[0] + "/xxx";
  122. }
  123. if (lwsgs_user) {
  124. if (typeof MozWebSocket != "undefined")
  125. ws = new MozWebSocket(get_appropriate_ws_url(),
  126. "protocol-lws-messageboard");
  127. else
  128. ws = new WebSocket(get_appropriate_ws_url(),
  129. "protocol-lws-messageboard");
  130. try {
  131. ws.onopen = function() {
  132. document.getElementById("debug").textContent = "ws opened";
  133. }
  134. ws.onmessage =function got_packet(msg) {
  135. add_div("messages", JSON.parse(msg.data));
  136. }
  137. ws.onclose = function(){
  138. }
  139. } catch(exception) {
  140. alert('<p>Error' + exception);
  141. }
  142. }
  143. function mupd()
  144. {
  145. document.getElementById("send").disabled = !document.getElementById("msg").value;
  146. }
  147. </script>
  148. </body>
  149. </html>