2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-29 12:36:47 +00:00
|
|
|
"net"
|
2021-05-14 14:47:02 +00:00
|
|
|
|
2021-05-14 15:23:33 +00:00
|
|
|
"git.xdrm.io/go/ws/internal/uri"
|
2018-05-02 20:24:50 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// All channels that a server features
|
2018-09-29 12:36:47 +00:00
|
|
|
type serverChannelSet struct {
|
2018-05-03 17:31:09 +00:00
|
|
|
register chan *client
|
|
|
|
unregister chan *client
|
2018-05-05 16:43:16 +00:00
|
|
|
broadcast chan Message
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// Server is a websocket server
|
2018-05-03 17:31:09 +00:00
|
|
|
type Server struct {
|
2018-09-29 12:36:47 +00:00
|
|
|
sock net.Listener // listen socket
|
|
|
|
addr []byte // server listening ip/host
|
|
|
|
port uint16 // server listening port
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
clients map[net.Conn]*client
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
ctl ControllerSet // controllers
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
ch serverChannelSet
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// NewServer creates a server
|
|
|
|
func NewServer(host string, port uint16) *Server {
|
2018-05-03 17:31:09 +00:00
|
|
|
return &Server{
|
2018-09-29 12:36:47 +00:00
|
|
|
addr: []byte(host),
|
|
|
|
port: port,
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
clients: make(map[net.Conn]*client, 0),
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
ctl: ControllerSet{
|
2018-05-02 20:24:50 +00:00
|
|
|
Def: nil,
|
2021-05-14 14:47:02 +00:00
|
|
|
URI: make([]*Controller, 0),
|
2018-05-02 20:24:50 +00:00
|
|
|
},
|
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
ch: serverChannelSet{
|
2018-05-05 16:43:16 +00:00
|
|
|
register: make(chan *client, 1),
|
|
|
|
unregister: make(chan *client, 1),
|
|
|
|
broadcast: make(chan Message, 1),
|
2018-05-02 20:24:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BindDefault binds a default controller
|
|
|
|
// it will be called if the URI does not
|
|
|
|
// match another controller
|
2018-09-29 12:36:47 +00:00
|
|
|
func (s *Server) BindDefault(f ControllerFunc) {
|
2018-05-03 17:31:09 +00:00
|
|
|
s.ctl.Def = &Controller{
|
2018-05-02 20:24:50 +00:00
|
|
|
URI: nil,
|
|
|
|
Fun: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// Bind a controller to an URI scheme
|
2021-05-14 15:23:33 +00:00
|
|
|
func (s *Server) Bind(uriStr string, f ControllerFunc) error {
|
|
|
|
uriScheme, err := uri.FromString(uriStr)
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != nil {
|
2021-06-15 20:13:36 +00:00
|
|
|
return fmt.Errorf("cannot build URI: %w", err)
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
s.ctl.URI = append(s.ctl.URI, &Controller{
|
2018-05-02 20:24:50 +00:00
|
|
|
URI: uriScheme,
|
|
|
|
Fun: f,
|
2018-09-29 12:36:47 +00:00
|
|
|
})
|
2018-05-02 20:24:50 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
// Launch the websocket server
|
2018-05-03 17:31:09 +00:00
|
|
|
func (s *Server) Launch() error {
|
2021-06-15 22:04:09 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
url = fmt.Sprintf("%s:%d", s.addr, s.port)
|
|
|
|
)
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
s.sock, err = net.Listen("tcp", url)
|
|
|
|
if err != nil {
|
2021-06-15 20:13:36 +00:00
|
|
|
return fmt.Errorf("listen: %w", err)
|
2018-05-02 20:24:50 +00:00
|
|
|
}
|
|
|
|
defer s.sock.Close()
|
|
|
|
|
|
|
|
fmt.Printf("+ listening on %s\n", url)
|
2021-06-15 22:04:09 +00:00
|
|
|
go s.schedule()
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
sock, err := s.sock.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
go func() {
|
2021-06-15 22:04:09 +00:00
|
|
|
cli, err := newClient(sock, s.ctl, s.ch)
|
2018-05-03 17:31:09 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" - %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ch.register <- cli
|
|
|
|
}()
|
2018-05-02 20:24:50 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// schedule client registration and broadcast
|
|
|
|
func (s *Server) schedule() {
|
2018-05-02 20:24:50 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
case client := <-s.ch.register:
|
|
|
|
s.clients[client.io.sock] = client
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
case client := <-s.ch.unregister:
|
|
|
|
delete(s.clients, client.io.sock)
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
case msg := <-s.ch.broadcast:
|
|
|
|
for _, c := range s.clients {
|
|
|
|
c.ch.send <- msg
|
|
|
|
}
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|