Skip to content

Latest commit

 

History

History
59 lines (50 loc) · 1019 Bytes

README.md

File metadata and controls

59 lines (50 loc) · 1019 Bytes

mini-websocket

websocket framework

go version

go 1.17+

function

  • 序列化和反序列化帧
  • 握手与挥手
  • 良好的封装
  • 心跳api
  • 数据分片传输
  • 跨域处理
  • 压缩

start

go get -u github.com/cold-bin/mini-websocket

使用示例

// @author cold bin
// @date 2022/7/25

package mini_websocket

import (
	"log"
	"net/http"

	websocket "github.com/cold-bin/mini-websocket"
)

func main() {
	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		wsConn, err := websocket.DefaultUpGrader.UpGrade(c.Req, c.Writer)
		if err != nil {
			log.Println(err)
			return
		}
		wsConn.SendMessage("欢迎使用本websocket连接") //当前为server端,不会做掩码处理

		for {
			_, bytes, err := wsConn.ReadMessage()
			if err != nil {
				log.Println(err)
				return
			}

			wsConn.SendMessage(string(bytes))
		}
	})

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Println(err)
		return
	}
}