From 427d7d06c85e1bc07db2b8b7afdd8bde61e3e3c7 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 26 Apr 2018 23:38:50 +0200 Subject: [PATCH] added server Launch() routine using s.dispatch() --- ws/server.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/ws/server.go b/ws/server.go index 462f0b9..79362a7 100644 --- a/ws/server.go +++ b/ws/server.go @@ -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 } \ No newline at end of file