0

How I can implement this kind of scenario:

1.I have LoginHandler which receives some user data - email and signedXml:

func LoginHandler(c *gin.Context) {
    var (
        err  error
        data LoginPost
    )
    if err = c.BindJSON(&data); err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"status": "error"})
        return
    }

    ...

    c.JSON(http.StatusOK, gin.H{"status": "ok"})
}

2.I need to send signedXml to another server via websocket

3.Save result (success or error)

4.Close connection

Every HTTP request will open connection, send 1 message, get 1 result and finally close socket. I was trying with channel, but no success. Is this possible to implement my case?

UPDATE

package main

import (
    "log"
    "net/url"

    "github.com/gorilla/mux"
    "github.com/gorilla/websocket"
    "net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
    message := r.FormValue("message")
    w.Write([]byte(message))
}

func postHandler(w http.ResponseWriter, r *http.Request) {
    var (
        message = r.FormValue("message")
        u = url.URL{Scheme: "ws", Host: "echo.websocket.org", Path: "/"}
        err error
        out []byte
        conn *websocket.Conn
    )
    log.Printf("message: %s\n", message)
    log.Printf("connecting to %s\n", u.String())
    conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
    if err != nil {
        log.Fatal("dial:", err)
    }
    log.Println("write")
    if err = conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
        log.Fatal("write:", err)
    }
    log.Println("read")
    if _, out, err = conn.ReadMessage(); err != nil {
        log.Fatal("read:", err)
    }
    w.Write(out)
    log.Println("close")
    conn.Close()
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", indexHandler).Methods("GET")
    r.HandleFunc("/post", postHandler).Methods("POST")
    http.Handle("/", r)
    http.ListenAndServe(":8080", nil)
}
2
  • Are you able to change the other server? Websockets don't really seem appropriate for this situation. Commented Oct 5, 2017 at 13:21
  • I tried example from github.com/gorilla/websocket/blob/master/examples/echo/…. But they listen until interrupt signal received and then close connection. How I can rewrite that example without channels and for-select statements? Commented Oct 5, 2017 at 14:35

1 Answer 1

3

Call Dial, WriteMessage, ReadMessage and Close in sequence.

c, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
    // handle error
}
err := c.WriteMessage(websocket.TextMessage, signedXML)
if err != nil {
     // handle error
}
_, p, err := c.ReadMessage()
if err != nil {
     // handle error
}
c.Close()

// p is a []byte with the first received message.
Sign up to request clarification or add additional context in comments.

1 Comment

I tested code above (UPDATE*) and it freezes after conn.ReadMessage()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.