-
Notifications
You must be signed in to change notification settings - Fork 487
Optimize and standardize the readSSEStream function handling #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wangchaodeyuzhou
wants to merge
10
commits into
mark3labs:main
Choose a base branch
from
wangchaodeyuzhou:fix-readSSE
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
36d27b9
optimize capability and notification (#184)
dugenkui03 4413691
fix: correct JSON key for client capabilities (#197)
TBXark af8def7
fix(client): optimize and standardize the readSSEStream function hand…
9751e03
fix(client): unexport sseEvent struct
8786de7
optimize capability and notification (#184)
dugenkui03 1134c1e
fix(client): capabilities json tag
250317c
fix(client): bufferReader replace scanner
0ecbcae
fix(client): optimize and standardize the readSSEStream function hand…
43c273b
fix(client): rebase handle conflict
cdf8efd
fix(client): handle param evt -> event
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
.idea | ||
.opencode | ||
.claude | ||
|
||
.vscode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package transport | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type sseEvent struct { | ||
event string | ||
data string | ||
} | ||
|
||
// ReadSSEStream continuously reads the SSE stream and processes events. | ||
func ReadSSEStream(ctx context.Context, reader io.ReadCloser, onEvent func(event sseEvent)) error { | ||
defer func(reader io.ReadCloser) { | ||
err := reader.Close() | ||
if err != nil { | ||
fmt.Printf("Error closing reader: %v\n", err) | ||
} | ||
}(reader) | ||
|
||
br := bufio.NewReader(reader) | ||
var event, data strings.Builder | ||
|
||
processEvent := func() { | ||
if event.Len() > 0 || data.Len() > 0 { | ||
onEvent(sseEvent{event: event.String(), data: data.String()}) | ||
event.Reset() | ||
data.Reset() | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
default: | ||
line, err := br.ReadString('\n') | ||
if err != nil { | ||
if err == io.EOF { | ||
// Handle last event when EOF | ||
processEvent() | ||
return nil | ||
} | ||
return fmt.Errorf("error reading SSE stream: %w", err) | ||
} | ||
|
||
// Remove only newline markers | ||
line = strings.TrimRight(line, "\r\n") | ||
|
||
switch { | ||
case strings.HasPrefix(line, "event:"): | ||
event.Reset() | ||
event.WriteString(strings.TrimSpace(strings.TrimPrefix(line, "event:"))) | ||
case strings.HasPrefix(line, "data:"): | ||
if data.Len() > 0 { | ||
data.WriteString("\n") | ||
} | ||
data.WriteString(strings.TrimSpace(strings.TrimPrefix(line, "data:"))) | ||
case line == "": | ||
processEvent() | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.