Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2ed58bb0
authored
Apr 25, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: improve the implementation of sse
parent
796cb86b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
13 deletions
+14
-13
controller/relay.go
+14
-13
No files found.
controller/relay.go
View file @
2ed58bb0
...
@@ -2,7 +2,6 @@ package controller
...
@@ -2,7 +2,6 @@ package controller
import
(
import
(
"bufio"
"bufio"
"bytes"
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"io"
"io"
...
@@ -76,23 +75,25 @@ func Relay(c *gin.Context) {
...
@@ -76,23 +75,25 @@ func Relay(c *gin.Context) {
for
k
,
v
:=
range
resp
.
Header
{
for
k
,
v
:=
range
resp
.
Header
{
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
c
.
Writer
.
Header
()
.
Set
(
k
,
v
[
0
])
}
}
c
.
Writer
.
Header
()
.
Set
(
"Content-Type"
,
"text/event-stream"
)
c
.
Writer
.
Header
()
.
Set
(
"Cache-Control"
,
"no-cache"
)
c
.
Writer
.
Header
()
.
Set
(
"Connection"
,
"keep-alive"
)
c
.
Writer
.
WriteHeaderNow
()
//w := c.Writer
//flusher, _ := w.(http.Flusher)
c
.
Stream
(
func
(
w
io
.
Writer
)
bool
{
c
.
Stream
(
func
(
w
io
.
Writer
)
bool
{
select
{
select
{
case
data
:=
<-
dataChan
:
case
data
:=
<-
dataChan
:
//fmt.Println(data)
suffix
:=
""
//c.Data(http.StatusOK, "text/event-stream", []byte(data))
//c.Render(-1, common.Event{Data: data})
//c.SSEvent("", data)
//w.Write([]byte(data))
//w.(http.Flusher).Flush()
//c.Writer.Write(append([]byte(data), []byte("\n\n")...))
outputBytes
:=
bytes
.
NewBufferString
(
data
)
w
.
Write
(
outputBytes
.
Bytes
())
if
strings
.
HasPrefix
(
data
,
"data: "
)
{
if
strings
.
HasPrefix
(
data
,
"data: "
)
{
w
.
Write
([]
byte
(
"
\n\n
"
))
suffix
=
"
\n\n
"
}
_
,
err
:=
fmt
.
Fprintf
(
w
,
"%s%s"
,
data
,
suffix
)
if
err
!=
nil
{
return
false
}
}
//w.Write(append(outputBytes.Bytes(), []byte("\n\n")...)
)
flusher
,
_
:=
w
.
(
http
.
Flusher
)
w
.
(
http
.
Flusher
)
.
Flush
()
flusher
.
Flush
()
//fmt.Println(data)
//fmt.Println(data)
return
true
return
true
case
<-
stopChan
:
case
<-
stopChan
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment