mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add wss example for websocket
This commit is contained in:
@ -350,7 +350,10 @@ func (s *Server) GetRouteMap() string {
|
||||
}
|
||||
addr := s.config.Addr
|
||||
if s.config.HTTPSAddr != "" {
|
||||
addr += ",tls" + s.config.HTTPSAddr
|
||||
if len(addr) > 0 {
|
||||
addr += ","
|
||||
}
|
||||
addr += "tls" + s.config.HTTPSAddr
|
||||
}
|
||||
for _, a := range m {
|
||||
data := make([]string, 8)
|
||||
|
||||
@ -35,7 +35,7 @@
|
||||
}
|
||||
|
||||
$(function () {
|
||||
var url = "ws://127.0.0.1:8199/ws";
|
||||
var url = "wss://127.0.0.1:8199/wss";
|
||||
var ws = new WebSocket(url);
|
||||
try {
|
||||
// ws连接成功
|
||||
|
||||
@ -3,12 +3,13 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := g.Server()
|
||||
s.BindHandler("/ws", func(r *ghttp.Request) {
|
||||
s.BindHandler("/wss", func(r *ghttp.Request) {
|
||||
ws, err := r.WebSocket()
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
@ -24,6 +25,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
})
|
||||
s.SetServerRoot(gfile.MainPkgPath())
|
||||
s.EnableHTTPS("../../https/server.crt", "../../https/server.key")
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/net/ghttp"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/glog"
|
||||
)
|
||||
|
||||
@ -24,6 +25,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
})
|
||||
s.SetServerRoot(gfile.MainPkgPath())
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user