=sse update event
This commit is contained in:
parent
2464596698
commit
066b72385e
12 changed files with 56 additions and 36 deletions
BIN
sse/be/be
BIN
sse/be/be
Binary file not shown.
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -136,6 +137,9 @@ func (e *Event) SendMessage(c *gin.Context) {
|
|||
Event: "message",
|
||||
Data: "Ciao",
|
||||
})
|
||||
e.Send(Message{
|
||||
Data: "Senza event",
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Event) SendMsg(c *gin.Context) {
|
||||
|
@ -143,6 +147,7 @@ func (e *Event) SendMsg(c *gin.Context) {
|
|||
Event: "msg",
|
||||
Data: "hello",
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "msg"})
|
||||
}
|
||||
|
||||
func (e *Event) SendPing(c *gin.Context) {
|
||||
|
@ -150,7 +155,7 @@ func (e *Event) SendPing(c *gin.Context) {
|
|||
Event: "ping",
|
||||
Data: "Ping",
|
||||
})
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ping"})
|
||||
}
|
||||
|
||||
func (e *Event) SendPong(c *gin.Context) {
|
||||
|
@ -158,6 +163,7 @@ func (e *Event) SendPong(c *gin.Context) {
|
|||
Event: "pong",
|
||||
Data: "Pong",
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "pong"})
|
||||
}
|
||||
|
||||
func remove(slice []Client, s *Client) []Client {
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"version":"1704556541085"}
|
||||
{"version":"1704628342008"}
|
Binary file not shown.
Binary file not shown.
|
@ -5,17 +5,17 @@
|
|||
<link rel="icon" href="/favicon.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<link rel="modulepreload" href="/_app/immutable/entry/start.IlXTmMTk.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/entry/start.yEbS545I.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/chunks/scheduler.k-kUyWhY.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/chunks/singletons.JSxIQYeI.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/entry/app.sUqO_5CB.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/chunks/singletons.sQrbzYB-.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/entry/app.zjPk-GsH.js">
|
||||
<link rel="modulepreload" href="/_app/immutable/chunks/index.PctGSwCt.js">
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">
|
||||
<script>
|
||||
{
|
||||
__sveltekit_qniv18 = {
|
||||
__sveltekit_bea7py = {
|
||||
base: "",
|
||||
env: null
|
||||
};
|
||||
|
@ -23,8 +23,8 @@
|
|||
const element = document.currentScript.parentElement;
|
||||
|
||||
Promise.all([
|
||||
import("/_app/immutable/entry/start.IlXTmMTk.js"),
|
||||
import("/_app/immutable/entry/app.sUqO_5CB.js")
|
||||
import("/_app/immutable/entry/start.yEbS545I.js"),
|
||||
import("/_app/immutable/entry/app.zjPk-GsH.js")
|
||||
]).then(([kit, app]) => {
|
||||
kit.start(app, element);
|
||||
});
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -5,17 +5,17 @@
|
|||
<link rel="icon" href="../favicon.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
|
||||
<link rel="modulepreload" href="../_app/immutable/entry/start.IlXTmMTk.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/entry/start.yEbS545I.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/chunks/scheduler.k-kUyWhY.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/chunks/singletons.JSxIQYeI.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/entry/app.sUqO_5CB.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/chunks/singletons.sQrbzYB-.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/entry/app.zjPk-GsH.js">
|
||||
<link rel="modulepreload" href="../_app/immutable/chunks/index.PctGSwCt.js">
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">
|
||||
<script>
|
||||
{
|
||||
__sveltekit_qniv18 = {
|
||||
__sveltekit_bea7py = {
|
||||
base: new URL("..", location).pathname.slice(0, -1),
|
||||
env: null
|
||||
};
|
||||
|
@ -23,8 +23,8 @@
|
|||
const element = document.currentScript.parentElement;
|
||||
|
||||
Promise.all([
|
||||
import("../_app/immutable/entry/start.IlXTmMTk.js"),
|
||||
import("../_app/immutable/entry/app.sUqO_5CB.js")
|
||||
import("../_app/immutable/entry/start.yEbS545I.js"),
|
||||
import("../_app/immutable/entry/app.zjPk-GsH.js")
|
||||
]).then(([kit, app]) => {
|
||||
kit.start(app, element);
|
||||
});
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,8 +1,11 @@
|
|||
<script>
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
let events = ['ping', 'pong'];
|
||||
|
||||
$: msg = '';
|
||||
$: jmsg = {};
|
||||
$: recv = '';
|
||||
$: dping = '';
|
||||
$: dpong = '';
|
||||
|
||||
|
@ -13,7 +16,13 @@
|
|||
}
|
||||
|
||||
function SSEMsg() {
|
||||
const sse = new EventSource(`/api/sse?event=ping&event=pong`);
|
||||
let url = '/api/sse?';
|
||||
for (let e in events) {
|
||||
url += `event=${events[e]}&`;
|
||||
}
|
||||
console.log(`sse url: ${url}`);
|
||||
const sse = new EventSource(url);
|
||||
|
||||
console.log('subscribe ping pong');
|
||||
|
||||
sse.onopen = () => {
|
||||
|
@ -42,35 +51,35 @@
|
|||
}
|
||||
|
||||
function Subscribe() {
|
||||
const sse = new EventSource(`/api/sse`);
|
||||
const sse = new EventSource(`/api/sse?event=*`);
|
||||
console.log('subscribe');
|
||||
|
||||
sse.onopen = () => {
|
||||
console.log(`sse open ${now()}`);
|
||||
msg = `sse open ${now()}`;
|
||||
};
|
||||
// sse.onopen = () => {
|
||||
// console.log(`sse open ${now()}`);
|
||||
// msg = `sse open ${now()}`;
|
||||
// };
|
||||
|
||||
sse.onerror = () => {
|
||||
console.log(`sse error ${now()}`);
|
||||
if (sse.readyState === EventSource.CONNECTING) {
|
||||
console.log(`sse reconnecting ${now()}`);
|
||||
msg = `sse reconnecting ${now()}`;
|
||||
} else {
|
||||
msg = `sse error ${now()}`;
|
||||
}
|
||||
};
|
||||
// sse.onerror = () => {
|
||||
// console.log(`sse error ${now()}`);
|
||||
// if (sse.readyState === EventSource.CONNECTING) {
|
||||
// console.log(`sse reconnecting ${now()}`);
|
||||
// msg = `sse reconnecting ${now()}`;
|
||||
// } else {
|
||||
// msg = `sse error ${now()}`;
|
||||
// }
|
||||
// };
|
||||
|
||||
sse.onmessage = (e) => {
|
||||
let Msg = JSON.parse(e.data);
|
||||
console.log(`received: ${JSON.stringify(Msg)}`);
|
||||
msg = `received: ${JSON.stringify(Msg)} ${now()}`;
|
||||
jmsg = Msg;
|
||||
recv = `received: ${JSON.stringify(Msg)} ${now()}`;
|
||||
};
|
||||
|
||||
sse.addEventListener('ping', (e) => {
|
||||
let Msg = JSON.parse(e.data);
|
||||
msg = `ping: ${Msg.id} ${Msg.data} ${now()}`;
|
||||
console.log(msg);
|
||||
jmsg = Msg;
|
||||
dping = Msg.id;
|
||||
});
|
||||
|
||||
|
@ -78,6 +87,7 @@
|
|||
let Msg = JSON.parse(e.data);
|
||||
msg = `pong: ${Msg.id} ${Msg.data} ${now()}`;
|
||||
console.log(msg);
|
||||
jmsg = Msg;
|
||||
dpong = Msg.id;
|
||||
});
|
||||
|
||||
|
@ -85,6 +95,7 @@
|
|||
let Msg = JSON.parse(e.data);
|
||||
msg = `Msg: ${Msg.id} ${Msg.data} ${now()}`;
|
||||
console.log(msg);
|
||||
jmsg = Msg;
|
||||
});
|
||||
|
||||
return () => {
|
||||
|
@ -113,11 +124,14 @@
|
|||
|
||||
<h1>{msg}</h1>
|
||||
|
||||
<ul>
|
||||
<li>{jmsg.event}</li>
|
||||
<li>{jmsg.data}</li>
|
||||
<li>{jmsg.id}</li>
|
||||
</ul>
|
||||
{#if recv != ''}
|
||||
<pre>{recv}</pre>
|
||||
{/if}
|
||||
|
||||
{#if jmsg != {}}
|
||||
<pre>{JSON.stringify(jmsg, null, 2)}</pre>
|
||||
{/if}
|
||||
|
||||
<br />
|
||||
<h2>Ping: {dping}</h2>
|
||||
<br />
|
||||
|
|
Loading…
Add table
Reference in a new issue