Skip to content

Commit

Permalink
chore(websocket): update
Browse files Browse the repository at this point in the history
Change-Id: I799fa54d219b91bace65d521e5d4ba785af6bfa2
  • Loading branch information
andeya committed Feb 15, 2019
1 parent 62e3f8d commit f8770ad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
10 changes: 5 additions & 5 deletions mixer/websocket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func (p *P) Divide(arg *Arg) (int, *tp.Rerror) {
}

func TestJSONWebsocket(t *testing.T) {
srv := ws.NewServer("/ws", tp.PeerConfig{ListenPort: 9090})
srv := ws.NewServer("/", tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(P))
go srv.ListenAndServe()

time.Sleep(time.Second * 1)

cli := ws.NewClient(":9090", "/ws", tp.PeerConfig{})
sess, err := cli.Dial()
cli := ws.NewClient("/", tp.PeerConfig{})
sess, err := cli.Dial(":9090")
if err != nil {
t.Fatal(err)
}
Expand All @@ -66,9 +66,9 @@ func TestPbWebsocketTLS(t *testing.T) {

time.Sleep(time.Second * 1)

cli := ws.NewClient(":9091", "/ws", tp.PeerConfig{})
cli := ws.NewClient("/ws", tp.PeerConfig{})
cli.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
sess, err := cli.DialProtobuf()
sess, err := cli.DialProtobuf(":9091")
if err != nil {
t.Fatal(err)
}
Expand Down
43 changes: 21 additions & 22 deletions mixer/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,47 @@ import (
// Client a websocket client
type Client struct {
tp.Peer
addr string
}

// NewClient creates a websocket client.
func NewClient(addr, pattern string, cfg tp.PeerConfig, globalLeftPlugin ...tp.Plugin) *Client {
globalLeftPlugin = append(globalLeftPlugin, NewDialPlugin(pattern))
func NewClient(rootPath string, cfg tp.PeerConfig, globalLeftPlugin ...tp.Plugin) *Client {
globalLeftPlugin = append(globalLeftPlugin, NewDialPlugin(rootPath))
peer := tp.NewPeer(cfg, globalLeftPlugin...)
return &Client{
Peer: peer,
addr: addr,
}
}

// DialJSON connects with the JSON protocol.
func (c *Client) DialJSON() (tp.Session, *tp.Rerror) {
return c.Dial(jsonSubProto.NewJSONSubProtoFunc())
func (c *Client) DialJSON(addr string) (tp.Session, *tp.Rerror) {
return c.Dial(addr, jsonSubProto.NewJSONSubProtoFunc())
}

// DialProtobuf connects with the Protobuf protocol.
func (c *Client) DialProtobuf() (tp.Session, *tp.Rerror) {
return c.Dial(pbSubProto.NewPbSubProtoFunc())
func (c *Client) DialProtobuf(addr string) (tp.Session, *tp.Rerror) {
return c.Dial(addr, pbSubProto.NewPbSubProtoFunc())
}

// Dial connects with the peer of the destination address.
func (c *Client) Dial(protoFunc ...tp.ProtoFunc) (tp.Session, *tp.Rerror) {
func (c *Client) Dial(addr string, protoFunc ...tp.ProtoFunc) (tp.Session, *tp.Rerror) {
if len(protoFunc) == 0 {
return c.Peer.Dial(c.addr, defaultProto)
return c.Peer.Dial(addr, defaultProto)
}
return c.Peer.Dial(c.addr, protoFunc...)
return c.Peer.Dial(addr, protoFunc...)
}

// NewDialPlugin creates a websocket plugin for client.
func NewDialPlugin(pattern string) tp.Plugin {
pattern = path.Join("/", strings.TrimRight(pattern, "/"))
if pattern == "/" {
pattern = ""
}
return &clientPlugin{pattern}
func NewDialPlugin(rootPath string) tp.Plugin {
return &clientPlugin{fixRootPath(rootPath)}
}

func fixRootPath(rootPath string) string {
rootPath = path.Join("/", strings.TrimRight(rootPath, "/"))
return rootPath
}

type clientPlugin struct {
pattern string
rootPath string
}

var (
Expand All @@ -86,11 +85,11 @@ func (*clientPlugin) Name() string {
func (c *clientPlugin) PostDial(sess tp.PreSession) *tp.Rerror {
var location, origin string
if sess.Peer().TLSConfig() == nil {
location = "ws://" + sess.RemoteAddr().String() + c.pattern
origin = "ws://" + sess.LocalAddr().String() + c.pattern
location = "ws://" + sess.RemoteAddr().String() + c.rootPath
origin = "ws://" + sess.LocalAddr().String() + c.rootPath
} else {
location = "wss://" + sess.RemoteAddr().String() + c.pattern
origin = "wss://" + sess.LocalAddr().String() + c.pattern
location = "wss://" + sess.RemoteAddr().String() + c.rootPath
origin = "wss://" + sess.LocalAddr().String() + c.rootPath
}
cfg, err := ws.NewConfig(location, origin)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions mixer/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ type Server struct {
cfg tp.PeerConfig
serveMux *http.ServeMux
server *http.Server
pattern string
rootPath string
lis net.Listener
handshake func(*ws.Config, *http.Request) error
}

// NewServer creates a websocket server.
func NewServer(pattern string, cfg tp.PeerConfig, globalLeftPlugin ...tp.Plugin) *Server {
func NewServer(rootPath string, cfg tp.PeerConfig, globalLeftPlugin ...tp.Plugin) *Server {
p := tp.NewPeer(cfg, globalLeftPlugin...)
serveMux := http.NewServeMux()
return &Server{
Peer: p,
cfg: cfg,
serveMux: serveMux,
pattern: pattern,
rootPath: fixRootPath(rootPath),
server: &http.Server{Addr: cfg.ListenerAddr(), Handler: serveMux},
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (srv *Server) ListenAndServe(protoFunc ...tp.ProtoFunc) (err error) {
return errors.New("Invalid network config, refer to the following: tcp, tcp4, tcp6")
case "tcp", "tcp4", "tcp6":
}
srv.Handle(srv.pattern, NewServeHandler(srv.Peer, srv.handshake, protoFunc...))
srv.Handle(srv.rootPath, NewServeHandler(srv.Peer, srv.handshake, protoFunc...))
addr := srv.cfg.ListenerAddr()
if addr == "" {
if srv.Peer.TLSConfig() != nil {
Expand Down Expand Up @@ -115,15 +115,15 @@ func (srv *Server) SetHandshake(handshake func(*ws.Config, *http.Request) error)
srv.handshake = handshake
}

// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (srv *Server) Handle(pattern string, handler http.Handler) {
srv.serveMux.Handle(pattern, handler)
// Handle registers the handler for the given rootPath.
// If a handler already exists for rootPath, Handle panics.
func (srv *Server) Handle(rootPath string, handler http.Handler) {
srv.serveMux.Handle(rootPath, handler)
}

// HandleFunc registers the handler function for the given pattern.
func (srv *Server) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
srv.serveMux.HandleFunc(pattern, handler)
// HandleFunc registers the handler function for the given rootPath.
func (srv *Server) HandleFunc(rootPath string, handler func(http.ResponseWriter, *http.Request)) {
srv.serveMux.HandleFunc(rootPath, handler)
}

// NewJSONServeHandler creates a websocket json handler.
Expand Down
10 changes: 5 additions & 5 deletions mixer/websocket/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (p *P) Divide(arg *Arg) (int, *tp.Rerror) {
}

func TestJSONWebsocket(t *testing.T) {
srv := ws.NewServer("/ws", tp.PeerConfig{ListenPort: 9090})
srv := ws.NewServer("/", tp.PeerConfig{ListenPort: 9090})
srv.RouteCall(new(P))
go srv.ListenAndServe()

time.Sleep(time.Second * 1)

cli := ws.NewClient(":9090", "/ws", tp.PeerConfig{})
sess, err := cli.Dial()
cli := ws.NewClient("/", tp.PeerConfig{})
sess, err := cli.Dial(":9090")
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,9 +55,9 @@ func TestPbWebsocketTLS(t *testing.T) {

time.Sleep(time.Second * 1)

cli := ws.NewClient(":9091", "/ws", tp.PeerConfig{})
cli := ws.NewClient("/ws", tp.PeerConfig{})
cli.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
sess, err := cli.DialProtobuf()
sess, err := cli.DialProtobuf(":9091")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f8770ad

Please sign in to comment.