go -netty-ws is a high-performance Websocket server & client library written based on go-netty .
- API design based on event callback, simple and easy to use
- High performance (refer to go-websocket-benchmark for stress test data )
- Support compression protocol
- Supports upgrade from standard library, seamless connection
update record
- v1.0.3
- Fix compression-related issues and improve compatibility with browser interactions
Install
go get github.com/go-netty/go-netty-ws@latest
API preview
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
Simple use case:
Server side example:
// 创建一个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)
}
Client example:
// 创建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)
}
Upgrade from standard HTTP server:
// 创建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)
}