added 'chan *Frame' and channel principle to build the Controller implementation on
This commit is contained in:
parent
dcfdb3411a
commit
a3f72a9bf7
|
@ -75,13 +75,12 @@ func (s *Server) dispatch(sock net.Conn, u *upgrader.T){
|
||||||
/* (2) Add controller to client */
|
/* (2) Add controller to client */
|
||||||
client.ctl = controller
|
client.ctl = controller
|
||||||
|
|
||||||
/* (3) Launch controller routine (for new connection) */
|
/* (3) Bind controller */
|
||||||
go controller.fun(client, nil)
|
go controller.fun(client, controller.dat)
|
||||||
|
|
||||||
/* (4) Launch continuous frame reader */
|
/* (4) Launch continuous frame reader */
|
||||||
go client.awaitFrame()
|
go client.awaitFrame()
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,12 +118,13 @@ func (c *Client) awaitFrame() {
|
||||||
|
|
||||||
fmt.Printf("+ elapsed: %.3f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
fmt.Printf("+ elapsed: %.3f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
||||||
|
|
||||||
// Call controller
|
// Trigger data channel
|
||||||
c.ctl.fun(c, frame)
|
c.ctl.dat <- frame
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(" - error\n")
|
fmt.Printf(" - error; disconnecting\n")
|
||||||
|
close(c.ctl.dat)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
ws/server.go
10
ws/server.go
|
@ -12,7 +12,10 @@ import (
|
||||||
// it will be called if the URI does not
|
// it will be called if the URI does not
|
||||||
// match another controller
|
// match another controller
|
||||||
func (s *Server) BindDefault(c ControllerFunc) error{
|
func (s *Server) BindDefault(c ControllerFunc) error{
|
||||||
s.def = &Controller{nil, c};
|
s.def = new(Controller);
|
||||||
|
s.def.uri = nil
|
||||||
|
s.def.fun = c
|
||||||
|
s.def.dat = make(chan *Frame, maxChannelBufferLength)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -25,7 +28,10 @@ func (s *Server) Bind(uri string, c ControllerFunc) error {
|
||||||
if err != nil { return fmt.Errorf("Cannot build URI: %s", err) }
|
if err != nil { return fmt.Errorf("Cannot build URI: %s", err) }
|
||||||
|
|
||||||
/* (2) Create controller */
|
/* (2) Create controller */
|
||||||
ctl := &Controller{uri: uriScheme, fun: c}
|
ctl := new(Controller)
|
||||||
|
ctl.uri = uriScheme
|
||||||
|
ctl.fun = c
|
||||||
|
ctl.dat = make(chan *Frame, maxChannelBufferLength)
|
||||||
|
|
||||||
/* (3) Add to server */
|
/* (3) Add to server */
|
||||||
s.ctl = append(s.ctl, ctl)
|
s.ctl = append(s.ctl, ctl)
|
||||||
|
|
|
@ -6,15 +6,17 @@ import (
|
||||||
"git.xdrm.io/gws/ws/frame"
|
"git.xdrm.io/gws/ws/frame"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxBufferLength = 4096
|
const maxBufferLength = 4096
|
||||||
|
const maxChannelBufferLength = 1
|
||||||
|
|
||||||
// Represents a websocket controller callback function
|
// Represents a websocket controller callback function
|
||||||
type ControllerFunc func(*Client, *Frame)
|
type ControllerFunc func(*Client, chan *Frame)
|
||||||
|
|
||||||
// Represents a websocket controller
|
// Represents a websocket controller
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
uri *parser.Scheme
|
uri *parser.Scheme
|
||||||
fun ControllerFunc
|
fun ControllerFunc
|
||||||
|
dat chan *Frame // data channel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue