From f8770ad0d0a112debb1012d0295e32a919261c81 Mon Sep 17 00:00:00 2001 From: henrylee2cn Date: Sat, 16 Feb 2019 06:42:31 +0800 Subject: [PATCH] chore(websocket): update Change-Id: I799fa54d219b91bace65d521e5d4ba785af6bfa2 --- mixer/websocket/README.md | 10 +++---- mixer/websocket/client.go | 43 +++++++++++++++---------------- mixer/websocket/server.go | 22 ++++++++-------- mixer/websocket/websocket_test.go | 10 +++---- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/mixer/websocket/README.md b/mixer/websocket/README.md index 736fa2f3..357b8b67 100644 --- a/mixer/websocket/README.md +++ b/mixer/websocket/README.md @@ -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) } @@ -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) } diff --git a/mixer/websocket/client.go b/mixer/websocket/client.go index 92eb69be..44b50c34 100644 --- a/mixer/websocket/client.go +++ b/mixer/websocket/client.go @@ -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 ( @@ -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 { diff --git a/mixer/websocket/server.go b/mixer/websocket/server.go index d1ecc48f..f19e2440 100644 --- a/mixer/websocket/server.go +++ b/mixer/websocket/server.go @@ -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}, } } @@ -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 { @@ -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. diff --git a/mixer/websocket/websocket_test.go b/mixer/websocket/websocket_test.go index 6bfcc341..7fa7832c 100644 --- a/mixer/websocket/websocket_test.go +++ b/mixer/websocket/websocket_test.go @@ -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) } @@ -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) }