-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.go
68 lines (56 loc) · 1.63 KB
/
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
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
package main
import (
"fmt"
"github.com/Ox1O5/trill/trill"
)
type pingRouter struct {
trill.BaseRouter
}
func (this *pingRouter) Handle(request trill.IRequest) {
fmt.Println("Call pingRouter Handle")
fmt.Println("receive from client : msgID = ", request.GetMsgID(),
" data = ", string(request.GetData()))
err := request.GetConnection().SendMsg(0, []byte("ping ping ping...\n"))
if err != nil {
fmt.Println("SendMsg error ", err)
}
}
type helloRouter struct {
trill.BaseRouter
}
func (h *helloRouter) Handle(request trill.IRequest) {
fmt.Println("Call helloRouter Handle")
fmt.Println("receive from client : msgID = ", request.GetMsgID(),
" data = ", string(request.GetData()))
err := request.GetConnection().SendMsg(1, []byte("hello hello hello...\n"))
if err != nil {
fmt.Println("SendMsg error ", err)
}
}
func doConnectionBegin(conn trill.IConnection) {
fmt.Println("DoConnecionBegin is Called ... ")
fmt.Println("Set conn Name, Home done!")
conn.SetProperty("Name", "0x105")
conn.SetProperty("Home", "Ox105.github.io")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
fmt.Println(err)
}
}
func doConnectionLost(conn trill.IConnection) {
if name, err:= conn.GetProperty("Name"); err == nil {
fmt.Println("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
fmt.Println("Conn Property Home = ", home)
}
fmt.Println("DoConnectionLost is Called ... ")
}
func main() {
s := trill.NewServer("[trill v0.9]")
s.SetOnConnStart(doConnectionBegin)
s.SetOnConnStop(doConnectionLost)
s.AddRouter(0, &pingRouter{})
s.AddRouter(1, &helloRouter{})
s.Server()
}