Skip to content
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

How to integerate iris.FromStd with mvc.Application #2355

Closed
Macfee opened this issue Jan 22, 2024 · 2 comments
Closed

How to integerate iris.FromStd with mvc.Application #2355

Macfee opened this issue Jan 22, 2024 · 2 comments
Assignees

Comments

@Macfee
Copy link

Macfee commented Jan 22, 2024

I want to use the "github.com/alexandrevicenzi/go-sse" package to implement SSE functionality. And I want to integrate this with mvc.application. I'm not very familiar with iris.fromstd at the moment. Don't know what to do to make them run properly, please give me a little help. Thanks。

package main

import (
	"log"
	"net/http"
	"os"
	"time"

	sse "github.com/alexandrevicenzi/go-sse"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// Install the sse third-party package.
// $ go get -u github.com/alexandrevicenzi/go-sse
//
// Documentation: https://pkg.go.dev/github.com/alexandrevicenzi/go-sse
func main() {
	app := iris.New()

	s := sse.NewServer(&sse.Options{
		// Increase default retry interval to 10s.
		RetryInterval: 10 * 1000,
		// CORS headers
		Headers: map[string]string{
			"Access-Control-Allow-Origin":  "*",
			"Access-Control-Allow-Methods": "GET, OPTIONS",
			"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
		},
		// Custom channel name generator
		ChannelNameFunc: func(request *http.Request) string {
			return request.URL.Path
		},
		// Print debug info
		Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
	})

	defer s.Shutdown()

	app.Get("/", func(ctx iris.Context) {
		ctx.ServeFile("./index.html")
	})
	Init(app)
	app.Listen(":4000")
}

func Init(app *iris.Application) {
	mvc.Configure(app.Party("/"), func(m *mvc.Application) {
		// m.Router.Use(NewBroker().ServeHTTP)
		m.Handle(NewSSEController())
	})
}

type SSEController struct {
	Ctx iris.Context
}

func NewSSEController() *SSEController {
	return &SSEController{}
}

func (t *SSEController) BeforeActivation(b mvc.BeforeActivation) {

	b.Handle("GET", "/event/", "GetChannel")
}

func (t *SSEController) GetChannel() {
	go func() {
		for {
			// s.SendMessage("/channel", sse.SimpleMessage(time.Now().Format("2006/02/01/ 15:04:05")))
			time.Sleep(5 * time.Second)
		}
	}()

}

@kataras
Copy link
Owner

kataras commented Jan 22, 2024

Hello @Macfee,

The 3rd-party library you use for SSE, provides an http.Handler which works as an event broker on by itself (clients are registered to a channel group in order to receive server-sent messages), so the only thing you have to do is to send any events using its own "SendMessage" method in a different go routine. There is no reason to register that SSE Handler inside a controller: app.Get("/events/{channel}", iris.FromStd(s)), the iris.FromStd converts any http.Handler/http.HandlerFunc or http.Handler with next http.Handler to an iris.Handler. See https://github.com/kataras/iris/blob/main/_examples/response-writer/sse-third-party-2/main.go

@Macfee
Copy link
Author

Macfee commented Jan 23, 2024

@kataras thanks for your reply. how to close the connection properly, if I write based on the example of iris-contrib/examples/response-write/sse. I‘m using mvc router.

@Macfee Macfee closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants