/tags/1.2/src/main/webapp/example/chat.html

http://jquery-stream.googlecode.com/ · HTML · 83 lines · 76 code · 7 blank · 0 comment · 0 complexity · e5e8019c06d84634925653ba6c24ad29 MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Chat</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript" src="../jquery.js"></script>
  7. <script type="text/javascript" src="../jquery.stream.js"></script>
  8. <script type="text/javascript">
  9. $.stream.setup({enableXDR: true});
  10. var chat = {
  11. lastUsername: "Donghwan Kim",
  12. username: $.trim(window.prompt("Username?")) || "Anonymous" + $(window).width()
  13. };
  14. $(function() {
  15. $.stream("chat", {
  16. dataType: "json",
  17. context: $("#content")[0],
  18. open: function() {
  19. $("#editor .message").removeAttr("disabled").focus();
  20. $.stream().send({username: chat.username, message: "Hello"});
  21. },
  22. message: function(event) {
  23. if (chat.lastUsername !== event.data.username) {
  24. $("<p />").addClass("user").text(chat.lastUsername = event.data.username).appendTo(this);
  25. }
  26. $("<p />").addClass("message").text(event.data.message).appendTo(this);
  27. this.scrollTop = this.scrollHeight;
  28. },
  29. error: function() {
  30. $("#editor .message").attr("disabled", "disabled");
  31. },
  32. close: function() {
  33. $("#editor .message").attr("disabled", "disabled");
  34. }
  35. });
  36. $("#editor .user").text(chat.username);
  37. $("#editor .message").keyup(function(event) {
  38. if (event.which === 13 && $.trim(this.value)) {
  39. $.stream().send({username: chat.username, message: this.value});
  40. this.value = "";
  41. }
  42. });
  43. $(window).resize(function() {
  44. $("#content")
  45. .height($(window).height() - $("#editor").outerHeight(true) - 15)
  46. .each(function() {
  47. this.scrollTop = this.scrollHeight;
  48. });
  49. }).resize();
  50. });
  51. </script>
  52. <style>
  53. body {padding: 0; margin: 0; min-width: 320px; font-family: 'Trebuchet MS','Malgun Gothic',Verdana,Helvetica,Arial,sans-serif; font-size: 62.5%; color: #333333}
  54. .content {height: 100%; overflow-y: auto; padding: 14px 15px 0 25px;}
  55. .content p {margin: 0; padding: 0;}
  56. .content .user {font-size: 1.8em; color: #3e3e3e; font-weight: bold; letter-spacing: -1px; margin-top: 0.5em;}
  57. .content .message {font-size: 1.3em; color: #444444; line-height: 1.7em; word-wrap: break-word;}
  58. .editor {margin: 0 25px 15px 25px;}
  59. .editor .user {font-size: 1.5em; display: inline-block; margin: 1em;}
  60. .editor input {font-family: 'Trebuchet MS','Malgun Gothic',Verdana,Helvetica,Arial,sans-serif;}
  61. .editor .message {width: 100%; height: 28px; line-height: 28px; border: medium none; border-color: #E5E5E5 #DBDBDB #D2D2D2; border-style: solid; border-width: 1px;}
  62. </style>
  63. </head>
  64. <body>
  65. <div id="content" class="content">
  66. <p class="user"><span>Donghwan Kim</span></p>
  67. <p class="message">Welcome to jQuery Stream!</p>
  68. </div>
  69. <div id="editor" class="editor">
  70. <p class="user"></p>
  71. <form action="#" onsubmit="return false;">
  72. <input class="message" type="text" disabled="disabled" />
  73. </form>
  74. </div>
  75. </body>
  76. </html>