-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
57 lines (52 loc) · 1.34 KB
/
server_test.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
package peregrine_test
import (
"fmt"
"github.com/RealFax/peregrine"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/panjf2000/gnet/v2"
"net/http"
"net/url"
"testing"
"time"
)
func Handler(req *peregrine.Packet) {
// echo
wsutil.WriteServerText(req.Conn, req.Request)
}
func TestServer_ListenAndServer(t *testing.T) {
server := peregrine.NewServer(
"tcp://127.0.0.1:9010",
peregrine.WithUpgrader(&ws.Upgrader{
OnRequest: peregrine.RequestProxy(func(req *url.URL) error {
return nil
}),
OnHost: peregrine.HostProxy(func(host string) error {
return nil
}),
OnHeader: peregrine.HeaderProxy(func(key, value string) error {
return nil
}),
}),
peregrine.WithHandler(Handler),
peregrine.WithOnCloseHandler(func(conn *peregrine.Conn, err error) {
t.Logf("RemoteAddr: %s close, reason: %v", conn.RemoteAddr(), err)
}),
peregrine.WithConnTimeout(15*time.Second),
)
// status monitor
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(fmt.Sprintf("Online: %d", server.ConnTableLen())))
})
t.Log("[+] Monitor server: http://localhost:9090")
http.ListenAndServe("localhost:9090", nil)
}()
if err := server.ListenAndServe(
gnet.WithMulticore(true),
gnet.WithReuseAddr(true),
gnet.WithReusePort(true),
); err != nil {
t.Fatal(err)
}
}