我正在尝试启动chrome浏览器客户端和服务器之间的websocket连接。
我的实施概述: 有一组不同的启动和运行项目。所有其他项目的负载均衡器,它处理所有其他子项目的http请求,路由和代理。这些所有项目都使用负载平衡器。我的尝试是创建一个从chrom浏览器到一个子项目的websocket连接。
球童版本:0.9.3
websocket go库: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)
首先,当客户端访问该域时,请输入$ DOMAIN_NAME,例如www.sample.com。球童将客户请求代理到https://localhost:8107/analytics
https://{$DOMAIN_NAME}/analytics/ {
tls pem_file key_file
proxy / https://localhost:8105/analytics {
websocket
insecure_skip_verify
}
}
有多个独立服务器。以下是服务器的caddy配置,它在 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
}
}
在分析子项目中,“ / api / ws”将触发CreateSocketConnection()方法。
//Starting the API server
router := routes.NewRouter()
http.Handle("/", router)
http.HandleFunc("/api/ws", api.CreateSocketConnection)
CreateSocketConnection实现:
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)
}
客户端实现:
conn = new WebSocket("wss://xxxx.com/analytics/api/ws");
goso后端服务器中发生Websocket升级错误
2018/06/21 16:53:41 http:来自127.0.0.1:47216的TLS握手错误: 远程错误:tls:证书错误
我如何在同一台Web服务器上使用此安全的https和websocket。可以吗?
网络请求:“ wss://xxxx.com/analytics/api/ws”失败:在执行期间出错 WebSocket握手:意外的响应代码:502
我在实现过程中缺少什么吗?
预先感谢