2017-08-18 11:30:29 +00:00
|
|
|
package overflow_gateway_websocket
|
|
|
|
|
2017-08-21 10:23:45 +00:00
|
|
|
import (
|
2017-08-24 09:24:38 +00:00
|
|
|
"log"
|
2017-08-21 10:23:45 +00:00
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/clients"
|
2017-08-24 09:24:38 +00:00
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/websocket"
|
|
|
|
"github.com/valyala/fasthttp"
|
2017-08-18 11:30:29 +00:00
|
|
|
)
|
|
|
|
|
2017-08-24 09:24:38 +00:00
|
|
|
type ()
|
|
|
|
|
2017-08-18 11:30:29 +00:00
|
|
|
type Server interface {
|
2017-08-24 09:24:38 +00:00
|
|
|
ListenAndServe(addr string) error
|
2017-08-24 11:47:41 +00:00
|
|
|
HandleClient(pattern string, o *clients.Options)
|
2017-08-18 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
2017-08-24 11:29:22 +00:00
|
|
|
_option *Options
|
|
|
|
_upgrader *websocket.Upgrader
|
|
|
|
_handlers map[string]*clients.Options
|
|
|
|
_clients map[string]clients.Client
|
2017-08-18 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:24:38 +00:00
|
|
|
func NewServer(o *Options) Server {
|
2017-08-18 11:30:29 +00:00
|
|
|
s := &server{
|
2017-08-24 11:49:51 +00:00
|
|
|
_option: o.Validate(),
|
|
|
|
_handlers: make(map[string]*clients.Options, 1),
|
|
|
|
_clients: make(map[string]clients.Client, 100),
|
2017-08-21 10:23:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
s._upgrader = &websocket.Upgrader{
|
|
|
|
ReadBufferSize: s._option.ReadBufferSize,
|
|
|
|
WriteBufferSize: s._option.WriteBufferSize,
|
|
|
|
HandshakeTimeout: s._option.HandshakeTimeout,
|
2017-08-24 14:50:14 +00:00
|
|
|
CheckOrigin: s._option.OnCheckOrigin,
|
2017-08-24 10:42:40 +00:00
|
|
|
}
|
2017-08-21 10:23:45 +00:00
|
|
|
|
2017-08-18 11:30:29 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:24:38 +00:00
|
|
|
func (s *server) onPush(cb OnPushFunc) {
|
|
|
|
|
2017-08-18 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
func (s *server) onDisconnected(c clients.Client) {
|
|
|
|
delete(s._clients, c.ID())
|
2017-08-24 09:24:38 +00:00
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
s._option.OnDisconnected(c)
|
2017-08-18 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
func (s *server) onConnection(ctx *fasthttp.RequestCtx) {
|
2017-08-24 14:50:14 +00:00
|
|
|
path := string(ctx.Path())
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
s._upgrader.Upgrade(ctx, nil, func(conn *websocket.Conn, err error) {
|
2017-08-24 09:24:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print("upgrade:", err)
|
|
|
|
return
|
|
|
|
}
|
2017-08-24 11:29:22 +00:00
|
|
|
co, ok := s._handlers[path]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("Path[%s] is not exist.", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
cid := s._option.IDGenerator(ctx)
|
2017-08-24 11:29:22 +00:00
|
|
|
c := clients.New(cid, co, ctx, conn)
|
2017-08-24 10:42:40 +00:00
|
|
|
s._clients[cid] = c
|
2017-08-24 11:29:22 +00:00
|
|
|
s._option.OnConnection(path, c)
|
2017-08-24 09:24:38 +00:00
|
|
|
})
|
|
|
|
}
|
2017-08-18 11:30:29 +00:00
|
|
|
|
2017-08-24 11:29:22 +00:00
|
|
|
func (s *server) HandleClient(pattern string, o *clients.Options) {
|
|
|
|
s._handlers[pattern] = o.Validate()
|
|
|
|
}
|
|
|
|
|
2017-08-24 09:24:38 +00:00
|
|
|
func (s *server) ListenAndServe(addr string) error {
|
2017-08-24 10:42:40 +00:00
|
|
|
return fasthttp.ListenAndServe(addr, s.onConnection)
|
2017-08-18 11:30:29 +00:00
|
|
|
}
|