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 (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/getAlby/lndhub.go/common"
|
"github.com/getAlby/lndhub.go/common"
|
||||||
"github.com/getAlby/lndhub.go/db/models"
|
"github.com/getAlby/lndhub.go/db/models"
|
||||||
@@ -16,6 +17,11 @@ type InvoiceStreamController struct {
|
|||||||
svc *service.LndhubService
|
svc *service.LndhubService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type InvoiceEventWrapper struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Invoice *IncomingInvoice `json:"invoice, omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewInvoiceStreamController(svc *service.LndhubService) *InvoiceStreamController {
|
func NewInvoiceStreamController(svc *service.LndhubService) *InvoiceStreamController {
|
||||||
return &InvoiceStreamController{svc: svc}
|
return &InvoiceStreamController{svc: svc}
|
||||||
}
|
}
|
||||||
@@ -31,6 +37,7 @@ func (controller *InvoiceStreamController) StreamInvoices(c echo.Context) error
|
|||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
upgrader := websocket.Upgrader{}
|
upgrader := websocket.Upgrader{}
|
||||||
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||||
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -41,18 +48,26 @@ SocketLoop:
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
break SocketLoop
|
break SocketLoop
|
||||||
|
case <-ticker.C:
|
||||||
|
err := ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
||||||
|
if err != nil {
|
||||||
|
controller.svc.Logger.Error(err)
|
||||||
|
break SocketLoop
|
||||||
|
}
|
||||||
case invoice := <-invoiceChan:
|
case invoice := <-invoiceChan:
|
||||||
err := ws.WriteJSON(
|
err := ws.WriteJSON(
|
||||||
&IncomingInvoice{
|
&InvoiceEventWrapper{
|
||||||
PaymentHash: invoice.RHash,
|
Type: "invoice",
|
||||||
PaymentRequest: invoice.PaymentRequest,
|
Invoice: &IncomingInvoice{
|
||||||
Description: invoice.Memo,
|
PaymentHash: invoice.RHash,
|
||||||
PayReq: invoice.PaymentRequest,
|
PaymentRequest: invoice.PaymentRequest,
|
||||||
Timestamp: invoice.CreatedAt.Unix(),
|
Description: invoice.Memo,
|
||||||
Type: common.InvoiceTypeUser,
|
PayReq: invoice.PaymentRequest,
|
||||||
Amount: invoice.Amount,
|
Timestamp: invoice.CreatedAt.Unix(),
|
||||||
IsPaid: invoice.State == common.InvoiceStateSettled,
|
Type: common.InvoiceTypeUser,
|
||||||
})
|
Amount: invoice.Amount,
|
||||||
|
IsPaid: invoice.State == common.InvoiceStateSettled,
|
||||||
|
}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
controller.svc.Logger.Error(err)
|
controller.svc.Logger.Error(err)
|
||||||
break SocketLoop
|
break SocketLoop
|
||||||
|
|||||||
Reference in New Issue
Block a user