-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
38 lines (34 loc) · 883 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
func main() {
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("error: failed to upgrades the HTTP server connection to the WebSocket protocol: %v\n", err)
return
}
for {
typ, msg, err := conn.ReadMessage()
if err != nil {
log.Printf("error: failed to read message from client: %v\n", err)
return
}
if err := conn.WriteMessage(typ, msg); err != nil {
log.Printf("error: failed to write message to client: %v\n", err)
return
}
}
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.ListenAndServe(":8080", nil)
}