OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / gorilla / websocket / examples / echo / server.go
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", "localhost: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         flag.Parse()
50         log.SetFlags(0)
51         http.HandleFunc("/echo", echo)
52         http.HandleFunc("/", home)
53         log.Fatal(http.ListenAndServe(*addr, nil))
54 }
55
56 var homeTemplate = template.Must(template.New("").Parse(`
57 <!DOCTYPE html>
58 <html>
59 <head>
60 <meta charset="utf-8">
61 <script>  
62 window.addEventListener("load", function(evt) {
63
64     var output = document.getElementById("output");
65     var input = document.getElementById("input");
66     var ws;
67
68     var print = function(message) {
69         var d = document.createElement("div");
70         d.innerHTML = message;
71         output.appendChild(d);
72     };
73
74     document.getElementById("open").onclick = function(evt) {
75         if (ws) {
76             return false;
77         }
78         ws = new WebSocket("{{.}}");
79         ws.onopen = function(evt) {
80             print("OPEN");
81         }
82         ws.onclose = function(evt) {
83             print("CLOSE");
84             ws = null;
85         }
86         ws.onmessage = function(evt) {
87             print("RESPONSE: " + evt.data);
88         }
89         ws.onerror = function(evt) {
90             print("ERROR: " + evt.data);
91         }
92         return false;
93     };
94
95     document.getElementById("send").onclick = function(evt) {
96         if (!ws) {
97             return false;
98         }
99         print("SEND: " + input.value);
100         ws.send(input.value);
101         return false;
102     };
103
104     document.getElementById("close").onclick = function(evt) {
105         if (!ws) {
106             return false;
107         }
108         ws.close();
109         return false;
110     };
111
112 });
113 </script>
114 </head>
115 <body>
116 <table>
117 <tr><td valign="top" width="50%">
118 <p>Click "Open" to create a connection to the server, 
119 "Send" to send a message to the server and "Close" to close the connection. 
120 You can change the message and send multiple times.
121 <p>
122 <form>
123 <button id="open">Open</button>
124 <button id="close">Close</button>
125 <p><input id="input" type="text" value="Hello world!">
126 <button id="send">Send</button>
127 </form>
128 </td><td valign="top" width="50%">
129 <div id="output"></div>
130 </td></tr></table>
131 </body>
132 </html>
133 `))