Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.04 KB | None | 0 0
  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.  
  5. // +build ignore
  6.  
  7. package main
  8.  
  9. import (
  10.     "flag"
  11.     "html/template"
  12.     "log"
  13.     "net/http"
  14.  
  15.     "github.com/gorilla/websocket"
  16. )
  17.  
  18. var addr = flag.String("addr", "127.0.0.1:8080", "http service address")
  19.  
  20. var upgrader = websocket.Upgrader{} // use default options
  21.  
  22. func echo(w http.ResponseWriter, r *http.Request) {
  23.     c, err := upgrader.Upgrade(w, r, nil)
  24.     if err != nil {
  25.         log.Print("upgrade:", err)
  26.         return
  27.     }
  28.     defer c.Close()
  29.     for {
  30.         mt, message, err := c.ReadMessage()
  31.         if err != nil {
  32.             log.Println("read:", err)
  33.             break
  34.         }
  35.         log.Printf("recv: %s", message)
  36.         err = c.WriteMessage(mt, message)
  37.         if err != nil {
  38.             log.Println("write:", err)
  39.             break
  40.         }
  41.     }
  42. }
  43.  
  44. func home(w http.ResponseWriter, r *http.Request) {
  45.     homeTemplate.Execute(w, "ws://"+r.Host+"/echo")
  46. }
  47.  
  48. func main() {
  49.  
  50.     flag.Parse()
  51.     log.SetFlags(0)
  52.     http.HandleFunc("/echo", echo)
  53.     http.HandleFunc("/", home)
  54.     log.Fatal(http.ListenAndServe(*addr, nil))
  55. }
  56.  
  57. var homeTemplate = template.Must(template.New("").Parse(`
  58. <!DOCTYPE html>
  59. <html>
  60. <head>
  61. <meta charset="utf-8">
  62. <script>  
  63. window.addEventListener("load", function(evt) {
  64.  
  65.     var output = document.getElementById("output");
  66.     var input = document.getElementById("input");
  67.     var ws;
  68.  
  69.     var print = function(message) {
  70.         var d = document.createElement("div");
  71.         d.innerHTML = message;
  72.         output.appendChild(d);
  73.     };
  74.  
  75.     document.getElementById("open").onclick = function(evt) {
  76.         if (ws) {
  77.             return false;
  78.         }
  79.         ws = new WebSocket("{{.}}");
  80.         ws.onopen = function(evt) {
  81.             print("OPEN");
  82.         }
  83.         ws.onclose = function(evt) {
  84.             print("CLOSE");
  85.             ws = null;
  86.         }
  87.         ws.onmessage = function(evt) {
  88.             print("RESPONSE: " + evt.data);
  89.         }
  90.         ws.onerror = function(evt) {
  91.             print("ERROR: " + evt.data);
  92.         }
  93.         return false;
  94.     };
  95.  
  96.     document.getElementById("send").onclick = function(evt) {
  97.         if (!ws) {
  98.             return false;
  99.         }
  100.         print("SEND: " + input.value);
  101.         ws.send(input.value);
  102.         return false;
  103.     };
  104.  
  105.     document.getElementById("close").onclick = function(evt) {
  106.         if (!ws) {
  107.             return false;
  108.         }
  109.         ws.close();
  110.         return false;
  111.     };
  112.  
  113. });
  114. </script>
  115. </head>
  116. <body>
  117. <table>
  118. <tr><td valign="top" width="50%">
  119. <p>Click "Open" to create a connection to the server,
  120. "Send" to send a message to the server and "Close" to close the connection.
  121. You can change the message and send multiple times.
  122. <p>
  123. <form>
  124. <button id="open">Open</button>
  125. <button id="close">Close</button>
  126. <p><input id="input" type="text" value="Hello world!">
  127. <button id="send">Send</button>
  128. </form>
  129. </td><td valign="top" width="50%">
  130. <div id="output"></div>
  131. </td></tr></table>
  132. </body>
  133. </html>
  134. `))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement