Add examples folder with ping-pong example and Makefile
This commit is contained in:
parent
1e395f69ef
commit
78b04e2f0c
5
Makefile
Normal file
5
Makefile
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
ping-pong: pp-server pp-client
|
||||||
|
@
|
||||||
|
|
||||||
|
pp-client pp-server:
|
||||||
|
go build -o ./bin/$@ ./examples/ping-pong/$@/
|
26
examples/ping-pong/pp-client/main.go
Normal file
26
examples/ping-pong/pp-client/main.go
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.qowevisa.me/Qowevisa/tcpmachine/tcpclient"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conf := tcpclient.GetDefaultConfig()
|
||||||
|
client := tcpclient.CreateClient(conf)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if client.IsConnected {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
client.Server.Write([]byte("PING\n"))
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go client.ErrorResolver(client.ErrorsChannel)
|
||||||
|
client.StartClient("127.0.0.1:10000")
|
||||||
|
}
|
39
examples/ping-pong/pp-server/main.go
Normal file
39
examples/ping-pong/pp-server/main.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.qowevisa.me/Qowevisa/tcpmachine/tcpcommand"
|
||||||
|
"git.qowevisa.me/Qowevisa/tcpmachine/tcpserver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
bundle, err := tcpcommand.CreateCommandBundle([]tcpcommand.Command{
|
||||||
|
{
|
||||||
|
Command: "PING",
|
||||||
|
Action: func(s []string, client net.Conn) {
|
||||||
|
fmt.Printf("todo..\n")
|
||||||
|
client.Write([]byte("PONG\n"))
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
conf := tcpserver.GetDefaultConfig()
|
||||||
|
handler, errorChannel := tcpserver.CreateHandleClientFuncFromCommands(bundle, conf)
|
||||||
|
go func() {
|
||||||
|
for err := range errorChannel {
|
||||||
|
fmt.Printf("Error:1: %v\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
conf.HandleClientFunc = handler
|
||||||
|
server := tcpserver.CreateServer(conf)
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
server.Exit <- true
|
||||||
|
}()
|
||||||
|
server.StartServer("127.0.0.1:10000")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user