socketio example: two tcp clients

This commit is contained in:
Li Jie
2024-09-08 20:29:24 +08:00
parent 566d5ef96f
commit ccc7d056ba

View File

@@ -173,7 +173,11 @@ func RunSocket() {
println("RunClient") println("RunClient")
timeout.Timeout(100 * time.Millisecond).Then(func(async.Void) { timeout.Timeout(100 * time.Millisecond).Then(func(async.Void) {
RunClient().Then(func(async.Void) { RunClient("Bob").Then(func(async.Void) {
println("RunClient done")
resolve(async.Void{})
})
RunClient("Uncle").Then(func(async.Void) {
println("RunClient done") println("RunClient done")
resolve(async.Void{}) resolve(async.Void{})
}) })
@@ -181,7 +185,7 @@ func RunSocket() {
})) }))
} }
func RunClient() async.Future[async.Void] { func RunClient(name string) async.Future[async.Void] {
return async.Async(func(resolve func(async.Void)) { return async.Async(func(resolve func(async.Void)) {
addr := "127.0.0.1:3927" addr := "127.0.0.1:3927"
socketio.Connect("tcp", addr).Then(func(v tuple.Tuple2[*socketio.Conn, error]) { socketio.Connect("tcp", addr).Then(func(v tuple.Tuple2[*socketio.Conn, error]) {
@@ -194,7 +198,7 @@ func RunClient() async.Future[async.Void] {
var loop func(client *socketio.Conn) var loop func(client *socketio.Conn)
loop = func(client *socketio.Conn) { loop = func(client *socketio.Conn) {
counter++ counter++
data := fmt.Sprintf("Hello %d", counter) data := fmt.Sprintf("Hello from %s %d", name, counter)
client.Write([]byte(data)).Then(func(err error) { client.Write([]byte(data)).Then(func(err error) {
if err != nil { if err != nil {
panic(err) panic(err)