Close old SSE connections, push down type strings
This commit is contained in:
parent
e8d865d0ab
commit
4925fe4857
4 changed files with 42 additions and 4 deletions
14
api/event.go
14
api/event.go
|
|
@ -20,8 +20,20 @@ type ConnectionSSE struct {
|
|||
userID int
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Resource string `json:"resource"`
|
||||
Time time.Time `json:"time"`
|
||||
Type string `json:"type"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
func (c *ConnectionSSE) SendEvent(w http.ResponseWriter, m platform.Event) error {
|
||||
return send(w, m)
|
||||
return send(w, Message{
|
||||
Resource: m.Resource,
|
||||
Time: m.Time,
|
||||
Type: m.Type.String(),
|
||||
URI: m.URI,
|
||||
})
|
||||
}
|
||||
func (c *ConnectionSSE) SendHeartbeat(w http.ResponseWriter, t time.Time) error {
|
||||
return send(w, platform.Event{
|
||||
|
|
|
|||
|
|
@ -59,6 +59,11 @@ window.SSEManager = (function () {
|
|||
console.error("SSE error:", err);
|
||||
isConnected = false;
|
||||
|
||||
// Close old connection
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
}
|
||||
|
||||
// Reconnect after delay
|
||||
setTimeout(() => {
|
||||
connectionPromise = null;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,11 @@
|
|||
{{ block "extraheader" . }}{{ end }}
|
||||
<script>
|
||||
SSEManager.subscribe("*", function (e) {
|
||||
console.log("event", e);
|
||||
if (e.type == "created") {
|
||||
console.log("created event", e);
|
||||
} else {
|
||||
console.log("other event", e);
|
||||
}
|
||||
});
|
||||
function restoreLocalStorage() {
|
||||
const expanded = localStorage.getItem("sidebar.expanded");
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
package event
|
||||
|
||||
import (
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
)
|
||||
|
||||
var chanEvents chan<- Envelope
|
||||
|
|
@ -27,11 +28,27 @@ type EventType int
|
|||
const (
|
||||
EventTypeCreated EventType = iota
|
||||
EventTypeDeleted
|
||||
EventTypeModified
|
||||
EventTypeHeartbeat
|
||||
EventTypeSudo
|
||||
EventTypeUpdated
|
||||
)
|
||||
|
||||
func (et EventType) String() string {
|
||||
switch et {
|
||||
case EventTypeCreated:
|
||||
return "created"
|
||||
case EventTypeDeleted:
|
||||
return "deleted"
|
||||
case EventTypeHeartbeat:
|
||||
return "heartbeat"
|
||||
case EventTypeSudo:
|
||||
return "sudo"
|
||||
case EventTypeUpdated:
|
||||
return "updated"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
type ResourceType int
|
||||
|
||||
const (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue