server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 The Gorilla WebSocket Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "flag"
  8. "html/template"
  9. "log"
  10. "net/http"
  11. "github.com/gorilla/websocket"
  12. )
  13. var addr = flag.String("addr", "localhost:8080", "http service address")
  14. var upgrader = websocket.Upgrader{} // use default options
  15. func echo(w http.ResponseWriter, r *http.Request) {
  16. c, err := upgrader.Upgrade(w, r, nil)
  17. if err != nil {
  18. log.Print("upgrade:", err)
  19. return
  20. }
  21. defer c.Close()
  22. for {
  23. mt, message, err := c.ReadMessage()
  24. if err != nil {
  25. log.Println("read:", err)
  26. break
  27. }
  28. log.Printf("recv: %s", message)
  29. err = c.WriteMessage(mt, message)
  30. if err != nil {
  31. log.Println("write:", err)
  32. break
  33. }
  34. }
  35. }
  36. func home(w http.ResponseWriter, r *http.Request) {
  37. homeTemplate.Execute(w, "ws://"+r.Host+"/echo")
  38. }
  39. func main() {
  40. flag.Parse()
  41. log.SetFlags(0)
  42. http.HandleFunc("/echo", echo)
  43. http.HandleFunc("/", home)
  44. log.Fatal(http.ListenAndServe(*addr, nil))
  45. }
  46. var homeTemplate = template.Must(template.New("").Parse(`
  47. <!DOCTYPE html>
  48. <html>
  49. <head>
  50. <meta charset="utf-8">
  51. <script>
  52. window.addEventListener("load", function(evt) {
  53. var output = document.getElementById("output");
  54. var input = document.getElementById("input");
  55. var ws;
  56. var print = function(message) {
  57. var d = document.createElement("div");
  58. d.textContent = message;
  59. output.appendChild(d);
  60. output.scroll(0, output.scrollHeight);
  61. };
  62. document.getElementById("open").onclick = function(evt) {
  63. if (ws) {
  64. return false;
  65. }
  66. ws = new WebSocket("{{.}}");
  67. ws.onopen = function(evt) {
  68. print("OPEN");
  69. }
  70. ws.onclose = function(evt) {
  71. print("CLOSE");
  72. ws = null;
  73. }
  74. ws.onmessage = function(evt) {
  75. print("RESPONSE: " + evt.data);
  76. }
  77. ws.onerror = function(evt) {
  78. print("ERROR: " + evt.data);
  79. }
  80. return false;
  81. };
  82. document.getElementById("send").onclick = function(evt) {
  83. if (!ws) {
  84. return false;
  85. }
  86. print("SEND: " + input.value);
  87. ws.send(input.value);
  88. return false;
  89. };
  90. document.getElementById("close").onclick = function(evt) {
  91. if (!ws) {
  92. return false;
  93. }
  94. ws.close();
  95. return false;
  96. };
  97. });
  98. </script>
  99. </head>
  100. <body>
  101. <table>
  102. <tr><td valign="top" width="50%">
  103. <p>Click "Open" to create a connection to the server,
  104. "Send" to send a message to the server and "Close" to close the connection.
  105. You can change the message and send multiple times.
  106. <p>
  107. <form>
  108. <button id="open">Open</button>
  109. <button id="close">Close</button>
  110. <p><input id="input" type="text" value="Hello world!">
  111. <button id="send">Send</button>
  112. </form>
  113. </td><td valign="top" width="50%">
  114. <div id="output" style="max-height: 70vh;overflow-y: scroll;"></div>
  115. </td></tr></table>
  116. </body>
  117. </html>
  118. `))