Skip to content

Commit 8542601

Browse files
feat: display sent and received events
1 parent 7df8471 commit 8542601

8 files changed

Lines changed: 137 additions & 6 deletions

File tree

lib/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,31 @@ const registerVerboseListeners = (
365365
});
366366
});
367367

368+
if (nsp !== adminNamespace) {
369+
if (typeof socket.onAny === "function") {
370+
socket.onAny((...args: any[]) => {
371+
adminNamespace.emit(
372+
"event_received",
373+
nsp.name,
374+
socket.id,
375+
args,
376+
new Date()
377+
);
378+
});
379+
}
380+
if (typeof socket.onAnyOutgoing === "function") {
381+
socket.onAnyOutgoing((...args: any[]) => {
382+
adminNamespace.emit(
383+
"event_sent",
384+
nsp.name,
385+
socket.id,
386+
args,
387+
new Date()
388+
);
389+
});
390+
}
391+
}
392+
368393
socket.on("disconnect", (reason: string) => {
369394
adminNamespace.emit(
370395
"socket_disconnected",

lib/typed-events.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export interface ServerEvents {
6363
) => void;
6464
room_joined: (nsp: string, room: string, id: string, timestamp: Date) => void;
6565
room_left: (nsp: string, room: string, id: string, timestamp: Date) => void;
66+
event_received: (
67+
nsp: string,
68+
id: string,
69+
args: any[],
70+
timestamp: Date
71+
) => void;
72+
event_sent: (nsp: string, id: string, args: any[], timestamp: Date) => void;
6673
}
6774

6875
export interface ClientEvents {

ui/src/App.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ export default {
194194
this.$store.commit("main/onRoomLeft", { timestamp, nsp, room, id });
195195
}
196196
);
197+
socket.on("event_received", (nsp, id, args, timestamp) => {
198+
this.$store.commit("main/onEventReceived", {
199+
timestamp,
200+
nsp,
201+
id,
202+
args,
203+
});
204+
});
205+
socket.on("event_sent", (nsp, id, args, timestamp) => {
206+
this.$store.commit("main/onEventSent", { timestamp, nsp, id, args });
207+
});
197208
},
198209
199210
onSubmit(form) {

ui/src/components/EventType.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export default {
2525
return colors.amber.base;
2626
case "disconnection":
2727
return colors.red.base;
28+
case "event_received":
29+
return colors.blue.base;
30+
case "event_sent":
31+
return colors.orange.base;
2832
}
2933
return colors.gray.base;
3034
},

ui/src/locales/en.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@
101101
"connection": "Connection",
102102
"disconnection": "Disconnection",
103103
"room_joined": "Room joined",
104-
"room_left": "Room left"
105-
}
104+
"room_left": "Room left",
105+
"event_received": "Event received",
106+
"event_sent": "Event sent"
107+
},
108+
"eventName": "Event name",
109+
"eventArgs": "Event arguments",
110+
"reason": "Reason",
111+
"room": "Room"
106112
}
107113
}

ui/src/locales/fr.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@
9898
"connection": "Connexion",
9999
"disconnection": "Déconnexion",
100100
"room_joined": "Salle rejointe",
101-
"room_left": "Salle quittée"
102-
}
101+
"room_left": "Salle quittée",
102+
"event_received": "Évènement reçu",
103+
"event_sent": "Évènement envoyé"
104+
},
105+
"eventName": "Nom de l'évènement",
106+
"eventArgs": "Argument de l'évènement",
107+
"reason": "Raison",
108+
"room": "Salle"
103109
}
104110
}

ui/src/store/modules/main.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,27 @@ export default {
234234
return elem.timestamp < Date.now() - TEN_MINUTES;
235235
});
236236
},
237+
onEventReceived(state, { timestamp, nsp, id, args }) {
238+
const namespace = getOrCreateNamespace(state.namespaces, nsp);
239+
const eventName = args.shift();
240+
pushEvents(namespace.events, {
241+
type: "event_received",
242+
timestamp,
243+
id,
244+
eventName,
245+
args,
246+
});
247+
},
248+
onEventSent(state, { timestamp, nsp, id, args }) {
249+
const namespace = getOrCreateNamespace(state.namespaces, nsp);
250+
const eventName = args.shift();
251+
pushEvents(namespace.events, {
252+
type: "event_sent",
253+
timestamp,
254+
id,
255+
eventName,
256+
args,
257+
});
258+
},
237259
},
238260
};

ui/src/views/Events.vue

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
item-key="eventId"
1515
:sort-by="['timestamp', 'eventId']"
1616
:sort-desc="[true, true]"
17+
single-expand
18+
show-expand
1719
>
1820
<template #item.type="{ value }">
1921
<EventType :type="value" />
@@ -24,6 +26,51 @@
2426
value
2527
}}</router-link>
2628
</template>
29+
30+
<template #item.args="{ item, value }">
31+
<span v-if="isExpandable(item)">
32+
{{ $t("events.eventName") }}{{ $t("separator")
33+
}}<code>{{ item.eventName }}</code>
34+
</span>
35+
<span v-else-if="item.type === 'disconnection'">
36+
{{ $t("events.reason") }}{{ $t("separator")
37+
}}<code>{{ value }}</code>
38+
</span>
39+
<span
40+
v-else-if="item.type === 'room_joined' || item.type === 'room_left'"
41+
>
42+
{{ $t("events.room") }}{{ $t("separator") }}<code>{{ value }}</code>
43+
</span>
44+
<span v-else>
45+
{{ value }}
46+
</span>
47+
</template>
48+
49+
<template #item.data-table-expand="{ item, isExpanded, expand }">
50+
<v-btn
51+
@click="expand(true)"
52+
v-if="isExpandable(item) && !isExpanded"
53+
icon
54+
>
55+
<v-icon>mdi-chevron-down</v-icon>
56+
</v-btn>
57+
<v-btn
58+
@click="expand(false)"
59+
v-if="isExpandable(item) && isExpanded"
60+
icon
61+
>
62+
<v-icon>mdi-chevron-up</v-icon>
63+
</v-btn>
64+
</template>
65+
66+
<template #expanded-item="{ headers, item }">
67+
<td :colspan="headers.length">
68+
<div class="ma-3">
69+
{{ $t("events.eventArgs") }}{{ $t("separator") }}
70+
<pre><code>{{ item.args }}</code></pre>
71+
</div>
72+
</td>
73+
</template>
2774
</v-data-table>
2875
</v-card>
2976
</div>
@@ -42,7 +89,7 @@ export default {
4289
data() {
4390
return {
4491
footerProps: {
45-
"items-per-page-options": [20, 100, -1],
92+
"items-per-page-options": [-1],
4693
},
4794
};
4895
},
@@ -73,10 +120,10 @@ export default {
73120
sortable: false,
74121
},
75122
{
76-
text: this.$t("args"),
77123
value: "args",
78124
sortable: false,
79125
},
126+
{ text: "", value: "data-table-expand" },
80127
];
81128
},
82129
...mapGetters("main", ["events"]),
@@ -92,6 +139,9 @@ export default {
92139
params: { nsp: this.selectedNamespace.name, id: sid },
93140
};
94141
},
142+
isExpandable(item) {
143+
return ["event_received", "event_sent"].includes(item.type);
144+
},
95145
},
96146
};
97147
</script>

0 commit comments

Comments
 (0)