-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
164 lines (137 loc) · 4.19 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (C) 2017 Jennal([email protected]). All rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
package main
import (
"bufio"
"fmt"
"os"
"github.com/jennal/goplay/aop"
"github.com/jennal/goplay/log"
"github.com/jennal/goplay/pkg"
"github.com/jennal/goplay/session"
"github.com/jennal/goplay/transfer"
"github.com/jennal/goplay/transfer/websocket"
)
func init() {
fmt.Println("init-1")
}
func init() {
fmt.Println("init-2")
}
type Handler struct{}
func (self *Handler) OnStarted() {
fmt.Println("Handler-OnStarted")
}
func (self *Handler) OnError(err error) {
fmt.Println("OnError", err)
}
func (self *Handler) OnBeforeStop() {
fmt.Println("OnBeforeStop")
}
func (self *Handler) OnStopped() {
fmt.Println("OnStopped")
}
func (self *Handler) OnNewClient(cli transfer.IClient) {
fmt.Println("Handler-OnNewClient", cli)
go func() {
aop.Recover(func() {
for {
header, body, err := cli.Recv()
log.Logf("Recv:\n\theader => %#v\n\tbody => %#v | %v\n\terr => %v\n", header, body, string(body), err)
err = cli.Send(header, body)
if err != nil {
log.Error(err)
}
}
}, func(err interface{}) {
log.RecoverErrorf("error: %#v", err)
})
}()
}
func (self *Handler) Test(sess *session.Session, line string) *pkg.ErrorMessage {
fmt.Println("Handler-Test", sess, line)
sess.Push("test.push", "Service: "+line)
return nil
}
func (self *Handler) Add(sess *session.Session, n int) (int, *pkg.ErrorMessage) {
fmt.Println("Handler-Add", sess, n)
return n + 1, nil
}
func main() {
str := ""
ser := websocket.NewServer("localhost", 9990)
ser.RegistDelegate(&Handler{})
// serv := service.NewService("test", ser)
// serv.RegistHanlder(&Handler{})
err := ser.Start()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("server started:", ser.Addr())
fmt.Scanf("%s", &str)
cli := websocket.NewClient()
cli.Connect("localhost", 9990)
idGen := pkg.NewIDGen()
scanner := bufio.NewScanner(os.Stdin)
// fmt.Println(scanner)
for scanner.Scan() {
// fmt.Println(scanner)
buffer := scanner.Text()
fmt.Println("Send: ", buffer)
cli.Send(pkg.NewHeader(pkg.PKG_NOTIFY, pkg.ENCODING_JSON, idGen, "test.route"), []byte(buffer))
}
// client := service.NewServiceClient(cli)
// client.AddListener("test.push", func(line string) {
// fmt.Println("[test.push] recv: ", line)
// })
// client.Connect("", 9990)
// client.Request("test.handler.add", 1, func(result int) {
// fmt.Println("[test.handler.add] callback: ", result)
// }, func(err *pkg.ErrorMessage) {
// fmt.Println("[test.handler.add] error: ", err)
// })
// client.Notify("test.handler.test", "Hello from Client")
// data, err := json.Marshal("Hello From Client")
// fmt.Println("json encode:", string(data))
// if err != nil {
// fmt.Println("json.Marshal error:", err)
// } else {
// // var str string
// // json.Unmarshal(data, &str)
// // fmt.Println("Unmarshal:", str)
// cli.Send(cli.NewHeader(pkg.PKG_NOTIFY, pkg.ENCODING_JSON, "test.handler.test"), data)
// }
// data, err = json.Marshal(1000)
// fmt.Println("json encode:", string(data))
// if err != nil {
// fmt.Println("json.Marshal error:", err)
// } else {
// cli.Send(cli.NewHeader(pkg.PKG_REQUEST, pkg.ENCODING_JSON, "test.handler.add"), data)
// }
// go func() {
// for i := 0; true; i++ {
// header, body, err := cli.Recv()
// fmt.Printf("[%v] Recv:\n\theader => %#v\n\tbody => %#v | %v\n\terr => %v\n", time.Now(), header, body, string(body), err)
// if err != nil {
// break
// }
// if /*i%4 == 0 &&*/ header.Type == pkg.PKG_HEARTBEAT {
// respHeader := cli.NewHeartBeatResponseHeader(header)
// cli.Send(respHeader, []byte{})
// }
// }
// }()
// fmt.Scanf("%s", nil)
// ser.Stop()
// fmt.Scanf("%s", nil)
}