Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save caugello/c3d4db7858ee8ed0bf25d30e68673619 to your computer and use it in GitHub Desktop.

Select an option

Save caugello/c3d4db7858ee8ed0bf25d30e68673619 to your computer and use it in GitHub Desktop.

Revisions

  1. @x32net x32net revised this gist Jun 7, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions go_example_websocket_novnc.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Go Websocket binary proxy noVnc
    package main

    import (
  2. @x32net x32net renamed this gist Jun 7, 2016. 1 changed file with 0 additions and 0 deletions.
  3. @bit4bit bit4bit created this gist Jun 30, 2015.
    60 changes: 60 additions & 0 deletions go_example_websocket_novnc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    package main

    import (
    "flag"
    "golang.org/x/net/websocket"
    "io"
    "log"
    "net"
    "net/http"
    "os"
    )

    var (
    sourceAddr = flag.String("source", "", "source address")
    targeAddr = flag.String("target", "", "target address")
    )

    func main() {
    flag.Parse()
    if *sourceAddr == "" || *targeAddr == "" {
    flag.PrintDefaults()
    os.Exit(1)
    }

    mux := websocket.Server{
    Handshake: bootHandshake,
    Handler: handleWss}

    http.Handle("/websockify", mux)
    err := http.ListenAndServe(*sourceAddr, nil)
    if err != nil {
    log.Fatal(err)
    }
    }

    func handleWss(wsconn *websocket.Conn) {
    log.Println("handlewss")
    conn, err := net.Dial("tcp", *targeAddr)

    if err != nil {
    log.Println(err)
    wsconn.Close()

    } else {
    wsconn.PayloadType = websocket.BinaryFrame

    go io.Copy(conn, wsconn)
    go io.Copy(wsconn, conn)

    select {}
    }
    }

    func bootHandshake(config *websocket.Config, r *http.Request) error {
    config.Protocol = []string{"binary"}

    r.Header.Set("Access-Control-Allow-Origin", "*")
    r.Header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE")
    return nil
    }