Skip to content

Commit

Permalink
feat(mimetype/sse): 添加 ServerEvent.Server 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 19, 2024
1 parent e628fea commit 7ad4800
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions mimetype/sse/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ func NewServer[T comparable](s web.Server, retry, keepAlive time.Duration, bufCa
}

func (srv *Server[T]) keepAlive(now time.Time) error {
srv.sources.Range(func(_ T, v *Source) bool {
for _, v := range srv.Sources() {
if v.last.After(now) {
b := bufpool.New()
b.WriteString(":\n\n")
v.buf <- b
}
return true
})
}
return nil
}

Expand Down Expand Up @@ -220,6 +219,9 @@ func (e *ServerEvent[T]) Sent(f func(sid T, lastEventID string) any) {
}
}

// Server 关联的 [Server] 对象
func (e *ServerEvent[T]) Server() *Server[T] { return e.server }

// LastEventID 客户端提交的报头 Last-Event-ID 值
//
// 当此值不为空时表示该链接刚从客户端重新连接上。
Expand Down
6 changes: 3 additions & 3 deletions mimetype/sse/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func TestServer(t *testing.T) {

a.Equal(0, e.Len())

s, wait := e.NewSource(id, ctx)
s.Sent([]string{"connect", strconv.FormatInt(id, 10)}, "", "1")
src, wait := e.NewSource(id, ctx)
src.Sent([]string{"connect", strconv.FormatInt(id, 10)}, "", "1")
time.Sleep(time.Microsecond * 500)

event := s.NewEvent("event", sj.Marshal)
event := src.NewEvent("event", sj.Marshal)
a.NotError(event.Sent(1))
time.Sleep(time.Microsecond * 500)
a.NotError(event.Sent(&struct{ ID int }{ID: 5}))
Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.100.5"
const Version = "0.100.6"

type (
Logger = logs.Logger
Expand Down

0 comments on commit 7ad4800

Please sign in to comment.