~dricottone/simple-chat

ref: 3fd5e03203d8708fcb3b2da80bc8b1fae0cbf338 simple-chat/server/main.go -rw-r--r-- 2.4 KiB
3fd5e032Dominic Ricottone Adding digital signatures. 2 years ago
                                                                                
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/gorilla/websocket"
)

type Client struct {
	chatroom *Chatroom
	conn     *websocket.Conn
	id       int
	queue    chan []byte
}

func (client *Client) read() {
	defer func() {
		client.chatroom.kill <- client
		client.conn.Close()
	}()

	for {
		//NOTE: not using msg type
		_, msg, err := client.conn.ReadMessage()
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
				log.Println("while reading message:", err)
			}
			break
		}
		client.chatroom.queue <-msg
	}
}

func (client *Client) write() {
	ticker := time.NewTicker(60 * time.Second)

	defer func() {
		ticker.Stop()
		client.chatroom.kill <-client
		client.conn.Close()
	}()

	for {
		select {
		case msg := <-client.queue:
			err := client.conn.WriteMessage(websocket.TextMessage, msg)
			if err != nil {
				log.Println("while writing message:", err)
				break
			}
		case <-ticker.C:
			//TODO: add keep-alive ping w/ client.conn.PingMessage
		}
	}
}

type Chatroom struct {
	clients map[*Client]bool
	queue   chan []byte
	kill    chan *Client
}

func (chatroom *Chatroom) introduce(conn *websocket.Conn) *Client {
	client := &Client{
		chatroom: chatroom,
		conn: conn,
		id: len(chatroom.clients),
		queue: make(chan []byte),
	}
	chatroom.clients[client] = true
	return client
}

func (chatroom *Chatroom) run() {
	for {
		select {
		case dead := <-chatroom.kill:
			delete(chatroom.clients, dead)
		case msg := <-chatroom.queue:
			for client := range chatroom.clients {
				client.queue <-msg
			}
		}
	}
}

func main() {
	// Run chatroom
	chatroom := &Chatroom{
		clients: make(map[*Client]bool),
		queue: make(chan []byte),
		kill: make(chan *Client),
	}
	go chatroom.run()

	// Configure websocket upgrader
	upgrader := websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			origin := r.Header.Get("Origin")
			return origin == "https://www.dominic-ricottone.com"
		},
	}

	// Handler for new connections
	http.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
		conn, err := upgrader.Upgrade(w, r, nil)
		if err != nil {
			log.Println("while upgrading connection:", err)
			return
		}

		client := chatroom.introduce(conn)
		go client.read()
		go client.write()
	})

	// Run server
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Println("while running server:", err)
	}
}