-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
102 lines (85 loc) · 2.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"fmt"
"net/http"
"log"
"os"
)
var addr = flag.String("addr", ":80", "http service address")
var addrs = flag.String("addrs", ":443", "https service address")
func init() {
file := "./" +"signal"+ ".log"
logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
if err != nil {
panic(err)
}
log.SetOutput(logFile) // 将文件设置为log输出的文件
log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
return
}
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", 404)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", 405)
return
}
http.ServeFile(w, r, "home.html")
}
var hub *Hub
func main() {
flag.Parse()
hub = newHub()
go hub.run()
//http.HandleFunc("/", serveHome)
http.HandleFunc("/ws", wsHandler)
http.HandleFunc("/wss", wsHandler)
http.HandleFunc("/", wsHandler)
http.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) {
//fmt.Printf("URL: %s\n", r.URL.String())
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write([]byte(fmt.Sprintf("%d", hub.ClientNum)))
})
if Exists("cert/crt.pem") && Exists("cert/crt.key") {
go func() {
log.Printf("Start to listening the incoming requests on https address: %s\n", *addrs)
err := http.ListenAndServeTLS(*addrs, "cert/crt.pem", "cert/crt.key", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
}
log.Printf("Start to listening the incoming requests on http address: %s\n", *addr)
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
//fmt.Printf("URL: %s\n", r.URL.String())
r.ParseForm()
defer func() { // 必须要先声明defer,否则不能捕获到panic异常
if err := recover(); err != nil {
log.Println(err) // 这里的err其实就是panic传入的内容
}
}()
id := r.Form.Get("id")
if id != "" {
serveWs(hub, w, r, id)
}
}
// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}