支持 fasthttp 三个 websocket 库

By ego008 at 2018-01-18 19:00 • 2137次点击
ego008

支持 fasthttp 的 websocket 库

https://github.com/fasthttp-contrib/websocket 35
https://github.com/leavengood/websocket 24
https://github.com/clevergo/websocket 25 fork 上面,去掉了支持net/http 功能,改进了Upgrader

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

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 128 适合用来做微服务


fasthttp, websocket,


不支持http 2.0

一花一叶一世界 at 2018-08-17 08:43
1
登录 后发表评论