Files
next.orly.dev/pkg/protocol/publish/publisher.go
mleku ed412dcb7e
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled
Add WebSocket workaround test and enhance connection handling
- Introduced a new test file `workaround_test.go` to validate the behavior of a "dumb" WebSocket client that does not handle ping/pong messages correctly, ensuring the connection remains alive through server-side workarounds.
- Updated the `handle-websocket.go` file to improve message size handling and refactor ping/pong logic, allowing for direct message sending and better error management.
- Enhanced the `listener.go` file to support a more robust write channel mechanism, allowing pings to interrupt writes and improving overall connection management.
- Bumped version to v0.23.4 to reflect these changes.
2025-11-03 13:49:14 +00:00

69 lines
1.4 KiB
Go

package publish
import (
"time"
"github.com/gorilla/websocket"
"next.orly.dev/pkg/encoders/event"
"next.orly.dev/pkg/interfaces/publisher"
"next.orly.dev/pkg/interfaces/typer"
)
// WriteRequest represents a write operation to be performed by the write worker
type WriteRequest struct {
Data []byte
MsgType int
IsControl bool
Deadline time.Time
IsPing bool // Special marker for ping messages
}
// WriteChanSetter defines the interface for setting write channels
type WriteChanSetter interface {
SetWriteChan(*websocket.Conn, chan<- WriteRequest)
GetWriteChan(*websocket.Conn) (chan<- WriteRequest, bool)
}
// S is the control structure for the subscription management scheme.
type S struct {
publisher.Publishers
}
// New creates a new publish.S.
func New(p ...publisher.I) (s *S) {
s = &S{Publishers: p}
return
}
var _ publisher.I = &S{}
func (s *S) Type() string { return "publish" }
func (s *S) Deliver(ev *event.E) {
for _, p := range s.Publishers {
p.Deliver(ev)
}
}
func (s *S) Receive(msg typer.T) {
t := msg.Type()
for _, p := range s.Publishers {
if p.Type() == t {
p.Receive(msg)
return
}
}
}
// GetSocketPublisher returns the socketapi publisher instance
func (s *S) GetSocketPublisher() WriteChanSetter {
for _, p := range s.Publishers {
if p.Type() == "socketapi" {
if socketPub, ok := p.(WriteChanSetter); ok {
return socketPub
}
}
}
return nil
}