Created
December 31, 2021 03:07
-
-
Save wathenjiang/d7603ec539d7e8003a972e67c93564f9 to your computer and use it in GitHub Desktop.
gnet_client_main_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/panjf2000/gnet" | |
"github.com/panjf2000/gnet/test/protocol" | |
"sync" | |
"time" | |
) | |
// Example command: go run client.go | |
type codeClient struct { | |
*gnet.EventServer | |
wg sync.WaitGroup | |
} | |
func (cs *codeClient) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) { | |
fmt.Println("received: ", string(frame)) | |
cs.wg.Done() | |
return | |
} | |
func (cs *codeClient) OnOpened(c gnet.Conn) ([]byte, gnet.Action) { | |
item := protocol.CustomLengthFieldProtocol{Version: protocol.DefaultProtocolVersion, ActionType: protocol.ActionData} | |
c.SetContext(item) | |
return nil, gnet.None | |
} | |
func main() { | |
var port int | |
var count int | |
// Example command: go run client.go --port 9000 --count 10 | |
flag.IntVar(&port, "port", 9000, "server port") | |
flag.IntVar(&count, "count", 10, "message count") | |
flag.Parse() | |
addr := fmt.Sprintf("127.0.0.1:%d", port) | |
codec := &protocol.CustomLengthFieldProtocol{} | |
cs := &codeClient{wg: sync.WaitGroup{}} | |
client, err := gnet.NewClient( | |
cs, | |
gnet.WithCodec(codec), | |
gnet.WithTCPNoDelay(gnet.TCPDelay), | |
gnet.WithTCPKeepAlive(time.Minute*5), | |
) | |
if err != nil { | |
panic(err) | |
} | |
err = client.Start() | |
if err != nil { | |
panic(err) | |
} | |
conn, err := client.Dial("tcp", addr) | |
if err != nil { | |
panic(err) | |
} | |
defer conn.Close() | |
// store customize protocol header param using `c.SetContext()` | |
cs.wg.Add(count) | |
for i := 0; i < count; i++ { | |
err = conn.AsyncWrite([]byte("hello")) | |
if err != nil { | |
panic(err) | |
} | |
} | |
cs.wg.Wait() // wait for completing async write operation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment