给Go的Gin web框架增加 WebSocket 功能,让WebSocket 更好用

简介: 给Go的Gin web框架增加 WebSocket 功能,让WebSocket 更好用

Gin 是一个 go 的 web 框架,它具有轻量级,高性能,运行速度快,分组的路由器,良好的崩溃捕获和错误处理,非常好的支持中间件,rest api和json。


总之在 Go语言开发领域是一款值得好好研究的 Web 框架。官方地址:https://github.com/gin-gonic/gin


但是最近想尝试下websocket功能,发现Gin框架里没有。


Go 官方没有提供对 WebSocket 的支持,必须选择第三方提供的包。


常用的有两种,golang.org/x/net/websocket和 https://github.com/gorilla/websocket


《Go Web 编程》一书中的例子使用的是golang.org/x/net/websocket


其中gorilla/websocket更常用些,Apache的Paho GO mqtt client库中和go的另外一个web框架iris中,就使用的是gorilla/websocket库。


是不是直接把gorilla/websocket引入进gin框架就可以了?


但是还得需要自己完成封装,若不花功夫封装好,不是很好用,稳定性更难保证。


网上找到gin中使用gorilla的websocket库的例子,都只是一些简单的测试,一点儿都不好用。


接下来,为了让websocket在gin中更好用些,移植iris框架中的websocket功能到gin框架中,使用起来就简单啦,使用如下:


github 地址: https://github.com/yangyongzhen/gin-websocket.git


package main
import (
  "fmt"
  //"github.com/gin-contrib/cors"
  "github.com/gin-gonic/gin"
  "net/http"
  "strings"
  "websockTest/websocket"
)
func main() {
  ws := websocket.New(websocket.Config{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
  })
  ws.OnConnection(handleConnection)
  r := gin.Default()
  //允许跨域
  //config := cors.DefaultConfig()
  //config.AllowOrigins = []string{"http://127.0.0.1:9090"}
  //r.Use(Cors())
  //静态资源
  r.Static("/static", "./static")
  r.LoadHTMLGlob("views/*")
  r.GET("/api/v3/device", ws.Handler())
  r.GET("/test", func(c *gin.Context) {
    c.HTML(http.StatusOK, "test.html", gin.H{
      "title": "this is a test",
    })
  })
  r.Run(":9090")
}
func handleConnection(c websocket.Connection) {
  fmt.Println("client connected,id=", c.ID())
  c.Write(1, []byte("welcome client"))
  // 测试从浏览器中读取事件
  c.On("chat", func(msg string) {
    // 将消息打印到控制台
    fmt.Printf("%s sent: %s\n", c.Context().ClientIP(), msg)
    // 将消息写回客户端消息所有者:
    // c.Emit("chat", msg)
    c.To(websocket.All).Emit("chat", msg)
  })
  c.OnMessage(func(msg []byte) {
    fmt.Println("received msg:", string(msg))
    c.Write(1, []byte("hello aa"))
    c.To(websocket.All).Emit("chat", msg)
  })
  c.OnDisconnect(func() {
    fmt.Println("client Disconnect,id=", c.ID())
  })
}


附,网上找到的一些测试代码如下:


package server
import (
  "fmt"
  "net"
  "net/http"
  "time"
  "github.com/gorilla/websocket"
)
const (
  // Time allowed to write a message to the peer.
  writeWait = 10 * time.Second
  // Maximum message size allowed from peer.
  maxMessageSize = 8192
  // Time allowed to read the next pong message from the peer.
  pongWait = 60 * time.Second
)
type WsServer struct {
  listener net.Listener
  addr     string
  upgrade  *websocket.Upgrader
}
func NewWsServer() *WsServer {
  ws := new(WsServer)
  ws.addr = "0.0.0.0:9090"
  ws.upgrade = &websocket.Upgrader{
    ReadBufferSize:  4096,
    WriteBufferSize: 1024,
    CheckOrigin: func(r *http.Request) bool {
      if r.Method != "GET" {
        fmt.Println("method is not GET")
        return false
      }
      if r.URL.Path != "/ws" {
        fmt.Println("path error")
        return false
      }
      return true
    },
  }
  return ws
}
func (self *WsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  if r.URL.Path != "/ws" {
    httpCode := http.StatusInternalServerError
    reasePhrase := http.StatusText(httpCode)
    fmt.Println("path error ", reasePhrase)
    fmt.Println("path error,url=", r.URL.Path)
    http.Error(w, reasePhrase, httpCode)
    return
  }
  conn, err := self.upgrade.Upgrade(w, r, nil)
  if err != nil {
    fmt.Println("websocket error:", err)
    return
  }
  fmt.Println("client connect :", conn.RemoteAddr())
  go self.connHandle(conn)
}
func (self *WsServer) connHandle(conn *websocket.Conn) {
  defer func() {
    conn.Close()
  }()
  stopCh := make(chan int)
  go self.send(conn, stopCh)
  conn.SetReadLimit(maxMessageSize)
  conn.SetReadDeadline(time.Now().Add(pongWait))
  conn.SetPongHandler(func(string) error {
    fmt.Println("this is PongHandler")
    conn.SetReadDeadline(time.Now().Add(pongWait))
    if err := conn.WriteMessage(websocket.PongMessage, nil); err != nil {
      fmt.Println("resp pong error", err)
    } else {
      fmt.Println("resp pong ok")
    }
    return nil
  })
  for {
    conn.SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(60000)))
    _, msg, err := conn.ReadMessage()
    if err != nil {
      // 判断是不是超时
      if netErr, ok := err.(net.Error); ok {
        if netErr.Timeout() {
          fmt.Printf("ReadMessage timeout remote: %v\n", conn.RemoteAddr())
          close(stopCh)
          return
        }
      }
      // 其他错误,如果是 1001 和 1000 就不打印日志
      if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
        fmt.Printf("ReadMessage other remote:%v error: %v \n", conn.RemoteAddr(), err)
      }
      //close(stopCh)
      return
    }
    fmt.Println("收到消息:", string(msg))
  }
}
//测试一次性发送 10万条数据给 client, 如果不使用 time.Sleep browser 过了超时时间会断开
func (self *WsServer) send10(conn *websocket.Conn) {
  for i := 0; i < 1; i++ {
    data := fmt.Sprintf("hello websocket test from server %v,count=%d", time.Now().UnixNano(), i+1)
    err := conn.WriteMessage(1, []byte(data))
    if err != nil {
      fmt.Println("send msg faild ", err)
      return
    }
    //time.Sleep(time.Millisecond * 1)
  }
}
func (self *WsServer) send(conn *websocket.Conn, stopCh chan int) {
  self.send10(conn)
  for {
    select {
    case <-stopCh:
      fmt.Println("connect closed")
      return
      // case <-time.After(time.Second * 1):
      //  fmt.Println("time after....")
    }
  }
}
func (w *WsServer) Start() (err error) {
  w.listener, err = net.Listen("tcp", w.addr)
  if err != nil {
    fmt.Println("net listen error:", err)
    return
  }
  err = http.Serve(w.listener, w)
  if err != nil {
    fmt.Println("http serve error:", err)
    return
  }
  return nil
}


相关文章
|
13天前
|
Go API 数据库
【Go 语言专栏】Go 语言中的 ORM 框架使用与比较
【4月更文挑战第30天】本文对比分析了Go语言中的常见ORM框架:GORM、XORM和BeeORM。GORM功能强大,支持复杂查询和关联关系处理,但可能影响性能;XORM以其简单易用和高性能受到青睐,但文档不全;BeeORM简洁高效,适合基础应用场景。选择ORM应考虑功能、性能、易用性和社区支持,根据项目需求进行评估和选择,以提升开发效率和应用性能。
|
13天前
|
网络协议 Java Go
【Go语言专栏】Go语言中的WebSocket实时通信应用
【4月更文挑战第30天】Go语言(Golang)是Google开发的编程语言,适用于云计算、微服务等领域。本文介绍了WebSocket,一种实现浏览器与服务器全双工通信的协议,其特点是实时性、全双工和轻量级。在Go中实现WebSocket,可以使用gorilla/websocket库。示例展示了如何创建服务器端和客户端,实现消息的收发。WebSocket广泛应用于聊天、游戏、通知推送和实时数据同步等场景。学习Go语言中的WebSocket对于开发实时通信应用至关重要。
|
23小时前
|
设计模式 开发框架 数据库
Python Web开发主要常用的框架
Python Web开发框架包括Django、Flask、Tornado和Pyramid。Django适用于复杂应用,提供ORM、模板引擎等全套功能;Flask轻量级,易于扩展,适合小型至中型项目;Tornado擅长处理高并发,支持异步和WebSockets;Pyramid灵活强大,可适配多种数据库和模板引擎,适用于各种规模项目。选择框架需依据项目需求和技术栈。
8 2
|
2天前
|
机器学习/深度学习 JSON 编译器
C++ 资源大全:标准库、Web框架、人工智能等 | 最全整理
C++ 资源列表,内容包括: 标准库、Web应用框架、人工智能、数据库、图片处理、机器学习、日志、代码分析等
15 1
|
2天前
|
Go
golang学习3,golang 项目中配置gin的web框架
golang学习3,golang 项目中配置gin的web框架
|
3天前
|
前端开发
t-io websocket的聊天功能学习记录(二)
t-io websocket的聊天功能学习记录(二)
|
3天前
t-io websocket的聊天功能学习记录(一)
t-io websocket的聊天功能学习记录(一)
|
6天前
|
分布式计算 Java Go
Golang深入浅出之-Go语言中的分布式计算框架Apache Beam
【5月更文挑战第6天】Apache Beam是一个统一的编程模型,适用于批处理和流处理,主要支持Java和Python,但也提供实验性的Go SDK。Go SDK的基本概念包括`PTransform`、`PCollection`和`Pipeline`。在使用中,需注意类型转换、窗口和触发器配置、资源管理和错误处理。尽管Go SDK文档有限,生态系统尚不成熟,且性能可能不高,但它仍为分布式计算提供了可移植的解决方案。通过理解和掌握Beam模型,开发者能编写高效的数据处理程序。
134 1
|
11天前
|
存储 前端开发 搜索推荐
13:Session机制实现用户登录与注销功能-Java Web
13:Session机制实现用户登录与注销功能-Java Web
25 3
|
11天前
|
安全 前端开发 Java
10:基于Servlet模拟用户登录功能的实现与解析-Java Web
10:基于Servlet模拟用户登录功能的实现与解析-Java Web
24 3