123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- //go:build ignore
- // +build ignore
- package main
- import (
- "errors"
- "log"
- "os"
- "os/signal"
- "regexp"
- "syscall"
- "time"
- "git.me9.top/git/tinymq"
- "git.me9.top/git/tinymq/config"
- )
- func main() {
- cf := config.NewConfig()
- localChannel := "/tinymq/server"
- remoteChannel := "/tinymq/client"
- var hub *tinymq.Hub
- hub = tinymq.NewHub(cf, localChannel,
- func(channel string, proxy bool) (hostInfo *tinymq.HostInfo, err error) {
- return nil, errors.New("not host found")
- }, 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, version uint8, channel string, auth []byte) bool {
- return string(auth) == "tinymq"
- }, func(conn *tinymq.Line) {
- log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Updated()))
- if conn.State() == tinymq.Connected {
- go hub.Get(regexp.MustCompile(remoteChannel), "hello", []byte("hello from server push"),
- func(response *tinymq.ResponseData) (ok bool) {
- log.Println("[hello response]", response.State, string(response.Data))
- return true
- })
- go hub.Get(regexp.MustCompile(remoteChannel), "nodata", nil,
- func(response *tinymq.ResponseData) (ok bool) {
- log.Println("[nodata response]", response.State, string(response.Data))
- return true
- })
- }
- },
- )
- // tcp2协议
- bindTpv2Info := &tinymq.HostInfo{
- Proto: "tcp",
- Version: 2,
- Bind: "127.0.0.1",
- Port: 34222,
- Hash: "xor:1qaz2wsx3",
- }
- hub.BindForServer(bindTpv2Info)
- // ws2协议
- bindws2Info := &tinymq.HostInfo{
- Proto: "ws",
- Version: 2,
- Bind: "127.0.0.1",
- Port: 34211,
- Path: "/tinymq-xor",
- Hash: "xor:1qaz2wsx3edc",
- }
- hub.BindForServer(bindws2Info)
- // ws2协议,没有加密算法
- bindInfo := &tinymq.HostInfo{
- Proto: "ws",
- Version: 2,
- // Bind: "127.0.0.1",
- Port: 34211,
- Path: "/tinymq",
- }
- hub.BindForServer(bindInfo)
- // 中间件
- hub.UseMiddle(func(request *tinymq.RequestData) (response *tinymq.ResponseData) {
- log.Println("[Middle]", request.Conn().Channel(), request.Cmd)
- return nil
- })
- // 订阅频道
- hub.Subscribe(regexp.MustCompile(remoteChannel), "hello",
- func(request *tinymq.RequestData) (state uint8, result []byte) {
- log.Println("[server RECV]<-", string(request.Data))
- return 1, []byte("tiny server")
- },
- )
- hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata",
- func(request *tinymq.RequestData) (state uint8, result []byte) {
- log.Println("[server RECV]<-", string(request.Data))
- return 1, nil
- },
- )
- hub.Subscribe(regexp.MustCompile(remoteChannel), "push",
- func(request *tinymq.RequestData) (state uint8, result []byte) {
- log.Println("[server RECV]<-", string(request.Data))
- return 1, nil
- },
- )
- // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindws2Info.Bind, strconv.Itoa(int(bindws2Info.Port))), nil))
- // 初始化一个channel
- exit := make(chan os.Signal, 3)
- //notify方法用来监听收到的信号
- signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
- sig := <-exit
- log.Println("[Exist with]", sig.String())
- }
|