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.7k stars 1.04k forks source link

[Question]: gnet client works not as expected even tho i did it according to the api ref #591

Closed Z3NTL3 closed 7 months ago

Z3NTL3 commented 7 months ago

Connecting with a web server, however when the connection is opened i write http request data payload, send it. But get no response back..

What is wrong?

package main

import (
    "fmt"
    "log"
    "net"

    "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")
    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(err)
    return gnet.None
}

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:80")
    if err != nil {
        log.Fatal(err)
    }

}
panjf2000 commented 7 months ago

Too few information, use bug report template to create a new issue.