Giter Site home page Giter Site logo

sse client about resty HOT 6 OPEN

chrisfeng0723 avatar chrisfeng0723 commented on June 13, 2024
sse client

from resty.

Comments (6)

jeevatkm avatar jeevatkm commented on June 13, 2024 3

@chrisfeng0723 In resty response, body read fully at once. It is not a stream. Maybe you could try https://pkg.go.dev/github.com/go-resty/resty/v2#Request.SetDoNotParseResponse and handle the body yourself. However, it will not be an actual SSE experience.

I may think of adding an SSE client in the v3.

from resty.

CermakM avatar CermakM commented on June 13, 2024 1

SSE client would be very appreciated! 🙏

from resty.

xuji-cny avatar xuji-cny commented on June 13, 2024

I have the same doubt

from resty.

xiaziheng98 avatar xiaziheng98 commented on June 13, 2024

I have the same doubt too

from resty.

gospider007 avatar gospider007 commented on June 13, 2024

this can help you : https://github.com/gospider007/requests/blob/master/test/protocol/sse_test.go

from resty.

sosyz avatar sosyz commented on June 13, 2024

maybe this code can help you

func TestSSE(t *testing.T) {
	// Start the server
	go func() {
		err := http.ListenAndServe(":5000", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "text/event-stream")
			w.Header().Set("Cache-Control", "no-cache")
			w.Header().Set("Connection", "keep-alive")
			for i := 0; i < 3; i++ {
				_, err := w.Write([]byte(fmt.Sprintf("%d\n", i)))
				if err != nil {
					t.Log("Error writing SSE event:", err)
					return
				}

				if f, ok := w.(http.Flusher); ok {
					f.Flush()
				}
				time.Sleep(1 * time.Second)
			}
		}))
		if err != nil {
			t.Error(err)
		}
	}()
	time.Sleep(time.Second * 3)

	resp, err := resty.New().
		R().
		SetDoNotParseResponse(true).
		Get("http://localhost:5000")
	if err != nil {
		t.Error(err)
	}
	defer resp.RawResponse.Body.Close()

	scanner := bufio.NewScanner(resp.RawResponse.Body)
	reply := ""
	for scanner.Scan() {
		_res := scanner.Text()
		if _res == "" {
			continue
		}
		reply += _res
		t.Log(_res)
	}

	t.Log(reply)
}

from resty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.