Couldn't able to establish Web Socket connection when server and client running as containers in different hosts

62 views
Skip to first unread message

thatipelli santhosh

unread,
Feb 21, 2021, 3:48:53 PM2/21/21
to golang-nuts
Hi Everyone,

Server and client both are developed in go. I am using github.com/gorilla/websocket library to establish a web socket connection. 
My server is running on port 8080 as docker container and my client (cli tool) is running as docker container in another host. When the client requests the server with "WS://domain_name:8080/api/connect" to establish ws connection, nothing is happening even an error also not occurred at both ends. It is working good in localhost but when the server and client are running as containers, it is not working as expected.

If anyone has any idea to solve this issue,please let me know. 

Any help is appreciated. Thank you!!

Sample server code:
var upgrader = websocket.Upgrader{} // use default options
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}






Sample client code:

var addr = flag.String("addr", "localhost:8080", "http service address")
func main() {
flag.Parse()
log.SetFlags(0)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "ws", Host: *addr, Path: "/echo"}
log.Printf("connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
Reply all
Reply to author
Forward
0 new messages