package wsutilimport ()// DebugUpgrader is a wrapper around ws.Upgrader. It tracks I/O of a// WebSocket handshake.//// Note that it must not be used in production applications that requires// Upgrade() to be efficient.typeDebugUpgraderstruct {// Upgrader contains upgrade to WebSocket options. Upgrader ws.Upgrader// OnRequest and OnResponse are the callbacks that will be called with the // HTTP request and response respectively. OnRequest, OnResponse func([]byte)}// Upgrade calls Upgrade() on underlying ws.Upgrader and tracks I/O on conn.func ( *DebugUpgrader) ( io.ReadWriter) ( ws.Handshake, error) {var (// Take the Reader and Writer parts from conn to be probably replaced // below.io.Reader = io.Writer = )if := .OnRequest; != nil {varbytes.Buffer// First, we must read the entire request. , := http.ReadRequest(bufio.NewReader(io.TeeReader(, &), ))if == nil {// Fulfill the buffer with the response body.io.Copy(ioutil.Discard, .Body) .Body.Close() } (.Bytes()) = io.MultiReader( &, , ) }if := .OnResponse; != nil {varbytes.Buffer// Intercept the response stream written by the Upgrade(). = io.MultiWriter( , &, )deferfunc() { (.Bytes()) }() }return .Upgrader.Upgrade(struct {io.Readerio.Writer }{, })}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.