8000 Attempt using sub package by gostega · Pull Request #1 · gostega/go-sse-poc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Attempt using sub package #1

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions controllers/node-controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"go-sse-poc/events"
"log"
"net/http"

Expand All @@ -16,6 +17,8 @@ func UpdateNode(c echo.Context) error {
log.Print("Fake updating a node to test SSE publishing")

// publish something here
m := events.NewMessage("hi", "topic")
_ = events.GetSseServer().Publish(m)

return c.String(http.StatusOK, "published a message, did you get it?\n")
}
90 changes: 90 additions & 0 deletions events/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package events

import (
"fmt"
"net/http"
"time"

"github.com/labstack/echo/v4"
"github.com/tmaxmax/go-sse"
)

const (
topicRandomNumbers = "numbers"
topicStateChanges = "evt_statechange"
)

func GetSseServer() *sse.Server {
return sseHandler
}

func NewMessage(message, topic string) *sse.Message {
m := &sse.Message{}
m.ID = sse.ID(topic)
m.AppendData(message)
// _ = sseHandler.Publish(m)
return m
}

func SseTest(c echo.Context) error {
// s := &sse.Server{}

// seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
// const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
go func() {

// randID := seededRand.Intn(10)
// b := make([]byte, 10)

// for range time.Tick(time.Second) {
// m := &sse.Message{}
// m.ID = sse.ID(fmt.Sprintf("thread:%d", randID))
// for i := range b {
// b[i] = charset[seededRand.Intn(len(charset))]
// }

// m.AppendData(string(b))
// _ = sseHandler.Publish(m)
// }
m := &sse.Message{}
m.ID = sse.ID(topicStateChanges)
m.AppendData("Connected")
_ = sseHandler.Publish(m)

}()

sseHandler.ServeHTTP(c.Response(), c.Request())

return nil
}

var sseHandler = &sse.Server{
Provider: &sse.Joe{
ReplayProvider: &sse.ValidReplayProvider{
TTL: time.Minute * 5,
GCInterval: time.Minute,
AutoIDs: false,
},
},
Logger: nil,
OnSession: func(s *sse.Session) (sse.Subscription, bool) {
topics := s.Req.URL.Query()["topic"]
for _, topic := range topics {
if topic != topicRandomNumbers && topic != topicStateChanges {
fmt.Fprintf(s.Res, "invalid topic %q; supported are %q, %q", topic, topicRandomNumbers, topicStateChanges)
s.Res.WriteHeader(http.StatusBadRequest)
return sse.Subscription{}, false
}
}
if len(topics) == 0 {
// Provide default topics, if none are given.
topics = []string{topicRandomNumbers, topicStateChanges}
}

return sse.Subscription{
Client: s,
LastEventID: s.LastEventID,
Topics: append(topics, sse.DefaultTopic), // the shutdown message is sent on the default topic
}, true
},
}
Binary file modified go-sse-poc
Binary file not shown.
74 changes: 4 additions & 70 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package main

import (
"fmt"
"go-sse-poc/controllers"
"go-sse-poc/events"
"log"
"net/http"
"time"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/tmaxmax/go-sse"
)

var portRaw string = "1988"
Expand All @@ -28,72 +26,8 @@ func init() {

}

const (
topicRandomNumbers = "numbers"
topicStateChanges = "evt_statechange"
)

func sseTest(c echo.Context) error {
// s := &sse.Server{}

// seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
// const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
go func() {

// randID := seededRand.Intn(10)
// b := make([]byte, 10)

// for range time.Tick(time.Second) {
// m := &sse.Message{}
// m.ID = sse.ID(fmt.Sprintf("thread:%d", randID))
// for i := range b {
// b[i] = charset[seededRand.Intn(len(charset))]
// }

// m.AppendData(string(b))
// _ = sseHandler.Publish(m)
// }
m := &sse.Message{}
m.ID = sse.ID(topicStateChanges)
m.AppendData("Connected")
_ = sseHandler.Publish(m)

}()

sseHandler.ServeHTTP(c.Response(), c.Request())

return nil
}

var sseHandler = &sse.Server{
Provider: &sse.Joe{
ReplayProvider: &sse.ValidReplayProvider{
TTL: time.Minute * 5,
GCInterval: time.Minute,
AutoIDs: false,
},
},
Logger: nil,
OnSession: func(s *sse.Session) (sse.Subscription, bool) {
topics := s.Req.URL.Query()["topic"]
for _, topic := range topics {
if topic != topicRandomNumbers && topic != topicStateChanges {
fmt.Fprintf(s.Res, "invalid topic %q; supported are %q, %q", topic, topicRandomNumbers, topicStateChanges)
s.Res.WriteHeader(http.StatusBadRequest)
return sse.Subscription{}, false
}
}
if len(topics) == 0 {
// Provide default topics, if none are given.
topics = []string{topicRandomNumbers, topicStateChanges}
}

return sse.Subscription{
Client: s,
LastEventID: s.LastEventID,
Topics: append(topics, sse.DefaultTopic), // the shutdown message is sent on the default topic
}, true
},
func GetServer() string {
return "hi"
}

func main() {
Expand All @@ -106,7 +40,7 @@ func main() {
// e.Use(middleware.Recover()) // only works with panics, not 'fatal'
// e.Renderer = NewTemplates()

e.GET("/events", sseTest)
e.GET("/events", events.SseTest)

e.POST("/api/nodes", controllers.UpdateNode)

Expand Down
0