gorilla/websocket的chat示例代码简单分析

代码地址:https://github.com/gorilla/websocket/tree/main/examples/chat

文件包含:main.go、hub.go、client.go、home.html

main.go文件

Go 复制代码
func main() {
	flag.Parse()
	hub := newHub() // 实例化Hub
	go hub.run() // 使用chan处理 增删Hub的连接 和 广播消息

	http.HandleFunc("/", serveHome) // 访问home.html页面

    // 处理websocket
	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		serveWs(hub, w, r)
	})

	server := &http.Server{
		Addr:              *addr,
		ReadHeaderTimeout: 3 * time.Second,
	}
	err := server.ListenAndServe()
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

hub.go文件

Go 复制代码
// 相当于连接池
type Hub struct {
	// Registered clients.
	clients map[*Client]bool // 存放所有websocket连接

	// Inbound messages from the clients.
	broadcast chan []byte // 广播消息

	// Register requests from the clients.
	register chan *Client // 添加websocket连接

	// Unregister requests from clients.
	unregister chan *Client // 删除websocket连接
}

// 实例化Hub
func newHub() *Hub {
	return &Hub{
		broadcast:  make(chan []byte),
		register:   make(chan *Client),
		unregister: make(chan *Client),
		clients:    make(map[*Client]bool),
	}
}

// 使用chan处理 增删Hub的连接 和 广播消息
func (h *Hub) run() {
	for {
		select {
		case client := <-h.register:
			h.clients[client] = true // 添加连接
		case client := <-h.unregister:
			if _, ok := h.clients[client]; ok {
				delete(h.clients, client) // 删除连接
				close(client.send)
			}
		case message := <-h.broadcast:
			for client := range h.clients {
				select {
				case client.send <- message: // 广播消息
				default:
					close(client.send)
					delete(h.clients, client)
				}
			}
		}
	}
}

client.go文件

Go 复制代码
// 连接
type Client struct {
	hub *Hub // 引用Hub

	// The websocket connection.
	conn *websocket.Conn // websocket连接

	// Buffered channel of outbound messages.
	send chan []byte // 消息发送chan
}

// readPump pumps messages from the websocket connection to the hub.
//
// The application runs readPump in a per-connection goroutine. The application
// ensures that there is at most one reader on a connection by executing all
// reads from this goroutine.
func (c *Client) readPump() { // 读数据
	defer func() {
		c.hub.unregister <- c
		c.conn.Close()
	}()
	c.conn.SetReadLimit(maxMessageSize)
	c.conn.SetReadDeadline(time.Now().Add(pongWait))
	c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
	for {
		_, message, err := c.conn.ReadMessage() // 接收消息
		if err != nil {
			if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
				log.Printf("error: %v", err)
			}
			break
		}
		message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
		c.hub.broadcast <- message // 将消息发送到广播消息chan
	}
}

// writePump pumps messages from the hub to the websocket connection.
//
// A goroutine running writePump is started for each connection. The
// application ensures that there is at most one writer to a connection by
// executing all writes from this goroutine.
func (c *Client) writePump() { // 写数据
	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		c.conn.Close()
	}()
	for {
		select {
		case message, ok := <-c.send: // 从连接的chan接收消息
			c.conn.SetWriteDeadline(time.Now().Add(writeWait))
			if !ok {
				// The hub closed the channel.
				c.conn.WriteMessage(websocket.CloseMessage, []byte{})
				return
			}

			w, err := c.conn.NextWriter(websocket.TextMessage)
			if err != nil {
				return
			}
			w.Write(message) // 发送消息

			// Add queued chat messages to the current websocket message.
			n := len(c.send)
			for i := 0; i < n; i++ {
				w.Write(newline)
				w.Write(<-c.send)
			}

			if err := w.Close(); err != nil {
				return
			}
		case <-ticker.C:
			c.conn.SetWriteDeadline(time.Now().Add(writeWait))
			if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
				return
			}
		}
	}
}

// serveWs handles websocket requests from the peer.
func serveWs(hub *Hub, w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println(err)
		return
	}

    // 初始化Client
	client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
	client.hub.register <- client

	// Allow collection of memory referenced by the caller by doing all work in
	// new goroutines.
	go client.writePump() // 写数据
	go client.readPump() // 读数据
}

其他chat参考:

https://github.com/android-coco/chat

https://github.com/GoLangFengShen/chat

https://www.golangblogs.com/read/im/date-2023.02.19.09.38.24

相关推荐
Wang's Blog25 分钟前
RocketMQ: Broker 使用指南
服务器·网络·rocketmq
北'辰1 小时前
使用ENSP实现DHCP
运维·网络
丁总学Java2 小时前
netstat -tuln | grep 27017(显示所有监听状态的 TCP 和 UDP 端口,并且以数字形式显示地址和端口号)
网络协议·tcp/ip·udp
黑客K-ing2 小时前
开源网络安全检测工具——伏羲 Fuxi-Scanner
网络·数据库·web安全
网络安全-老纪3 小时前
AWS云安全
网络·云计算·aws
fanxiaohui121383 小时前
浪潮信息自动驾驶框架AutoDRRT 2.0,赋能高阶自动驾驶
运维·服务器·网络·人工智能·机器学习·金融·自动驾驶
老码沉思录3 小时前
Android开发实战班 -网络编程 - Retrofit 网络请求 + OkHttp 使用详解
android·网络·retrofit
Winston Wood3 小时前
一文学习开源框架OkHttp
网络·okhttp
applebomb3 小时前
【vue3+Typescript】unapp+stompsj模式下替代plus-websocket的封装模块
websocket·typescript·vue·uniapp·plus-websocket
钰爱&4 小时前
【操作系统】Linux之网络编程(TCP)(头歌作业)
linux·网络·tcp/ip