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

11
ws.go
View File

@ -25,8 +25,8 @@ func NewWebsocketServer(base *ProxyServer) *WebsocketServer {
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,
} }
} }
@ -79,10 +79,10 @@ func WebsocketClientConn(url string, conn net.Conn, config *tls.Config) (*Websoc
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
} }