mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-21 14:44:45 +01:00
add keepalive messages
This commit is contained in:
@@ -2,6 +2,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/getAlby/lndhub.go/common"
|
||||
"github.com/getAlby/lndhub.go/db/models"
|
||||
@@ -16,6 +17,11 @@ type InvoiceStreamController struct {
|
||||
svc *service.LndhubService
|
||||
}
|
||||
|
||||
type InvoiceEventWrapper struct {
|
||||
Type string `json:"type"`
|
||||
Invoice *IncomingInvoice `json:"invoice, omitempty"`
|
||||
}
|
||||
|
||||
func NewInvoiceStreamController(svc *service.LndhubService) *InvoiceStreamController {
|
||||
return &InvoiceStreamController{svc: svc}
|
||||
}
|
||||
@@ -31,6 +37,7 @@ func (controller *InvoiceStreamController) StreamInvoices(c echo.Context) error
|
||||
ctx := c.Request().Context()
|
||||
upgrader := websocket.Upgrader{}
|
||||
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -41,9 +48,17 @@ SocketLoop:
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
break SocketLoop
|
||||
case <-ticker.C:
|
||||
err := ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
||||
if err != nil {
|
||||
controller.svc.Logger.Error(err)
|
||||
break SocketLoop
|
||||
}
|
||||
case invoice := <-invoiceChan:
|
||||
err := ws.WriteJSON(
|
||||
&IncomingInvoice{
|
||||
&InvoiceEventWrapper{
|
||||
Type: "invoice",
|
||||
Invoice: &IncomingInvoice{
|
||||
PaymentHash: invoice.RHash,
|
||||
PaymentRequest: invoice.PaymentRequest,
|
||||
Description: invoice.Memo,
|
||||
@@ -52,7 +67,7 @@ SocketLoop:
|
||||
Type: common.InvoiceTypeUser,
|
||||
Amount: invoice.Amount,
|
||||
IsPaid: invoice.State == common.InvoiceStateSettled,
|
||||
})
|
||||
}})
|
||||
if err != nil {
|
||||
controller.svc.Logger.Error(err)
|
||||
break SocketLoop
|
||||
|
||||
Reference in New Issue
Block a user