add websocket compression support (#56)

This commit is contained in:
septs 2016-11-09 18:07:01 +08:00 committed by ginuerzh
parent 1a88ca09f5
commit e22229b5b1

25
ws.go
View File

@ -22,11 +22,11 @@ func NewWebsocketServer(base *ProxyServer) *WebsocketServer {
Addr: base.Node.Addr, Addr: base.Node.Addr,
Base: base, Base: base,
upgrader: websocket.Upgrader{ upgrader: websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true }, CheckOrigin: func(r *http.Request) bool { return true },
EnableCompression: true,
}, },
CompressionSupported: true,
} }
} }
@ -75,14 +75,14 @@ type WebsocketConn struct {
func WebsocketClientConn(url string, conn net.Conn, config *tls.Config) (*WebsocketConn, error) { func WebsocketClientConn(url string, conn net.Conn, config *tls.Config) (*WebsocketConn, error) {
dialer := websocket.Dialer{ dialer := websocket.Dialer{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
TLSClientConfig: config, TLSClientConfig: config,
HandshakeTimeout: DialTimeout, HandshakeTimeout: DialTimeout,
EnableCompression: true,
NetDial: func(net, addr string) (net.Conn, error) { NetDial: func(net, addr string) (net.Conn, error) {
return conn, nil return conn, nil
}, },
CompressionSupported: true,
} }
c, resp, err := dialer.Dial(url, nil) c, resp, err := dialer.Dial(url, nil)
@ -90,11 +90,11 @@ func WebsocketClientConn(url string, conn net.Conn, config *tls.Config) (*Websoc
return nil, err return nil, err
} }
resp.Body.Close() resp.Body.Close()
c.EnableWriteCompression(true)
return &WebsocketConn{conn: c}, nil return &WebsocketConn{conn: c}, nil
} }
func WebsocketServerConn(conn *websocket.Conn) *WebsocketConn { func WebsocketServerConn(conn *websocket.Conn) *WebsocketConn {
conn.EnableWriteCompression(true)
return &WebsocketConn{ return &WebsocketConn{
conn: conn, conn: conn,
} }
@ -106,17 +106,12 @@ func (c *WebsocketConn) Read(b []byte) (n int, err error) {
} }
n = copy(b, c.rb) n = copy(b, c.rb)
c.rb = c.rb[n:] c.rb = c.rb[n:]
//log.Println("ws r:", n)
return return
} }
func (c *WebsocketConn) Write(b []byte) (n int, err error) { func (c *WebsocketConn) Write(b []byte) (n int, err error) {
err = c.conn.WriteMessage(websocket.BinaryMessage, b) err = c.conn.WriteMessage(websocket.BinaryMessage, b)
n = len(b) n = len(b)
//log.Println("ws w:", n)
return return
} }