ego008
ego008
9890 1 0

支持 fasthttp 三个 websocket 库

支持 fasthttp 的 websocket 库

补充最近2021年还活跃的库

以上 3 个都fork from https://github.com/gorilla/websocket

fasthttp-contrib/websocket 一个☝️栗子🌰

import (
	"github.com/fasthttp-contrib/websocket"
	"github.com/vayala/fasthttp"
)

func chat(c *websocket.Conn) {
	// defer c.Close()
	// mt, message, err := c.ReadMessage()
	// c.WriteMessage(mt, message)
}

var upgrader = websocket.New(chat) // use default options
//var upgrader = websocket.Custom(chat, 1024, 1024) // customized options, read and write buffer sizes (int). Default: 4096
// var upgrader = websocket.New(chat).DontCheckOrigin() // it's useful when you have the websocket server on a different machine

func myChatHandler(ctx *fasthttp.RequestCtx) {
	err := upgrader.Upgrade(ctx)// returns only error, executes the handler you defined on the websocket.New before (the 'chat' function)
}

func main() {
	fasthttp.ListenAndServe(":8080", myChatHandler)
}

fasthttp https://github.com/valyala/fasthttp 适合用来做微服务

0

See Also

Nearby


Discussion (1)

一花一叶一世界
一花一叶一世界 2018-08-17 00:43

不支持http 2.0

0
Login Topics