How does Go WebSocket handle concurrent connections?

PHPz
Release: 2024-06-04 09:02:00
Original
976 people have browsed it

Go WebSocket's approach to handling concurrent connections: Use a Goroutine for each connection. Connect communication through Channel. Use third-party libraries such as [gowebsocket](https://github.com/gobwas/ws), [gorilla/websocket](https://github.com/gorilla/websocket) to simplify processing.

Go WebSocket 如何处理并发连接?

How Go WebSocket handles concurrent connections

WebSocket is a full-duplex communication protocol that allows real-time two-way communication between clients and servers. The common way to handle WebSocket concurrent connections in the Go language is as follows:

1. Goroutine

A simple solution is to use a Goroutine for each connection. Goroutine is a lightweight thread in Go that can execute in parallel. When a new WebSocket connection is established, we can create a new Goroutine to handle it:

package main

import (
    "fmt"
    "net/http"
    "os"

    "github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{}

func main() {
    // WebSocket 服务器端口号
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // 对请求进行 WebSocket 升级
        conn, err := upgrader.Upgrade(w, r, nil)
        if err != nil {
            fmt.Println(err)
            return
        }

        // 创建一个 Goroutine 处理连接
        go handleConnection(conn)
    })

    http.ListenAndServe(":"+port, nil)
}

// handleConnection 处理一个 WebSocket 连接
func handleConnection(conn *websocket.Conn) {
    for {
        msgType, msg, err := conn.ReadMessage()
        if err != nil {
            fmt.Println(err)
            break
        }

        if msgType == websocket.TextMessage {
            // 处理文本消息
            fmt.Println("Received text message:", string(msg))
            if err := conn.WriteMessage(msgType, msg); err != nil {
                fmt.Println(err)
            }
        } else {
            // 处理其他类型的消息
        }
    }

    conn.Close()
}
Copy after login

2. Channel

Channel is used for communication in Go Concurrent channels. We can handle multiple connections through a Channel:

package main

import (
    "fmt"
    "net/http"
    "os"
    "sync"
    "time"

    "github.com/gorilla/websocket"
)

var (
    upgrader = websocket.Upgrader{}
    connections = make(chan *websocket.Conn, 100)
    wg sync.WaitGroup
)

func main() {
    // WebSocket 服务器端口号
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // 对请求进行 WebSocket 升级
        conn, err := upgrader.Upgrade(w, r, nil)
        if err != nil {
            fmt.Println(err)
            return
        }

        // 将连接添加到 Channel
        connections <- conn
    })

    // 启动一个 Goroutine 处理 Channel 中的连接
    go handleConnections()

    http.ListenAndServe(":"+port, nil)
}

// handleConnections 处理 Channel 中的 WebSocket 连接
func handleConnections() {
    for {
        conn := <-connections

        wg.Add(1)
        go func() {
            defer wg.Done()
            for {
                msgType, msg, err := conn.ReadMessage()
                if err != nil {
                    fmt.Println(err)
                    break
                }

                if msgType == websocket.TextMessage {
                    // 处理文本消息
                    fmt.Println("Received text message:", string(msg))

                    // 向所有连接的客户端广播消息
                    for c := range connections {
                        if c != conn {
                            if err := c.WriteMessage(msgType, msg); err != nil {
                                fmt.Println(err)
                            }
                        }
                    }
                } else {
                    // 处理其他类型的消息
                }
            }

            conn.Close()
        }()
    }
}
Copy after login

Passing the connection through the pipeline can share the connection between all Goroutines and avoid the overhead caused by creating too many threads.

3. Third-party libraries

There are many third-party libraries that can simplify WebSocket concurrent processing, for example:

  • [gowebsocket]( https://github.com/gobwas/ws)
  • [gorilla/websocket](https://github.com/gorilla/websocket)
  • [fasthttp/websocket](https: //github.com/valyala/fasthttp/blob/master/websocket/websocket.go)

These libraries provide a high-level API to handle concurrent connections and simplify the use of WebSocket.

The above is the detailed content of How does Go WebSocket handle concurrent connections?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
go
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template