created [ws.*] to create a server, bind a default controller, or bind URI-bound controllers + launch() *TODO* + created websocket types

This commit is contained in:
xdrm-brackets 2018-04-26 22:54:17 +02:00
parent c6112c9c74
commit d603145f5f
4 changed files with 146 additions and 0 deletions

37
cmd/iface/main.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"time"
"git.xdrm.io/gws/ws"
"fmt"
)
func main(){
startTime := time.Now().UnixNano()
/* (1) Bind WebSocket server */
serv := ws.CreateServer("0.0.0.0", 4444)
/* (2) Bind default controller */
err := serv.BindDefault(func(c ws.Client, f ws.Frame){
fmt.Printf("Default controller")
})
if err != nil { panic(err) }
/* (3) Bind to URI */
err = serv.Bind("/channel/./room/./", func(c ws.Client, f ws.Frame){
fmt.Printf("URI controller")
})
if err != nil { panic(err) }
/* (4) Launch the server */
serv.Launch()
fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
}

19
ws/public.go Normal file
View File

@ -0,0 +1,19 @@
package ws
import ()
// CreateServer creates a server for a specific HOST and PORT
func CreateServer(host string, port uint16) *Server{
inst := new(Server)
inst.adr = make([]byte, len(host))
copy(inst.adr, []byte(host))
inst.prt = port
return inst
}

50
ws/server.go Normal file
View File

@ -0,0 +1,50 @@
package ws
import (
"fmt"
"git.xdrm.io/gws/internal/uri/parser"
)
// BindDefault binds a default controller
// it will be called if the URI does not
// match another controller
func (s *Server) BindDefault(c ControllerFunc) error{
s.def = &Controller{nil, c};
return nil
}
// Bind binds a controller to an URI scheme
func (s *Server) Bind(uri string, c ControllerFunc) error {
/* (1) Build URI parser */
uriScheme, err := parser.Build(uri)
if err != nil { return fmt.Errorf("Cannot build URI: %s", err) }
/* (2) Create controller */
ctl := &Controller{uri: uriScheme, fun: c}
/* (3) Add to server */
s.ctl = append(s.ctl, ctl)
return nil
}
// Launch launches the websocket server
func (s *Server) Launch(){
/* (1) Listen socket
---------------------------------------------------------*/
/* (1) Build full url */
url := fmt.Sprintf("%s:%d", s.adr, s.prt)
/* (3) Bind listen socket */
// lsock, err := net.Listen("tcp", )
}

40
ws/types.go Normal file
View File

@ -0,0 +1,40 @@
package ws
import (
"git.xdrm.io/gws/internal/uri/parser"
"git.xdrm.io/gws/ws/frame"
)
// Represents a websocket controller callback function
type ControllerFunc func(Client, Frame)
// Represents a websocket controller
type Controller struct {
uri *parser.Scheme
fun ControllerFunc
}
// Represents a websocket srever
type Server struct {
adr []byte // server listening ip/host
prt uint16 // server listening port
def *Controller // default controller
ctl []*Controller // URI-bound controllers
}
// Represents a websocket client
type Client struct {
pro string // choosen protocol (Sec-WebSocket-Protocol)
arg [][]string // URI parameters, index 0 is full URI, then matching groups
dat interface{} // store (for client implementation-specific data)
}
// Represents a websocket frame
type Frame struct {
met frame.Header
buf []byte
len uint64
}