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 */
|
||||
client.ctl = controller
|
||||
|
||||
/* (3) Launch controller routine (for new connection) */
|
||||
go controller.fun(client, nil)
|
||||
/* (3) Bind controller */
|
||||
go controller.fun(client, controller.dat)
|
||||
|
||||
/* (4) Launch continuous frame reader */
|
||||
go client.awaitFrame()
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
|
@ -119,12 +118,13 @@ func (c *Client) awaitFrame() {
|
|||
|
||||
fmt.Printf("+ elapsed: %.3f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
||||
|
||||
// Call controller
|
||||
c.ctl.fun(c, frame)
|
||||
// Trigger data channel
|
||||
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
|
||||
// match another controller
|
||||
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
|
||||
}
|
||||
|
@ -25,7 +28,10 @@ func (s *Server) Bind(uri string, c ControllerFunc) error {
|
|||
if err != nil { return fmt.Errorf("Cannot build URI: %s", err) }
|
||||
|
||||
/* (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 */
|
||||
s.ctl = append(s.ctl, ctl)
|
||||
|
|
|
@ -7,14 +7,16 @@ import (
|
|||
)
|
||||
|
||||
const maxBufferLength = 4096
|
||||
const maxChannelBufferLength = 1
|
||||
|
||||
// Represents a websocket controller callback function
|
||||
type ControllerFunc func(*Client, *Frame)
|
||||
type ControllerFunc func(*Client, chan *Frame)
|
||||
|
||||
// Represents a websocket controller
|
||||
type Controller struct {
|
||||
uri *parser.Scheme
|
||||
fun ControllerFunc
|
||||
dat chan *Frame // data channel
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue