snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
108
internal/unisse/transport.go
Normal file
108
internal/unisse/transport.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package unisse
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/centrifugal/centrifugo/v6/internal/tools"
|
||||
|
||||
"github.com/centrifugal/centrifuge"
|
||||
)
|
||||
|
||||
type eventsourceTransport struct {
|
||||
mu sync.Mutex
|
||||
req *http.Request
|
||||
ack chan struct{}
|
||||
messages chan [][]byte
|
||||
disconnectCh chan *centrifuge.Disconnect
|
||||
closedCh chan struct{}
|
||||
closed bool
|
||||
pingPongConfig centrifuge.PingPongConfig
|
||||
protoMajor int
|
||||
}
|
||||
|
||||
func newEventsourceTransport(req *http.Request, pingPongConfig centrifuge.PingPongConfig, ack chan struct{}) *eventsourceTransport {
|
||||
return &eventsourceTransport{
|
||||
messages: make(chan [][]byte),
|
||||
disconnectCh: make(chan *centrifuge.Disconnect),
|
||||
closedCh: make(chan struct{}),
|
||||
req: req,
|
||||
pingPongConfig: pingPongConfig,
|
||||
protoMajor: req.ProtoMajor,
|
||||
ack: ack,
|
||||
}
|
||||
}
|
||||
|
||||
const transportName = "uni_sse"
|
||||
|
||||
func (t *eventsourceTransport) Name() string {
|
||||
return transportName
|
||||
}
|
||||
|
||||
func (t *eventsourceTransport) Protocol() centrifuge.ProtocolType {
|
||||
return centrifuge.ProtocolTypeJSON
|
||||
}
|
||||
|
||||
// ProtocolVersion returns transport protocol version.
|
||||
func (t *eventsourceTransport) ProtocolVersion() centrifuge.ProtocolVersion {
|
||||
return centrifuge.ProtocolVersion2
|
||||
}
|
||||
|
||||
// Unidirectional returns whether transport is unidirectional.
|
||||
func (t *eventsourceTransport) Unidirectional() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// DisabledPushFlags ...
|
||||
func (t *eventsourceTransport) DisabledPushFlags() uint64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// PingPongConfig ...
|
||||
func (t *eventsourceTransport) PingPongConfig() centrifuge.PingPongConfig {
|
||||
return t.pingPongConfig
|
||||
}
|
||||
|
||||
// Emulation ...
|
||||
func (t *eventsourceTransport) Emulation() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AcceptProtocol ...
|
||||
func (t *eventsourceTransport) AcceptProtocol() string {
|
||||
return tools.GetAcceptProtocolLabel(t.protoMajor)
|
||||
}
|
||||
|
||||
func (t *eventsourceTransport) Write(message []byte) error {
|
||||
return t.WriteMany(message)
|
||||
}
|
||||
|
||||
func (t *eventsourceTransport) WriteMany(messages ...[]byte) error {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if t.closed {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case t.messages <- messages:
|
||||
case <-t.closedCh:
|
||||
}
|
||||
select {
|
||||
case <-t.ack:
|
||||
case <-t.closedCh:
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *eventsourceTransport) Close(_ centrifuge.Disconnect) error {
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
if t.closed {
|
||||
return nil
|
||||
}
|
||||
t.closed = true
|
||||
close(t.disconnectCh)
|
||||
<-t.closedCh
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user