143 lines
2.9 KiB
Go
143 lines
2.9 KiB
Go
package overflow_gateway_websocket
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/websocket"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
type ()
|
|
|
|
type sendRequest struct {
|
|
s Socket
|
|
m []byte
|
|
}
|
|
|
|
type Server interface {
|
|
ListenAndServe(addr string) error
|
|
HandleSocket(pattern string, o *SocketOptions)
|
|
Send(soc Socket, m []byte)
|
|
SendAll(m []byte)
|
|
}
|
|
|
|
type server struct {
|
|
_option *ServerOptions
|
|
_upgrader *websocket.Upgrader
|
|
_handlers map[string]*SocketOptions
|
|
_sockets map[string]Socket
|
|
_sIdMtx sync.Mutex
|
|
_addSocketCh chan Socket
|
|
_removeSocketCh chan Socket
|
|
_sendCh chan sendRequest
|
|
_sendAllCh chan []byte
|
|
}
|
|
|
|
func NewServer(o *ServerOptions) Server {
|
|
s := &server{
|
|
_option: o.Validate(),
|
|
_handlers: make(map[string]*SocketOptions, 1),
|
|
_sockets: make(map[string]Socket, 100),
|
|
}
|
|
|
|
s._upgrader = &websocket.Upgrader{
|
|
HandshakeTimeout: s._option.HandshakeTimeout,
|
|
ReadBufferSize: s._option.ReadBufferSize,
|
|
WriteBufferSize: s._option.WriteBufferSize,
|
|
CheckOrigin: s._option.OnCheckOrigin,
|
|
Error: s.onError,
|
|
EnableCompression: s._option.EnableCompression,
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *server) addSocket(soc Socket) {
|
|
s._addSocketCh <- soc
|
|
}
|
|
|
|
func (s *server) removeSocket(soc Socket) {
|
|
s._removeSocketCh <- soc
|
|
}
|
|
|
|
func (s *server) onError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
|
ctx.Response.Header.Set("Sec-Websocket-Version", "13")
|
|
ctx.Error(http.StatusText(status), status)
|
|
|
|
s._option.OnError(ctx, status, reason)
|
|
}
|
|
|
|
func (s *server) onDisconnected(soc Socket) {
|
|
s.removeSocket(soc)
|
|
s._option.OnDisconnected(soc)
|
|
}
|
|
|
|
func (s *server) onConnection(ctx *fasthttp.RequestCtx) {
|
|
path := string(ctx.Path())
|
|
co, ok := s._handlers[path]
|
|
if !ok {
|
|
s.onError(ctx, fasthttp.StatusNotFound, nil)
|
|
return
|
|
}
|
|
|
|
s._upgrader.Upgrade(ctx, nil, func(conn *websocket.Conn, err error) {
|
|
if err != nil {
|
|
s.onError(ctx, fasthttp.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
s._sIdMtx.Lock()
|
|
id := s._option.IDGenerator(ctx)
|
|
s._sIdMtx.Unlock()
|
|
soc := NewSocket(id, path, co, conn)
|
|
s.addSocket(soc)
|
|
s._option.OnConnection(soc)
|
|
|
|
soc.run()
|
|
})
|
|
}
|
|
|
|
func (s *server) listenHandler() {
|
|
for {
|
|
select {
|
|
// Add new a socket
|
|
case soc := <-s._addSocketCh:
|
|
s._sockets[soc.ID()] = soc
|
|
|
|
// remove a socket
|
|
case soc := <-s._removeSocketCh:
|
|
delete(s._sockets, soc.ID())
|
|
case sr := <-s._sendCh:
|
|
sr.s.Write(sr.m)
|
|
case m := <-s._sendAllCh:
|
|
for _, soc := range s._sockets {
|
|
soc.Write(m)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *server) Send(soc Socket, m []byte) {
|
|
r := sendRequest{
|
|
s: soc,
|
|
m: m,
|
|
}
|
|
s._sendCh <- r
|
|
}
|
|
|
|
func (s *server) SendAll(m []byte) {
|
|
s._sendAllCh <- m
|
|
}
|
|
|
|
func (s *server) HandleSocket(pattern string, o *SocketOptions) {
|
|
o.onDisconnected = s.onDisconnected
|
|
|
|
s._handlers[pattern] = o.Validate()
|
|
}
|
|
|
|
func (s *server) ListenAndServe(addr string) error {
|
|
go s.listenHandler()
|
|
|
|
return fasthttp.ListenAndServe(addr, s.onConnection)
|
|
}
|