HTTP streaming comet client

Hello,

I’m trying to implement a client for comet protocol.
In “streaming” mode the server returns all results on single http channel using chunked transfert.

Cache-Control: no-cache
Connection: keep-alive
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: application/json

With the following code, I can receive the first response from the commands that creates the server, but nothing else. The other following messages are lost.

	p := make([]byte, 50)
	rd := bufio.NewReader(body)
	for {
		l, err := rd.Read(p)
		if err != nil {
			log.Fatal("Read Error:", err)
			return
		}
		fmt.Println(l, string(p))
	}

And after 1 minute the error message:

2019/09/24 17:37:13 Read Error:unexpected EOF
exit status 1

Looks like the channel is closed after the first response.
Any idea?

2 Likes

Hi,
I am not familiar with Comet, but it looks like you want to implement Server-Sent Events. Most modern browsers support SSE streams and there are libraries for it.

2 Likes

Thanks for the suggestion. Yes I believe the principle is similar.

I’ve tried to grab the reader code from https://github.com/r3labs/sse, and the result is the same: “Read Error:unexpected EOF” Whatever reader or method I use, I received the same error when the 2nd message is sent. So I believe my issue is in lower level.

For reference, here is the doc about comet/bayeux protocol: https://docs.cometd.org/current/reference/#_concepts_bayeux_protocol

2 Likes

Unfortunately I’m getting nowhere:
I have simple loop to grab the messages

rsp, err := c.http.Do(req)
if err != nil {
	return err
}

defer rsp.Body.Close()
dec := json.NewDecoder(rsp.Body)

for {
	fmt.Println("Loop")
	var messages []message
	err = dec.Decode(&messages)
	if err != nil {
		if err == io.EOF {
			fmt.Println("EOF")
		}
		return err
	}
    fmt.Println("Handshake")
}

but after the first message I get EOF, probably because the first message is complete.

Loop
Handshake
Loop
EOF

How can I reset the reader so it waits for another message, on the same connection (without new request)

3 Likes

hi

I used the following template and it is fully functional…

you must consider max event channel number is 6!

regards

2 Likes

Thanks Matthias but I don’t have control over the server.
I’m trying to connect my client to it using the bayeux protocol.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.