panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.
https://gnet.host
Apache License 2.0
9.69k stars 1.04k forks source link

[Bug]: GNET client not working as expected #592

Closed Z3NTL3 closed 7 months ago

Z3NTL3 commented 7 months ago

Actions I've taken before I'm here

What happened?

Tried using GNET as a HTTP client, I send request payload but were unable to receive response back from the web server.

Major version of gnet

v2

Specific version of gnet

v2.5.0

Operating system

Windows

OS version

Windows 11 Home 10.0.22631 x64-based PC

Go version

go version go1.22.2 windows/amd64

Relevant log output

~\Documents\Lightning via 🐹 v1.22.2 
❯ go run .
boot
onopen
GET / HTTP/1.1
Host: pix4.dev

~\Documents\Lightning via 🐹 v1.22.2 took 9s

Code snippets (optional)

Standard HTTP client to test stuff, however even tho I send the HTTP request but I do get absolute no response back, ontraffic is not triggered. But I did send payload to remote socket at OnOpen event.

package main

import (
    "fmt"
    "log"

    "github.com/panjf2000/gnet/v2"
)

type EventHandler struct {
    gnet.BuiltinEventEngine
    eng gnet.Engine
}

func (es *EventHandler) OnBoot(eng gnet.Engine) gnet.Action {
    fmt.Println("boot")
    es.eng = eng
    return gnet.None
}

func (e *EventHandler) OnOpen(c gnet.Conn) (out []byte, action gnet.Action) {
    fmt.Println("onopen")
    out = []byte("GET / HTTP/1.1\r\nHost: pix4.dev\r\n\r\n")
    fmt.Println(string(out))
    return out, gnet.None
}

func (e *EventHandler) OnTraffic(c gnet.Conn) (action gnet.Action) {
    fmt.Println("traffic")
    rd, err := c.Next(-1)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(rd), 1)
    return gnet.None
}

func (e *EventHandler) OnClose(c gnet.Conn, err error) (action gnet.Action) {
    fmt.Println("close")
    fmt.Println(err)
    return gnet.Close
}

func main() {
    client, err := gnet.NewClient(&EventHandler{}, gnet.WithMulticore(true))
    if err != nil {
        log.Fatal(err)
    }

    if err := client.Start(); err != nil {
        log.Fatal(err)
    }

    _, err = client.Dial("tcp", "104.21.25.193:443")
    if err != nil {
        log.Fatal(err)
    }
}

How to Reproduce

Reproduce what I've described above.

Does this issue reproduce with the latest release?

It can reproduce with the latest release

panjf2000 commented 7 months ago

I think your program exited before it received the response, you need to hang in the main func.

Z3NTL3 commented 7 months ago

Oh damn, true lmao. my bad i forgot totally about its even driven architecture anyways thanks!

Got one more question if u do not mind. Currently it is not possible for me to convert the connection to accommodate TLS. For example when OnOpen emits, I convert to TLS then later everytime OnTraffic fires its a TLS connection.

Will be pretty dope and I like ``gnet``` pretty much so far. Good job 🫡🫡🫡

panjf2000 commented 7 months ago

gnet hasn't supported TLS yet, but it's on our roadmap, I hope we can get it done someday.

panjf2000 commented 7 months ago

Please close this issue if your problem has been solved.