server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.innerHTML = message;
  59. output.appendChild(d);
  60. };
  61. document.getElementById("open").onclick = function(evt) {
  62. if (ws) {
  63. return false;
  64. }
  65. ws = new WebSocket("{{.}}");
  66. ws.onopen = function(evt) {
  67. print("OPEN");
  68. }
  69. ws.onclose = function(evt) {
  70. print("CLOSE");
  71. ws = null;
  72. }
  73. ws.onmessage = function(evt) {
  74. print("RESPONSE: " + evt.data);
  75. }
  76. ws.onerror = function(evt) {
  77. print("ERROR: " + evt.data);
  78. }
  79. return false;
  80. };
  81. document.getElementById("send").onclick = function(evt) {
  82. if (!ws) {
  83. return false;
  84. }
  85. print("SEND: " + input.value);
  86. ws.send(input.value);
  87. return false;
  88. };
  89. document.getElementById("close").onclick = function(evt) {
  90. if (!ws) {
  91. return false;
  92. }
  93. ws.close();
  94. return false;
  95. };
  96. });
  97. </script>
  98. </head>
  99. <body>
  100. <table>
  101. <tr><td valign="top" width="50%">
  102. <p>Click "Open" to create a connection to the server,
  103. "Send" to send a message to the server and "Close" to close the connection.
  104. You can change the message and send multiple times.
  105. <p>
  106. <form>
  107. <button id="open">Open</button>
  108. <button id="close">Close</button>
  109. <p><input id="input" type="text" value="Hello world!">
  110. <button id="send">Send</button>
  111. </form>
  112. </td><td valign="top" width="50%">
  113. <div id="output"></div>
  114. </td></tr></table>
  115. </body>
  116. </html>
  117. `))