You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.7 KiB
Go
162 lines
3.7 KiB
Go
package notify
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_usechannel(t *testing.T) {
|
|
server, err := NewNotifyS("udp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
server.SetNotify("nihao", func(data SMsg) string {
|
|
fmt.Println("server recv:", data.Key, data.Value)
|
|
if data.Value != "" {
|
|
data.Reply("nba")
|
|
return "nb"
|
|
}
|
|
return ""
|
|
})
|
|
client, err := NewNotifyC("udp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
client.UseChannel = true
|
|
//time.Sleep(time.Second * 10)
|
|
client.Send("nihao")
|
|
client.SendValue("nihao", "lalala")
|
|
txt := <-client.Notify("nihao")
|
|
fmt.Println("client", txt)
|
|
txt = <-client.Notify("nihao")
|
|
fmt.Println("client", txt)
|
|
server.ServerStop()
|
|
<-client.Stoped()
|
|
client.ClientStop()
|
|
time.Sleep(time.Second * 3)
|
|
}
|
|
|
|
func Test_nochannel(t *testing.T) {
|
|
server, err := NewNotifyS("tcp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
server.SetNotify("nihao", func(data SMsg) string {
|
|
fmt.Println("server recv:", data.Key, data.Value)
|
|
if data.Value != "" {
|
|
data.Reply("nbaz")
|
|
return ""
|
|
}
|
|
return ""
|
|
})
|
|
client, err := NewNotifyC("tcp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
//time.Sleep(time.Second * 10)
|
|
client.UseChannel = false
|
|
client.SetNotify("nihao", func(data CMsg) {
|
|
fmt.Println("client recv:", data.Key, data.Value)
|
|
if data.Value != "" {
|
|
time.Sleep(time.Millisecond * 900)
|
|
client.SendValue("nihao", "dsb")
|
|
}
|
|
})
|
|
client.SendValue("nihao", "lalala")
|
|
time.Sleep(time.Second * 3)
|
|
server.ServerStop()
|
|
<-client.Stoped()
|
|
client.ClientStop()
|
|
time.Sleep(time.Second * 3)
|
|
}
|
|
|
|
func Test_pipec(t *testing.T) {
|
|
server, err := NewNotifyS("tcp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
server.SetAesKey([]byte("abcdefg123456789"))
|
|
server.SetNotify("ni\\||hao", func(data SMsg) string {
|
|
fmt.Println("name-get", data.GetName())
|
|
fmt.Println("name-set", data.SetName("iiiis"))
|
|
fmt.Println("name-get", data.GetName())
|
|
fmt.Println("server recv:", data.Key, data.Value, data.mode)
|
|
if data.Value != "" {
|
|
data.Reply("nba")
|
|
return ""
|
|
}
|
|
return ""
|
|
})
|
|
client, err := NewNotifyC("tcp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
client.SetAesKey([]byte("abcdefg123456789"))
|
|
client.UseChannel = false
|
|
sa, err := client.SendValueWait("ni\\||hao", "lalaeee", time.Second*10)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(sa)
|
|
sa, err = client.SendValueWait("ni\\||hao", "lalasdeee", time.Second*10)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(sa)
|
|
fmt.Println("sukidesu")
|
|
time.Sleep(time.Second * 3)
|
|
server.ServerStop()
|
|
<-client.Stoped()
|
|
client.ClientStop()
|
|
time.Sleep(time.Second * 2)
|
|
}
|
|
|
|
func Test_pips(t *testing.T) {
|
|
var testmsg SMsg
|
|
server, err := NewNotifyS("udp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
server.SetNotify("nihao", func(data SMsg) string {
|
|
fmt.Println("server recv:", data.Key, data.Value, data.mode)
|
|
testmsg = data
|
|
if data.Value != "" {
|
|
data.Reply("nbaz")
|
|
return ""
|
|
}
|
|
return ""
|
|
})
|
|
client, err := NewNotifyC("udp", "127.0.0.1:1926")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
//time.Sleep(time.Second * 10)
|
|
client.UseChannel = false
|
|
client.SetNotify("nihao", func(data CMsg) {
|
|
fmt.Println("client recv:", data.Key, data.Value, data.mode)
|
|
if data.mode != "pa" {
|
|
time.Sleep(time.Millisecond * 1200)
|
|
client.ReplyMsg(data, "nihao", "dsb")
|
|
}
|
|
})
|
|
client.SendValue("nihao", "lalala")
|
|
time.Sleep(time.Second * 3)
|
|
fmt.Println(server.SendWait(testmsg, "nihao", "wozuinb", time.Second*20))
|
|
fmt.Println("sakura")
|
|
server.ServerStop()
|
|
<-client.Stoped()
|
|
client.ClientStop()
|
|
time.Sleep(time.Second * 3)
|
|
}
|