I've been able to forward telnet over a websocket using golang, using something like

func forwardtcp(wsconn *websocket.Conn, conn *telnet.Conn) {
    connbuf := bufio.NewReader(conn)
    tcpbuffer := make([]byte, 128)

    for {
        n, err := connbuf.Read(tcpbuffer)

        if err != nil {
            log.Println("TCP Read failed")
            break
        }
        if err == nil {
            wsconn.WriteMessage(websocket.BinaryMessage, tcpbuffer[:n])
        }
    }
}

However I'm unable to do similar with an SSH or shell session. I'm not understanding a fundamental concept with the using the

targetStdout, _ := session.StdoutPipe()
targetStdin, _ := session.StdinPipe()

pieces.

I am able to use io.Copy, but not sure how to format these into a datagram that can be sent with the websocket connection.

Is it possible to treat the targetStdin and targetStdout pipes in a manner that they can be read and written to with bytes, such as those received from the websocket connection? Or is there a better approach to get io from the SSH connection?