├── Dockerfile
├── go.mod
├── go.sum
├── index.html
└── main.go
/Dockerfile:
--------------------------------------------------------------------------------
1 | # We'll choose the incredibly lightweight
2 | # Go alpine image to work with
3 | FROM golang:1.11.1 AS builder
4 |
5 | # We want to build and run
6 | # our application's binary executable
7 | RUN mkdir /app
8 | ADD . /app
9 | WORKDIR /app
10 | RUN CGO_ENABLED=0 GOOS=linux go build -o main ./...
11 |
12 | # the lightweight scratch image we'll
13 | # run our application within
14 | FROM alpine:latest AS Production
15 | COPY --from=builder /app .
16 | CMD ["./main"]
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/tutorialedge/go/go-websocket-tutorial
2 |
3 | require github.com/gorilla/websocket v1.4.0
4 |
--------------------------------------------------------------------------------
/go.sum:
--------------------------------------------------------------------------------
1 | github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
2 | github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
3 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Go WebSocket Tutorial
8 |
9 |
10 | Hello World
11 |
12 |
31 |
32 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "log"
6 | "net/http"
7 |
8 | "github.com/gorilla/websocket"
9 | )
10 |
11 | // We'll need to define an Upgrader
12 | // this will require a Read and Write buffer size
13 | var upgrader = websocket.Upgrader{
14 | ReadBufferSize: 1024,
15 | WriteBufferSize: 1024,
16 | CheckOrigin: func(r *http.Request) bool { return true },
17 | }
18 |
19 | // define a reader which will listen for
20 | // new messages being sent to our WebSocket
21 | // endpoint
22 | func reader(conn *websocket.Conn) {
23 | for {
24 | // read in a message
25 | messageType, p, err := conn.ReadMessage()
26 | if err != nil {
27 | log.Println(err)
28 | return
29 | }
30 | // print out that message for clarity
31 | log.Println(string(p))
32 |
33 | if err := conn.WriteMessage(messageType, p); err != nil {
34 | log.Println(err)
35 | return
36 | }
37 |
38 | }
39 | }
40 |
41 | func homePage(w http.ResponseWriter, r *http.Request) {
42 | fmt.Fprintf(w, "Home Page")
43 | }
44 |
45 | func wsEndpoint(w http.ResponseWriter, r *http.Request) {
46 | // upgrade this connection to a WebSocket
47 | // connection
48 | ws, err := upgrader.Upgrade(w, r, nil)
49 | if err != nil {
50 | log.Println(err)
51 | }
52 |
53 | log.Println("Client Connected")
54 | err = ws.WriteMessage(1, []byte("Hi Client!"))
55 | if err != nil {
56 | log.Println(err)
57 | }
58 | // listen indefinitely for new messages coming
59 | // through on our WebSocket connection
60 | reader(ws)
61 | }
62 |
63 | func setupRoutes() {
64 | http.HandleFunc("/", homePage)
65 | http.HandleFunc("/ws", wsEndpoint)
66 | }
67 |
68 | func main() {
69 | fmt.Println("Hello World")
70 | setupRoutes()
71 | log.Fatal(http.ListenAndServe(":8080", nil))
72 | }
73 |
--------------------------------------------------------------------------------