package overflow_gateway_websocket import ( "log" "git.loafle.net/overflow/overflow_gateway_websocket/websocket" "github.com/valyala/fasthttp" ) type () type Server interface { ListenAndServe(addr string) error } type server struct { o *Options upgrader *websocket.Upgrader } func NewServer(o *Options) Server { s := &server{ o: o.Validate(), } s.upgrader = &websocket.Upgrader{ ReadBufferSize: s.o.ReadBufferSize, WriteBufferSize: s.o.WriteBufferSize, HandshakeTimeout: s.o.HandshakeTimeout, } return s } func (s *server) onRequest(cb OnRequestFunc) { } func (s *server) onNotify(cb OnNotifyFunc) { } func (s *server) onPush(cb OnPushFunc) { } func (s *server) requestHandler(ctx *fasthttp.RequestCtx, c *websocket.Conn) { } func (s *server) connectionHandler(ctx *fasthttp.RequestCtx) { s.upgrader.Upgrade(ctx, nil, func(conn *websocket.Conn, err error) { if err != nil { log.Print("upgrade:", err) return } s.o.OnConnection(ctx, conn) s.requestHandler(ctx, conn) }) } func (s *server) ListenAndServe(addr string) error { return fasthttp.ListenAndServe(addr, s.connectionHandler) }