Add examples folder with ping-pong example and Makefile

This commit is contained in:
qowevisa 2024-10-10 08:32:41 +03:00
parent 1e395f69ef
commit 78b04e2f0c
3 changed files with 70 additions and 0 deletions

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
ping-pong: pp-server pp-client
@
pp-client pp-server:
go build -o ./bin/$@ ./examples/ping-pong/$@/

View 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")
}

View 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")
}