go-netty-ws 是一款基于 go-netty 编写的高性能 Websocket 服务器 & 客户端库。
- 基于事件回调的API设计,简单易用
- 高性能(压测数据参考go-websocket-benchmark)
- 支持压缩协议
- 支持从标准库升级,无缝衔接
更新记录
- v1.0.3
- 修复压缩相关问题,提升与浏览器交互兼容性
安装
go get github.com/go-netty/go-netty-ws@latest
API 预览
type Websocket
func NewWebsocket(options ...Option) *Websocket
func (ws *Websocket) Close() error
func (ws *Websocket) Listen(addr string) error
func (ws *Websocket) Open(addr string) error
func (ws *Websocket) UpgradeHTTP(w http.ResponseWriter, r *http.Request) (Conn, error)
type Option
func WithAsyncWrite(writeQueueSize int, writeForever bool) Option
func WithBinary() Option
func WithBufferSize(readBufferSize, writeBufferSize int) Option
func WithCompress(compressLevel int, compressThreshold int64) Option
func WithMaxFrameSize(maxFrameSize int64) Option
func WithNoDelay(noDelay bool) Option
func WithServeMux(serveMux *http.ServeMux) Option
func WithServeTLS(tls *tls.Config) Option
func WithValidUTF8() Option
简单使用案例:
服务端示例:
// 创建一个websocket实例
var ws = nettyws.NewWebsocket()
// 设置OnOpen事件处理器,当连接建立时被调用
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
}
// 设置OnData事件处理器,当收到一个消息时被调用
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
conn.Write(data)
}
// 设置OnClose事件处理器,当收到连接关闭时被调用
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("listening websocket connections ....")
// 启动端口监听,接受新的连接
if err := ws.Listen("ws://127.0.0.1:9527/ws"); nil != err {
panic(err)
}
客户端示例:
// 创建websocket实例
var ws = nettyws.NewWebsocket()
// 设置OnOpen事件处理器,当连接建立时被回调
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
conn.Write([]byte("hello world"))
}
// 设置OnData事件处理器,当连接收到消息时被回调
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
}
// 设置OnClose事件处理器,当连接关闭时被回调
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("open websocket connection ...")
// 连接到远程服务器
if err := ws.Open("ws://127.0.0.1:9527/ws"); nil != err {
panic(err)
}
从标准Http服务器升级:
// 创建websocket实例
var ws = nettyws.NewWebsocket()
// 设置OnOpen事件处理器,当连接建立时被调用
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
}
// 设置OnData事件处理器,当连接收到消息时被调用
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
conn.Write(data)
}
// 设置OnClose事件处理器,当连接关闭时被回调
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("upgrade websocket connections ....")
// 绑定http路由处理函数,并从http升级到websocket
serveMux := http.NewServeMux()
serveMux.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {
ws.UpgradeHTTP(writer, request)
})
// 启动端口监听,接受客户端请求
if err := http.ListenAndServe(":9527", serveMux); nil != err {
panic(err)
}
相关连接
- https://github.com/go-netty/go-netty
- https://github.com/go-netty/go-netty-transport
- https://github.com/gobwas/ws
- https://github.com/lesismal/go-websocket-benchmark
- https://github.com/crossbario/autobahn-testsuite