I’m trying to initiate a websocket connection between chrome browser client and server.
Overview of my implementation : There are set of different up and running projects. The load balancer to all other projects and it handle all http requests, routes and proxy to other sub projects. These all projects use load balancers. My attempt is to create a websocket connection from chrom browser to one sub project.
caddy version : 0.9.3
websocket go library : github.com/gorilla/websocket 1
// Starting caddy server to server static files
args := []string{"bin/caddy", "--conf=" + commons.ServerConfigurations.CaddyFile, "-pidfile=bin/caddy.pid"}
if err := exec.Command("nohup", args...).Start(); err != nil {
log.Fatalln("Error occourred while starting caddy server : ", err.Error())
os.Exit(1)
}
//Starting the API server
router := routes.NewRouter()
http.Handle("/", router)
httpsServer := &http.Server{
Addr: ":" + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
commons.ServerConfigurations.PortOffset),
Handler: logHandler,
ReadTimeout: time.Duration(commons.ServerConfigurations.ReadTimeOut) * time.Second,
WriteTimeout: time.Duration(commons.ServerConfigurations.WriteTimeOut) * time.Second,
MaxHeaderBytes: 1 << 20,
}
httpsServer.ListenAndServeTLS(commons.ServerConfigurations.SSLCertificateFile,
commons.ServerConfigurations.SSLKeyFile)
First when client reach to the domain, $DOMAIN_NAME such as www.sample.com. Caddy proxy the client request to the https://localhost:8107/analytics
https://{$DOMAIN_NAME}/analytics/ {
tls pem_file key_file
proxy / https://localhost:8105/analytics {
websocket
insecure_skip_verify
}
}
There are multiple standalone servers. Following is the caddy configuration of server.It serve static files at the directory in webapps/analytics
localhost:8105/analytics {
root webapps/analytics
gzip
ext .html
tls pem_file key_file
proxy /api https://localhost:8108 { HTTPS port:8108
websocket
insecure_skip_verify
}
}
Inside the analytics sub project, " /api/ws " would trigger CreateSocketConnection() method.
//Starting the API server
router := routes.NewRouter()
http.Handle("/", router)
http.HandleFunc("/api/ws", api.CreateSocketConnection)
CreateSocketConnection implementation :
func CreateSocketConnection(w http.ResponseWriter, r *http.Request) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
_, err = upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal("upgrader failed :", err.Error())
}
//controllers.HandleSocket(ws)
}
Client side implementation :
conn = new WebSocket("wss://xxxx.com/analytics/api/ws");
Websocket upgrade error happen in golang backend server
2018/06/21 16:53:41 http: TLS handshake error from 127.0.0.1:47216: remote error: tls: bad certificate
How I use this secure https and websocket in same web server.Is it posible ?
web request : 'wss://xxxx.com/analytics/api/ws' failed: Error during WebSocket handshake: Unexpected response code: 502
Am I missing something in my implementation?
Thanks in advance