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