Skip to content

Commit fb7dc72

Browse files
authored
Merge pull request #20 from Tom32i/feat/better-closings
Feat/better closings
2 parents 43980a1 + ed37504 commit fb7dc72

3 files changed

Lines changed: 664 additions & 632 deletions

File tree

go/server.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,21 @@ import (
77
"net/http"
88
)
99

10-
func Start(port int, path string, onSocket func(*websocket.Conn, *http.Request)) {
10+
func Start(port int, path string, onSocket func(w http.ResponseWriter, r *http.Request, c *websocket.Conn), checkOrigin func(r *http.Request) bool, errorHandler func(w http.ResponseWriter, r *http.Request, status int, reason error)) {
1111
upgrader := &websocket.Upgrader{
1212
ReadBufferSize: 1024,
1313
WriteBufferSize: 1024,
1414
Subprotocols: []string{"websocket"},
15-
Error: ErrorHandler,
16-
CheckOrigin: CheckOrigin,
15+
Error: errorHandler,
16+
CheckOrigin: checkOrigin,
1717
}
1818

1919
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
2020
s, err := upgrader.Upgrade(w, r, nil)
2121

22-
if err != nil {
23-
log.Printf("Could not upgrade connection: %s", err)
24-
return
22+
if err == nil {
23+
onSocket(w, r, s)
2524
}
26-
27-
onSocket(s, r)
2825
})
2926

3027
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
@@ -39,5 +36,5 @@ func CheckOrigin(r *http.Request) bool {
3936
}
4037

4138
func ErrorHandler(w http.ResponseWriter, r *http.Request, status int, reason error) {
42-
log.Printf("Error: %v %v", status, reason)
39+
http.Error(w, reason.Error(), status)
4340
}

0 commit comments

Comments
 (0)