Giter Site home page Giter Site logo

Comments (2)

garyburd avatar garyburd commented on May 18, 2024 1

Recoverable timeouts are difficult to implement. I am not aware of other Go network protocol libraries that support recoverable timeouts.

The application can implement timeout polling in a net.Conn shim between the websocket.Conn and the underlying net.Conn. A sketch for creating the shim is:

type appResponseWriter struct {  http.ResponseWriter }

func (w appResponseWriter)  Hijack() (net.Conn, *bufio.ReadWriter, error) {
    if h, ok := w.ResponseWriter.(http.Hijacker); ok {
        c, brw, err := h.Hijack()
        c = appConn(c)
        return c, brw, err
   } else {
        return nil, nil, errors.New("not supported")
   }
}

type appConn struct {
    net.Conn
}

func (c appConn) Read(p []byte) (int, err) {
    // implement timeout polling here.
}

func handler(w http.ResponseWriter, r *http.Request) {
    ws, err := websocket.Upgrade(appResponseWriter{w}, r, nil, 1024, 1024)
    if _, ok := err.(websocket.HandshakeError); ok {
        http.Error(w, "Not a websocket handshake", 400)
        return
    } else if err != nil {
        log.Println(err)
        return
    }
    ... Use conn to send and receive messages.
}

from websocket.

garyburd avatar garyburd commented on May 18, 2024

It is not idiomatic to poll connections with timeouts in Go.

Close the websocket connection one minute after writing the close message. This will cause a blocked reader to return with an error.

from websocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.