1m-go-websockets 项目教程

1m-go-websockets 项目教程

1m-go-websocketshandling 1M websockets connections in Go项目地址:https://gitcode.com/gh_mirrors/1m/1m-go-websockets

1. 项目的目录结构及介绍

1m-go-websockets/
├── 0_simple_web_server/
├── 1_ws_example/
├── 2_ws_ulimit/
├── 3_optimize_ws_goroutines/
├── 4_optimize_gobwas/
├── LICENSE
├── README.md
├── client.go
├── destroy.sh
├── go.mod
├── go.sum
├── pprof_goroutine.sh
├── pprof_heap.sh
└── setup.sh
  • 0_simple_web_server/: 简单的 Web 服务器示例。
  • 1_ws_example/: WebSocket 示例。
  • 2_ws_ulimit/: 调整 ulimit 的示例。
  • 3_optimize_ws_goroutines/: 优化 WebSocket 的 Goroutine 示例。
  • 4_optimize_gobwas/: 使用 gobwas 库优化 WebSocket 的示例。
  • LICENSE: 项目许可证(AGPL-3.0)。
  • README.md: 项目说明文档。
  • client.go: WebSocket 客户端文件。
  • destroy.sh: 销毁脚本。
  • go.mod: Go 模块文件。
  • go.sum: Go 模块校验文件。
  • pprof_goroutine.sh: 用于分析 Goroutine 的 pprof 脚本。
  • pprof_heap.sh: 用于分析堆内存的 pprof 脚本。
  • setup.sh: 设置脚本。

2. 项目的启动文件介绍

client.go 是项目的启动文件,主要用于创建和管理 WebSocket 连接。以下是 client.go 的主要内容:

package main

import (
    "flag"
    "fmt"
    "github.com/gorilla/websocket"
    "io"
    "log"
    "net/url"
    "os"
    "time"
)

var (
    ip = flag.String("ip", "127.0.0.1", "server IP")
    connections = flag.Int("conn", 1, "number of websocket connections")
)

func main() {
    flag.Usage = func() {
        io.WriteString(os.Stderr, `Websockets client generator
Example usage: ./client -ip=172.17.0.1 -conn=10`)
        flag.PrintDefaults()
    }
    flag.Parse()

    u := url.URL{Scheme: "ws", Host: *ip + ":8000", Path: "/"}
    log.Printf("Connecting to %s", u.String())

    var conns []*websocket.Conn
    for i := 0; i < *connections; i++ {
        c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
        if err != nil {
            fmt.Println("Failed to connect", i, err)
            break
        }
        conns = append(conns, c)
        defer func() {
            c.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), time.Now().Add(time.Second))
            time.Sleep(time.Second)
            c.Close()
        }()
    }

    log.Printf("Finished initializing %d connections", len(conns))
    tts := time.Second
    if *connections > 100 {
        tts = time.Millisecond * 5
    }
    for {
        for i := 0; i < len(conns); i++ {
            time.Sleep(tts)
            conn := conns[i]
            log.Printf("Conn %d sending message", i)
            if err := conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(time.Second*5)); err != nil {
                fmt.Printf("Failed to receive pong: %v", err)
            }
            conn.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Hello from conn %v", i)))
        }
    }
}

3. 项目的配置

1m-go-websocketshandling 1M websockets connections in Go项目地址:https://gitcode.com/gh_mirrors/1m/1m-go-websockets

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滑姗珊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值