12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- //go:build ignore
- // +build ignore
- package main
- import (
- "log"
- "regexp"
- "time"
- "git.me9.top/git/tinymq"
- "git.me9.top/git/tinymq/config"
- )
- func main() {
- cf := config.NewConfig()
- localChannel := "/tinymq/client/ws2"
- remoteChannel := "/tinymq/server"
- host := &tinymq.HostInfo{
- Proto: "ws",
- Version: 2,
- Host: "127.0.0.1",
- Port: 34211,
- // Path: "/tinymq",
- Path: "/tinymq-xor",
- Hash: "xor:1qaz2wsx3edc",
- }
- hub := tinymq.NewHub(cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
- return host, nil
- }, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
- // 从 remoteAuth 是否为空来判断是否需要返回信息
- if len(remoteAuth) <= 0 {
- // 客户端调用,返回验证信息
- return []byte("tinymq")
- } else {
- // 服务端调用,返回验证token,或者其他信息
- return nil
- }
- }, func(proto string, cversion uint8, hannel string, auth []byte) bool {
- return true
- }, func(conn *tinymq.Line) {
- log.Println("connect state", conn.Channel(), conn.State(), time.Since(conn.Updated()))
- })
- // 订阅频道
- hub.Subscribe(regexp.MustCompile(remoteChannel), "hello", func(request *tinymq.RequestData) (state uint8, result []byte) {
- log.Println("[client RECV]<-", string(request.Data))
- return 1, []byte("tiny client")
- },
- )
- hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata", func(request *tinymq.RequestData) (state uint8, result []byte) {
- log.Println("[client RECV]<-", string(request.Data))
- return 1, nil
- },
- )
- err := hub.ConnectToServer(remoteChannel, true, nil)
- if err != nil {
- log.Fatalln("[client ConnectToServer ERROR]", err)
- }
- // 获取信息
- rsp := hub.GetOne(regexp.MustCompile(remoteChannel), "hello", []byte("hello from client"))
- if rsp.State != config.STATE_OK {
- log.Println("error state:", rsp.State)
- return
- }
- log.Println("[RESULT]<-", string(rsp.Data))
- time.Sleep(time.Second * 5)
- hub.Push(regexp.MustCompile(remoteChannel), "push", []byte(time.Now().GoString()))
- time.Sleep(time.Second * 300)
- log.Println("client exit")
- }
|