-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathws_server.go
40 lines (32 loc) · 1003 Bytes
/
ws_server.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
package main
import (
"github.com/better-go/pkg/log"
"github.com/better-go/pkg/net/ws"
"net/http"
)
func main() {
// TODO: need absolute path, change here! ! !
homePage := "/Users/henry/Documents/iSpace/better-go/pkg/net/ws/example/ws.html"
log.Infof("ws html file path: %v", homePage)
s := ws.NewWebSocketServer(
func(receivedMessage []byte) (responseMessage []byte, err error) {
log.Infof("ws server: receive message: %v", string(receivedMessage))
resp := string(receivedMessage) + " >>> echo from server"
responseMessage = []byte(resp)
return responseMessage, nil
},
func(apiKey string) bool {
//return false // TODO: test ws auth
return true
},
)
// ws echo endpoint:
http.Handle("/echo", s.DispatchWithAuth())
// home page:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, homePage)
})
log.Infof("ready to run, please check web browser: localhost:8080")
// http server:
http.ListenAndServe(":8080", nil)
}