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-05-14 14:47:02 +00:00
|
|
|
// CreateServer for a specific HOST and PORT
|
2018-09-29 12:36:47 +00:00
|
|
|
func CreateServer(host string, port uint16) *Server {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
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-02 20:24:50 +00:00
|
|
|
|
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 {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Build URI parser
|
2021-05-14 15:23:33 +00:00
|
|
|
uriScheme, err := uri.FromString(uriStr)
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot build URI: %s", err)
|
|
|
|
}
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Create controller
|
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 {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
/* (1) Listen socket
|
|
|
|
---------------------------------------------------------*/
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Build full url
|
2018-05-02 20:24:50 +00:00
|
|
|
url := fmt.Sprintf("%s:%d", s.addr, s.port)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Bind socket to listen
|
2018-05-02 20:24:50 +00:00
|
|
|
s.sock, err = net.Listen("tcp", url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Listen socket: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer s.sock.Close()
|
|
|
|
|
|
|
|
fmt.Printf("+ listening on %s\n", url)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Launch scheduler
|
2018-05-02 20:24:50 +00:00
|
|
|
go s.scheduler()
|
|
|
|
|
|
|
|
/* (2) For each incoming connection (client)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for {
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Wait for client
|
2018-05-02 20:24:50 +00:00
|
|
|
sock, err := s.sock.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
go func() {
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Try to create client
|
2018-05-03 17:31:09 +00:00
|
|
|
cli, err := buildClient(sock, s.ctl, s.ch)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" - %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Register client
|
2018-05-03 17:31:09 +00:00
|
|
|
s.ch.register <- cli
|
2018-05-02 20:24:50 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
}()
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scheduler schedules clients registration and broadcast
|
2018-09-29 12:36:47 +00:00
|
|
|
func (s *Server) scheduler() {
|
2018-05-02 20:24:50 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Create client
|
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
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Remove client
|
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
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Broadcast
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|