template.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import "html/template"
  3. var html = template.Must(template.New("chat_room").Parse(`
  4. <html>
  5. <head>
  6. <title>{{.roomid}}</title>
  7. <link rel="stylesheet" type="text/css" href="http://meyerweb.com/eric/tools/css/reset/reset.css"/>
  8. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
  9. <script src="http://malsup.github.com/jquery.form.js"></script>
  10. <script>
  11. $('#message_form').focus();
  12. $(document).ready(function() {
  13. // bind 'myForm' and provide a simple callback function
  14. $('#myForm').ajaxForm(function() {
  15. $('#message_form').val('');
  16. $('#message_form').focus();
  17. });
  18. if (!!window.EventSource) {
  19. var source = new EventSource('/stream/{{.roomid}}');
  20. source.addEventListener('message', function(e) {
  21. $('#messages').append(e.data + "</br>");
  22. $('html, body').animate({scrollTop:$(document).height()}, 'slow');
  23. }, false);
  24. } else {
  25. alert("NOT SUPPORTED");
  26. }
  27. });
  28. </script>
  29. </head>
  30. <body>
  31. <h1>Welcome to {{.roomid}} room</h1>
  32. <div id="messages"></div>
  33. <form id="myForm" action="/room/{{.roomid}}" method="post">
  34. User: <input id="user_form" name="user" value="{{.userid}}"></input>
  35. Message: <input id="message_form" name="message"></input>
  36. <input type="submit" value="Submit" />
  37. </form>
  38. </body>
  39. </html>
  40. `))