added server Launch() routine using s.dispatch()

This commit is contained in:
xdrm-brackets 2018-04-26 23:38:50 +02:00
parent 8432c81e07
commit 427d7d06c8
1 changed files with 36 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package ws
import (
"git.xdrm.io/gws/upgrader"
"net"
"fmt"
"git.xdrm.io/gws/internal/uri/parser"
)
@ -34,7 +36,9 @@ func (s *Server) Bind(uri string, c ControllerFunc) error {
// Launch launches the websocket server
func (s *Server) Launch(){
func (s *Server) Launch() error {
var err error
/* (1) Listen socket
---------------------------------------------------------*/
@ -42,9 +46,39 @@ func (s *Server) Launch(){
url := fmt.Sprintf("%s:%d", s.adr, s.prt)
/* (3) Bind listen socket */
// lsock, err := net.Listen("tcp", )
s.soc, err = net.Listen("tcp", url)
if err != nil {
return fmt.Errorf("Listen socket: %s", err)
}
fmt.Printf("+ listening on %s\n", url)
/* (2) For each incoming connection (client)
---------------------------------------------------------*/
for {
// {1} Wait for connection //
sock, err := s.soc.Accept()
fmt.Printf(" + new client\n")
if err != nil {
fmt.Printf(" - error: %s\n", err)
continue
}
// {2} Upgrade request //
upgrader, err := upgrader.Upgrade(sock)
if err != nil {
fmt.Printf(" - upgrade error: %s\n", err)
continue
}
// {3} Dispatch to controllers //
go s.dispatch(sock, upgrader)
}
return nil
}