index.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <html>
  2. <head>
  3. <title>Basic Chat Page</title>
  4. <style type="text/css">
  5. textarea
  6. {
  7. width: 100%;
  8. resize: none;
  9. min-height: 2em;
  10. }
  11. .italics
  12. {
  13. font-style: italic;
  14. }
  15. </style>
  16. <script src="socket.io/socket.io.js"></script>
  17. <script type="text/javascript">
  18. var user;
  19. var socket = io.connect(window.location.href);
  20. socket.on('notification', function (data)
  21. {
  22. var elm = document.getElementById("display");
  23. elm.innerHTML = elm.innerHTML + data;
  24. });
  25. function init()
  26. {
  27. do
  28. {
  29. user = window.prompt("Enter a username here (Minimum 2 chars).");
  30. }
  31. while (user == null || user.trim().length < 2);
  32. ajax({username: user});
  33. }
  34. function autoGrow(element)
  35. {
  36. element.style.height = (element.scrollHeight)+"px";
  37. }
  38. function submit()
  39. {
  40. var elm = document.getElementById("userinput");
  41. ajax({
  42. username: user,
  43. message: elm.value
  44. });
  45. elm.value = "";
  46. }
  47. function ajax(data, fn)
  48. {
  49. /* var xhttp = new XMLHttpRequest();
  50. if (fn != undefined)
  51. {
  52. xhttp.onreadystatechange = function()
  53. {
  54. if (xhttp.readyState == 4 && xhttp.status == 200)
  55. {
  56. fn(xhttp.responseText);
  57. }
  58. };
  59. }
  60. xhttp.open("POST", "index.html", true);
  61. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  62. xhttp.send(jsonToURL(data));*/
  63. socket.emit('notification', data);
  64. }
  65. function jsonToURL(data)
  66. {
  67. var result = "";
  68. for (key in data)
  69. {
  70. result += encodeURIComponent(key)+"="+encodeURIComponent(data[key])+"&";
  71. }
  72. return result;
  73. }
  74. </script>
  75. </head>
  76. <body onload="init();">
  77. <table style="width: 100%; height: 100%;">
  78. <tr>
  79. <td colspan="2" style="height: 100%;">
  80. <div id="display" style="height: 100%; border: 1px solid #000000;"></div>
  81. </td>
  82. </tr>
  83. <tr>
  84. <td style="width: 100%;">
  85. <textarea id="userinput" style="overflow: hidden;" onkeyup="autoGrow(this);" placeholder="Type your message here!"></textarea>
  86. </td>
  87. <td>
  88. <button type="button" id="enterButton" onclick="submit();">Enter</button>
  89. </td>
  90. </tr>
  91. </table>
  92. </body>
  93. </html>