This commit is contained in:
Adrien Bouvais 2024-05-16 20:10:57 +02:00
parent 920c34c25c
commit 19ed54fbf3
3 changed files with 9 additions and 9 deletions

View File

@ -170,17 +170,17 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
outIcon := `<img src="` + selectedLLMs[idx].Model.Company.Icon + `" alt="User Image">` outIcon := `<img src="` + selectedLLMs[idx].Model.Company.Icon + `" alt="User Image">`
sseChanel.SendEvent( go sseChanel.SendEvent(
"swapContent-"+fmt.Sprintf("%d", message.Area.Position), "swapContent-"+fmt.Sprintf("%d", message.Area.Position),
outContent, outContent,
) )
sseChanel.SendEvent( go sseChanel.SendEvent(
"swapSelectionBtn-"+selectedLLMs[idx].ID.String(), "swapSelectionBtn-"+selectedLLMs[idx].ID.String(),
outBtn, outBtn,
) )
sseChanel.SendEvent( go sseChanel.SendEvent(
"swapIcon-"+fmt.Sprintf("%d", message.Area.Position), "swapIcon-"+fmt.Sprintf("%d", message.Area.Position),
outIcon, outIcon,
) )

View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"github.com/MrBounty/JADE2.0/ssefiber"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
@ -13,7 +12,7 @@ import (
var userTmpl *pongo2.Template var userTmpl *pongo2.Template
var botTmpl *pongo2.Template var botTmpl *pongo2.Template
var selectBtnTmpl *pongo2.Template var selectBtnTmpl *pongo2.Template
var sseChanel *ssefiber.FiberSSEChannel var sseChanel *FiberSSEChannel
func main() { func main() {
botTmpl = pongo2.Must(pongo2.FromFile("views/partials/message-bot.html")) botTmpl = pongo2.Must(pongo2.FromFile("views/partials/message-bot.html"))
@ -29,7 +28,7 @@ func main() {
AppName: "JADE 2.0", AppName: "JADE 2.0",
EnablePrintRoutes: true, EnablePrintRoutes: true,
}) })
sse := ssefiber.New(app, "/sse") sse := NewSSE(app, "/sse")
sseChanel = sse.CreateChannel("sse", "") sseChanel = sse.CreateChannel("sse", "")
// Add default logger // Add default logger

View File

@ -1,4 +1,4 @@
package ssefiber package main
import ( import (
"bufio" "bufio"
@ -84,7 +84,7 @@ Each channel has a name, a base route, and a channel for sending events.
// Events Channel // Events Channel
eventsChan := testChan.Events eventsChan := testChan.Events
*/ */
func New(app *fiber.App, base string) *FiberSSEApp { func NewSSE(app *fiber.App, base string) *FiberSSEApp {
// Add the base route // Add the base route
fiberRouter := app.Group(base, func(c *fiber.Ctx) error { fiberRouter := app.Group(base, func(c *fiber.Ctx) error {
// Set the headers for SSE // Set the headers for SSE
@ -178,7 +178,7 @@ func (fChan *FiberSSEChannel) ServeHTTP(c *fiber.Ctx) error {
for { for {
event, more := <-fChan.Events event, more := <-fChan.Events
// fmt.Fprintf(w, "event: %s\ndata: %s\n\n", string(event.Event), string(event.Data)) fmt.Println("event: ", string(event.Event), "\ndata: \n\n", string(event.Data))
// w.Flush() // w.Flush()
go event.FireEventHandlers(c) go event.FireEventHandlers(c)
if err := event.Flush(w); err != nil { if err := event.Flush(w); err != nil {
@ -216,6 +216,7 @@ func (channel *FiberSSEChannel) FireHandlers(fiberCtx *fiber.Ctx, event string)
func (e *FiberSSEEvent) FireEventHandlers(fiberCtx *fiber.Ctx) { func (e *FiberSSEEvent) FireEventHandlers(fiberCtx *fiber.Ctx) {
channel := e.OnChannel channel := e.OnChannel
for _, handler := range channel.EventHandlers[e.Event] { for _, handler := range channel.EventHandlers[e.Event] {
fmt.Println("Firing Event Handlers on Channel: " + channel.Name + " - Event: " + e.Event)
handler(fiberCtx, channel, e) handler(fiberCtx, channel, e)
} }
} }