# README
go-netty-ws
An Websocket server & client written by go-netty
Install
go get github.com/go-netty/go-netty-ws@latest
API overview
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) (Conn, error)
func (ws *Websocket) ServeHTTP(w http.ResponseWriter, r *http.Request)
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 WithClientHeader(header http.Header) Option
func WithDialer(dialer Dialer) Option
func WithMaxFrameSize(maxFrameSize int64) Option
func WithNoDelay(noDelay bool) Option
func WithServerHeader(header http.Header) Option
func WithServeMux(serveMux *http.ServeMux) Option
func WithServeTLS(tls *tls.Config) Option
func WithValidUTF8() Option
Easy to use
Note:
nettyws
does not support mixed text messages and binary messages, use theWithBinary
option to switch to binary message mode.
server :
// create websocket instance
var ws = nettyws.NewWebsocket()
// setup OnOpen handler
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
}
// setup OnData handler
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
conn.Write(data)
}
// setup OnClose handler
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("listening websocket connections ....")
// listen websocket server
if err := ws.Listen("ws://127.0.0.1:9527/ws"); nil != err {
panic(err)
}
client :
// create websocket instance
var ws = nettyws.NewWebsocket()
// setup OnOpen handler
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
conn.Write([]byte("hello world"))
}
// setup OnData handler
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
}
// setup OnClose handler
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("open websocket connection ...")
// connect to websocket server
if _, err := ws.Open("ws://127.0.0.1:9527/ws"); nil != err {
panic(err)
}
upgrade from http server:
// create websocket instance
var ws = nettyws.NewWebsocket()
// setup OnOpen handler
ws.OnOpen = func(conn nettyws.Conn) {
fmt.Println("OnOpen: ", conn.RemoteAddr())
}
// setup OnData handler
ws.OnData = func(conn nettyws.Conn, data []byte) {
fmt.Println("OnData: ", conn.RemoteAddr(), ", message: ", string(data))
conn.Write(data)
}
// setup OnClose handler
ws.OnClose = func(conn nettyws.Conn, err error) {
fmt.Println("OnClose: ", conn.RemoteAddr(), ", error: ", err)
}
fmt.Println("upgrade websocket connections ....")
// upgrade websocket connection from http server
http.Handle("/ws", ws)
// listen http server
if err := http.ListenAndServe(":9527", nil); nil != err {
panic(err)
}
Associated
# Functions
NewWebsocket create websocket instance with options.
WithAsyncWrite enable async write.
WithBinary switch to binary message mode.
WithBufferSize set the read/write buffer size.
WithClientHeader is an optional http.Header mapping that could be used to write additional headers to the handshake request.
WithCompress enable message compression with level, messages below the threshold will not be compressed.
WithDialer specify the client to connect to the network via a dialer.
WithMaxFrameSize set the maximum frame size.
WithNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm).
WithServeMux overwrite default http.ServeMux.
WithServerHeader is an optional http.Header mapping that could be used to write additional headers to the handshake response.
WithServeTLS serve port with TLS.
WithValidUTF8 enable UTF-8 checks for text frames payload.
# Variables
ErrServerClosed is returned by the Server call Shutdown or Close.
# Structs
ClosedError returned when peer has closed the connection with appropriate code and a textual reason.
No description provided by the author
# Type aliases
MessageType websocket message type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author