| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- //go:build ignore
- // +build ignore
- package main
- import (
- "log"
- "time"
- "git.me9.top/git/tinymq"
- "git.me9.top/git/tinymq/config"
- "git.me9.top/git/tinymq/conn"
- )
- func main() {
- cf := config.NewConfig()
- localChannel := "/tinymq/client/tcp2/proxy"
- remoteChannel := "/tinymq/server"
- remoteFilter := tinymq.StrChannelFilter(remoteChannel)
- hostProxy := &conn.HostInfo{
- Proto: "tcp",
- Version: 2,
- Host: "127.0.0.1",
- Port: 44222,
- Hash: "xor:1qaz2wsx3",
- Proxy: true,
- }
- hostServer := &conn.HostInfo{
- Proto: "tcp",
- Version: 2,
- Host: "127.0.0.1",
- Port: 34222,
- Hash: "xor:1qaz2wsx3",
- }
- hub := tinymq.NewHub(
- cf,
- localChannel,
- func(channel string, hostType tinymq.HostType) (hostInfo *conn.HostInfo, err error) {
- log.Println("[connectHostFunc]", channel, hostType)
- if hostType == tinymq.HostTypeDirect {
- return hostServer, nil
- }
- return hostProxy, nil
- },
- func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
- log.Println("[AuthFunc]", host, proto, version, channel, string(remoteAuth))
- return []byte("tinymq-client")
- },
- func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool {
- log.Println("[CheckAuthFunc]", host, proto, version, channel, string(auth))
- return string(auth) == "tinymq-server"
- },
- func(conn *tinymq.Line) {
- log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Started()))
- },
- )
- // 从过滤器到频道字符串
- hub.SetFilterToChannelFunc(func(filter tinymq.FilterFunc) (channel string) {
- return remoteChannel
- })
- // 订阅频道
- hub.Subscribe(remoteFilter, "hello", func(request *tinymq.RequestData) (state uint8, result any) {
- log.Println("[client RECV]<-", string(request.Data))
- return 1, "tiny client"
- },
- )
- hub.Subscribe(remoteFilter, "nodata", func(request *tinymq.RequestData) (state uint8, result any) {
- log.Println("[client RECV]<-", string(request.Data))
- return 1, nil
- },
- )
- // err := hub.ConnectToServer("/tinymq/server", true, nil)
- // if err != nil {
- // log.Fatalln("[client ConnectToServer ERROR]", err)
- // }
- // 获取信息
- rsp := hub.GetOne(remoteFilter, "hello", []byte("hello from client, hello from client, hello from client"))
- if rsp.State != tinymq.STATE_OK {
- log.Println("error state:", rsp.State)
- return
- }
- log.Println("[RESULT]<-", string(rsp.Data))
- log.Println("test finished")
- time.Sleep(time.Second * 10)
- log.Println("client exit")
- }
|