Merge pull request #663 from liyasthomas/feature/mqtt

Feature/mqtt
This commit is contained in:
Liyas Thomas
2020-03-12 01:44:59 +05:30
committed by GitHub
9 changed files with 635 additions and 332 deletions

View File

@@ -94,6 +94,8 @@ _Customized themes are synced with local session storage_
🌩 **Socket.IO**: Send and Receive data with socketio server. SocketIO is popular websocket solution.
🦟 **MQTT**: Subscribe and Publish to topics of a MQTT Broker.
🔮 **GraphQL**: GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.
- Set endpoint and get schemas

View File

@@ -0,0 +1,261 @@
<template>
<div>
<pw-section class="blue" :label="$t('request')">
<ul>
<li>
<label for="url">{{ $t("url") }}</label>
<input id="url" type="url" v-model="url" spellcheck="false" />
</li>
<div>
<li>
<label for="connect">&nbsp;</label>
<button id="connect" :disabled="!validUrl" @click="toggleConnection">
{{ this.connectionState ? $t("disconnect") : $t("connect") }}
<span>
<i class="material-icons">{{ !connectionState ? "sync" : "sync_disabled" }}</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<pw-section class="blue" :label="$t('communication')">
<ul>
<li>
<realtime-log :title="$t('log')" :log="this.log" />
</li>
</ul>
<ul>
<li>
<label for="pub_topic">{{ $t("mqtt_topic") }}</label>
<input id="pub_topic" type="text" v-model="pub_topic" spellcheck="false" />
</li>
<li>
<label for="message">{{ $t("message") }}</label>
<input id="message" type="text" v-model="msg" spellcheck="false" />
</li>
<div>
<li>
<label for="publish">&nbsp;</label>
<button id="publish" name="get" :disabled="!canpublish" @click="publish">
{{ $t("mqtt_publish") }}
<span>
<i class="material-icons">send</i>
</span>
</button>
</li>
</div>
</ul>
<ul>
<li>
<label for="sub_topic">{{ $t("mqtt_topic") }}</label>
<input id="sub_topic" type="text" v-model="sub_topic" spellcheck="false" />
</li>
<div>
<li>
<label for="subscribe">&nbsp;</label>
<button id="subscribe" name="get" :disabled="!cansubscribe" @click="toggleSubscription">
{{ subscriptionState ? $t("mqtt_unsubscribe") : $t("mqtt_subscribe") }}
<span>
<i class="material-icons">{{ subscriptionState ? "sync_disabled" : "sync" }}</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
</div>
</template>
<script>
import Paho from "paho-mqtt"
import { wsValid } from "~/functions/utils/valid"
export default {
components: {
"pw-section": () => import("../../components/layout/section"),
realtimeLog: () => import("./log"),
},
data: function() {
return {
url: "wss://test.mosquitto.org:8081",
client: null,
pub_topic: "",
sub_topic: "",
msg: "",
connectionState: false,
log: null,
manualDisconnect: false,
subscriptionState: false,
}
},
computed: {
validUrl() {
return wsValid(this.url)
},
canpublish() {
return this.pub_topic != "" && this.msg != "" && this.connectionState
},
cansubscribe() {
return this.sub_topic != "" && this.connectionState
},
},
methods: {
connect() {
this.log = [
{
payload: this.$t("connecting_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
},
]
let parseUrl = new URL(this.url)
this.client = new Paho.Client(
parseUrl.hostname,
parseUrl.port != "" ? Number(parseUrl.port) : 8081,
"postwoman"
)
this.client.connect({
onSuccess: this.onConnectionSuccess,
onFailure: this.onConnectionFailure,
})
this.client.onConnectionLost = this.onConnectionLost
this.client.onMessageArrived = this.onMessageArrived
},
onConnectionFailure() {
this.connectionState = false
this.log.push({
payload: this.$t("error_occurred"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
onConnectionSuccess() {
this.connectionState = true
this.log.push({
payload: this.$t("connected_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
})
this.$toast.success(this.$t("connected"), {
icon: "sync",
})
},
onMessageArrived(message) {
debugger
this.log.push({
payload: `Message: ${message.payloadString} arrived on topic: ${message.destinationName}`,
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
})
},
toggleConnection() {
if (this.connectionState) {
this.disconnect()
} else {
this.connect()
}
},
disconnect() {
this.manualDisconnect = true
this.client.disconnect()
this.log.push({
payload: this.$t("disconnected_from", { name: this.url }),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
onConnectionLost() {
this.connectionState = false
if (this.manualDisconnect) {
this.$toast.error(this.$t("disconnected"), {
icon: "sync_disabled",
})
} else {
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
}
this.manualDisconnect = false
this.subscriptionState = false
},
publish() {
try {
this.client.publish(this.pub_topic, this.msg, 0, false)
this.log.push({
payload: `Published message: ${this.msg} to topic: ${this.pub_topic}`,
ts: new Date().toLocaleTimeString(),
source: "info",
color: "var(--ac-color)",
})
} catch (e) {
this.log.push({
payload:
this.$t("error_occurred") +
`while publishing msg: ${this.msg} to topic: ${this.pub_topic}`,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
}
},
toggleSubscription() {
if (this.subscriptionState) {
this.unsubscribe()
} else {
this.subscribe()
}
},
subscribe() {
try {
this.client.subscribe(this.sub_topic, {
onSuccess: this.usubSuccess,
onFailure: this.usubFailure,
})
} catch (e) {
this.log.push({
payload: this.$t("error_occurred") + `while subscribing to topic: ${this.sub_topic}`,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
}
},
usubSuccess() {
this.subscriptionState = !this.subscriptionState
this.log.push({
payload:
`Successfully ` +
(this.subscriptionState ? "subscribed" : "unsubscribed") +
` to topic: ${this.sub_topic}`,
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
})
},
usubFailure() {
this.log.push({
payload:
`Failed to ` +
(this.subscriptionState ? "unsubscribe" : "subscribe") +
` to topic: ${this.sub_topic}`,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
unsubscribe() {
this.client.unsubscribe(this.sub_topic, {
onSuccess: this.usubSuccess,
onFailure: this.usubFailure,
})
},
},
}
</script>

View File

@@ -32,7 +32,7 @@
<pw-section class="purple" :label="$t('communication')" id="response" ref="response">
<ul>
<li>
<realtime-log :log="communication.log" />
<realtime-log :title="$t('log')" :log="communication.log" />
</li>
</ul>
<ul>
@@ -76,12 +76,11 @@
<script>
import { socketioValid } from "~/functions/utils/valid"
import io from "socket.io-client"
import realtimeLog from "./log"
export default {
components: {
"pw-section": () => import("../../components/layout/section"),
realtimeLog,
realtimeLog: () => import("./log"),
},
data() {
return {

160
components/realtime/sse.vue Normal file
View File

@@ -0,0 +1,160 @@
<template>
<div class="page">
<pw-section class="blue" :label="$t('request')" ref="request">
<ul>
<li>
<label for="server">{{ $t("server") }}</label>
<input
id="server"
type="url"
:class="{ error: !serverValid }"
v-model="server"
@keyup.enter="serverValid ? toggleSSEConnection() : null"
/>
</li>
<div>
<li>
<label for="start" class="hide-on-small-screen">&nbsp;</label>
<button :disabled="!serverValid" id="start" name="start" @click="toggleSSEConnection">
{{ !connectionSSEState ? $t("start") : $t("stop") }}
<span>
<i class="material-icons">
{{ !connectionSSEState ? "sync" : "sync_disabled" }}
</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<pw-section class="purple" :label="$t('communication')" id="response" ref="response">
<ul>
<li>
<realtime-log :title="$t('events')" :log="events.log" />
<div id="result"></div>
</li>
</ul>
</pw-section>
</div>
</template>
<script>
import { sseValid } from "~/functions/utils/valid"
export default {
components: {
"pw-section": () => import("../layout/section"),
realtimeLog: () => import("./log"),
},
data() {
return {
connectionSSEState: false,
server: "https://express-eventsource.herokuapp.com/events",
sse: null,
events: {
log: null,
input: "",
},
}
},
computed: {
serverValid() {
return sseValid(this.server)
},
},
methods: {
toggleSSEConnection() {
// If it is connecting:
if (!this.connectionSSEState) return this.start()
// Otherwise, it's disconnecting.
else return this.stop()
},
start() {
this.events.log = [
{
payload: this.$t("connecting_to", { name: this.server }),
source: "info",
color: "var(--ac-color)",
},
]
if (typeof EventSource !== "undefined") {
try {
this.sse = new EventSource(this.server)
this.sse.onopen = event => {
this.connectionSSEState = true
this.events.log = [
{
payload: this.$t("connected_to", { name: this.server }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
},
]
this.$toast.success(this.$t("connected"), {
icon: "sync",
})
}
this.sse.onerror = event => {
this.handleSSEError()
}
this.sse.onclose = event => {
this.connectionSSEState = false
this.events.log.push({
payload: this.$t("disconnected_from", { name: this.server }),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
this.$toast.error(this.$t("disconnected"), {
icon: "sync_disabled",
})
}
this.sse.onmessage = event => {
this.events.log.push({
payload: event.data,
source: "server",
ts: new Date().toLocaleTimeString(),
})
}
} catch (ex) {
this.handleSSEError(ex)
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
}
} else {
this.events.log = [
{
payload: this.$t("browser_support_sse"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
},
]
}
},
handleSSEError(error) {
this.stop()
this.connectionSSEState = false
this.events.log.push({
payload: this.$t("error_occurred"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
if (error !== null)
this.events.log.push({
payload: error,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
stop() {
this.sse.onclose()
this.sse.close()
},
},
}
</script>

View File

@@ -0,0 +1,182 @@
<template>
<div class="page">
<pw-section class="blue" :label="$t('request')" ref="request">
<ul>
<li>
<label for="url">{{ $t("url") }}</label>
<input
id="url"
type="url"
spellcheck="false"
:class="{ error: !urlValid }"
v-model="url"
@keyup.enter="urlValid ? toggleConnection() : null"
/>
</li>
<div>
<li>
<label for="connect" class="hide-on-small-screen">&nbsp;</label>
<button :disabled="!urlValid" id="connect" name="connect" @click="toggleConnection">
{{ !connectionState ? $t("connect") : $t("disconnect") }}
<span>
<i class="material-icons">
{{ !connectionState ? "sync" : "sync_disabled" }}
</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<pw-section class="purple" :label="$t('communication')" id="response" ref="response">
<ul>
<li>
<realtime-log :title="$t('log')" :log="communication.log" />
</li>
</ul>
<ul>
<li>
<label for="message">{{ $t("message") }}</label>
<input
id="message"
name="message"
type="text"
v-model="communication.input"
:readonly="!connectionState"
@keyup.enter="connectionState ? sendMessage() : null"
/>
</li>
<div>
<li>
<label for="send" class="hide-on-small-screen">&nbsp;</label>
<button id="send" name="send" :disabled="!connectionState" @click="sendMessage">
{{ $t("send") }}
<span>
<i class="material-icons">send</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
</div>
</template>
<script>
import { wsValid } from "~/functions/utils/valid"
export default {
components: {
"pw-section": () => import("../layout/section"),
realtimeLog: () => import("./log"),
},
data() {
return {
connectionState: false,
url: "wss://echo.websocket.org",
socket: null,
communication: {
log: null,
input: "",
},
}
},
computed: {
urlValid() {
return wsValid(this.url)
},
},
methods: {
toggleConnection() {
// If it is connecting:
if (!this.connectionState) return this.connect()
// Otherwise, it's disconnecting.
else return this.disconnect()
},
connect() {
this.communication.log = [
{
payload: this.$t("connecting_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
},
]
try {
this.socket = new WebSocket(this.url)
this.socket.onopen = event => {
this.connectionState = true
this.communication.log = [
{
payload: this.$t("connected_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
},
]
this.$toast.success(this.$t("connected"), {
icon: "sync",
})
}
this.socket.onerror = event => {
this.handleError()
}
this.socket.onclose = event => {
this.connectionState = false
this.communication.log.push({
payload: this.$t("disconnected_from", { name: this.url }),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
this.$toast.error(this.$t("disconnected"), {
icon: "sync_disabled",
})
}
this.socket.onmessage = event => {
this.communication.log.push({
payload: event.data,
source: "server",
ts: new Date().toLocaleTimeString(),
})
}
} catch (ex) {
this.handleError(ex)
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
}
},
disconnect() {
this.socket.close()
},
handleError(error) {
this.disconnect()
this.connectionState = false
this.communication.log.push({
payload: this.$t("error_occurred"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
if (error !== null)
this.communication.log.push({
payload: error,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
sendMessage() {
const message = this.communication.input
this.socket.send(message)
this.communication.log.push({
payload: message,
source: "client",
ts: new Date().toLocaleTimeString(),
})
this.communication.input = ""
},
},
}
</script>

View File

@@ -276,4 +276,10 @@ export default {
notes: "Notes",
socketio: "Socket.IO",
event_name: "Event Name",
mqtt: "MQTT",
mqtt_topic: "Topic",
mqtt_topic_title: "Publish / Subscribe topic",
mqtt_publish: "Publish",
mqtt_subscribe: "Subscribe",
mqtt_unsubscribe: "Unsubscribe",
}

17
package-lock.json generated
View File

@@ -2198,9 +2198,9 @@
"integrity": "sha512-8ZVAxwyCGAxQX8mOp9imSXH0hoSPkGfy8igJy+WO/7axL30saRhKgg1XPACSmxxPA7nfHVwM+ShWXT+vKsNuFg=="
},
"acorn": {
"version": "6.4.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.0.tgz",
"integrity": "sha512-gac8OEcQ2Li1dxIEWGZzsp2BitJxwkwcOm0zHAJLcPJaVvm58FRnk6RkuLRpU1EujipU2ZFODv2P9DLMfnV8mw=="
"version": "6.4.1",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.1.tgz",
"integrity": "sha512-ZVA9k326Nwrj3Cj9jlh3wGFutC2ZornPNARZwsNYqQYgN0EsV2d53w5RN/co65Ohn4sUAUtb1rSUAOD6XN9idA=="
},
"acorn-walk": {
"version": "6.2.0",
@@ -7322,9 +7322,9 @@
}
},
"kind-of": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz",
"integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA=="
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.3.tgz",
"integrity": "sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw=="
},
"last-call-webpack-plugin": {
"version": "3.0.0",
@@ -8983,6 +8983,11 @@
"resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
"integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ=="
},
"paho-mqtt": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/paho-mqtt/-/paho-mqtt-1.1.0.tgz",
"integrity": "sha512-KPbL9KAB0ASvhSDbOrZBaccXS+/s7/LIofbPyERww8hM5Ko71GUJQ6Nmg0BWqj8phAIT8zdf/Sd/RftHU9i2HA=="
},
"pako": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.10.tgz",

View File

@@ -43,6 +43,7 @@
"graphql-language-service-interface": "^2.3.3",
"nuxt": "^2.11.0",
"nuxt-i18n": "^6.6.0",
"paho-mqtt": "^1.1.0",
"socket.io-client": "^2.3.0",
"v-tooltip": "^2.0.3",
"vue-virtual-scroll-list": "^1.4.6",

View File

@@ -3,348 +3,35 @@
<section id="options">
<tabs>
<tab :id="'websocket'" :label="$t('websocket')" :selected="true">
<pw-section class="blue" :label="$t('request')" ref="request">
<ul>
<li>
<label for="url">{{ $t("url") }}</label>
<input
id="url"
type="url"
spellcheck="false"
:class="{ error: !urlValid }"
v-model="url"
@keyup.enter="urlValid ? toggleConnection() : null"
/>
</li>
<div>
<li>
<label for="connect" class="hide-on-small-screen">&nbsp;</label>
<button
:disabled="!urlValid"
id="connect"
name="connect"
@click="toggleConnection"
>
{{ !connectionState ? $t("connect") : $t("disconnect") }}
<span>
<i class="material-icons">
{{ !connectionState ? "sync" : "sync_disabled" }}
</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<pw-section class="purple" :label="$t('communication')" id="response" ref="response">
<ul>
<li>
<realtime-log :title="$t('log')" :log="communication.log" />
</li>
</ul>
<ul>
<li>
<label for="message">{{ $t("message") }}</label>
<input
id="message"
name="message"
type="text"
v-model="communication.input"
:readonly="!connectionState"
@keyup.enter="connectionState ? sendMessage() : null"
/>
</li>
<div>
<li>
<label for="send" class="hide-on-small-screen">&nbsp;</label>
<button id="send" name="send" :disabled="!connectionState" @click="sendMessage">
{{ $t("send") }}
<span>
<i class="material-icons">send</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<websocket />
</tab>
<tab :id="'sse'" :label="$t('sse')">
<pw-section class="blue" :label="$t('request')" ref="request">
<ul>
<li>
<label for="server">{{ $t("server") }}</label>
<input
id="server"
type="url"
:class="{ error: !serverValid }"
v-model="server"
@keyup.enter="serverValid ? toggleSSEConnection() : null"
/>
</li>
<div>
<li>
<label for="start" class="hide-on-small-screen">&nbsp;</label>
<button
:disabled="!serverValid"
id="start"
name="start"
@click="toggleSSEConnection"
>
{{ !connectionSSEState ? $t("start") : $t("stop") }}
<span>
<i class="material-icons">
{{ !connectionSSEState ? "sync" : "sync_disabled" }}
</i>
</span>
</button>
</li>
</div>
</ul>
</pw-section>
<pw-section class="purple" :label="$t('communication')" id="response" ref="response">
<ul>
<li>
<realtime-log :title="$t('events')" :log="events.log" />
<div id="result"></div>
</li>
</ul>
</pw-section>
<sse />
</tab>
<tab :id="'socketio'" :label="$t('socketio')">
<socketio />
</tab>
<tab :id="'mqtt'" :label="$t('mqtt')">
<mqtt />
</tab>
</tabs>
</section>
</div>
</template>
<script>
import { wsValid, sseValid } from "~/functions/utils/valid"
import realtimeLog from "~/components/realtime/log"
export default {
components: {
"pw-section": () => import("../components/layout/section"),
socketio: () => import("../components/realtime/socketio"),
tabs: () => import("../components/ui/tabs"),
tab: () => import("../components/ui/tab"),
realtimeLog,
},
data() {
return {
connectionState: false,
url: "wss://echo.websocket.org",
socket: null,
communication: {
log: null,
input: "",
},
connectionSSEState: false,
server: "https://express-eventsource.herokuapp.com/events",
sse: null,
events: {
log: null,
input: "",
},
}
},
computed: {
urlValid() {
return wsValid(this.url)
},
serverValid() {
return sseValid(this.server)
},
},
methods: {
toggleConnection() {
// If it is connecting:
if (!this.connectionState) return this.connect()
// Otherwise, it's disconnecting.
else return this.disconnect()
},
connect() {
this.communication.log = [
{
payload: this.$t("connecting_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
},
]
try {
this.socket = new WebSocket(this.url)
this.socket.onopen = event => {
this.connectionState = true
this.communication.log = [
{
payload: this.$t("connected_to", { name: this.url }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
},
]
this.$toast.success(this.$t("connected"), {
icon: "sync",
})
}
this.socket.onerror = event => {
this.handleError()
}
this.socket.onclose = event => {
this.connectionState = false
this.communication.log.push({
payload: this.$t("disconnected_from", { name: this.url }),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
this.$toast.error(this.$t("disconnected"), {
icon: "sync_disabled",
})
}
this.socket.onmessage = event => {
this.communication.log.push({
payload: event.data,
source: "server",
ts: new Date().toLocaleTimeString(),
})
}
} catch (ex) {
this.handleError(ex)
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
}
},
disconnect() {
this.socket.close()
},
handleError(error) {
this.disconnect()
this.connectionState = false
this.communication.log.push({
payload: this.$t("error_occurred"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
if (error !== null)
this.communication.log.push({
payload: error,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
sendMessage() {
const message = this.communication.input
this.socket.send(message)
this.communication.log.push({
payload: message,
source: "client",
ts: new Date().toLocaleTimeString(),
})
this.communication.input = ""
},
collapse({ target }) {
const el = target.parentNode.className
document.getElementsByClassName(el)[0].classList.toggle("hidden")
},
toggleSSEConnection() {
// If it is connecting:
if (!this.connectionSSEState) return this.start()
// Otherwise, it's disconnecting.
else return this.stop()
},
start() {
this.events.log = [
{
payload: this.$t("connecting_to", { name: this.server }),
source: "info",
color: "var(--ac-color)",
},
]
if (typeof EventSource !== "undefined") {
try {
this.sse = new EventSource(this.server)
this.sse.onopen = event => {
this.connectionSSEState = true
this.events.log = [
{
payload: this.$t("connected_to", { name: this.server }),
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString(),
},
]
this.$toast.success(this.$t("connected"), {
icon: "sync",
})
}
this.sse.onerror = event => {
this.handleSSEError()
}
this.sse.onclose = event => {
this.connectionSSEState = false
this.events.log.push({
payload: this.$t("disconnected_from", { name: this.server }),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
this.$toast.error(this.$t("disconnected"), {
icon: "sync_disabled",
})
}
this.sse.onmessage = event => {
this.events.log.push({
payload: event.data,
source: "server",
ts: new Date().toLocaleTimeString(),
})
}
} catch (ex) {
this.handleSSEError(ex)
this.$toast.error(this.$t("something_went_wrong"), {
icon: "error",
})
}
} else {
this.events.log = [
{
payload: this.$t("browser_support_sse"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
},
]
}
},
handleSSEError(error) {
this.stop()
this.connectionSSEState = false
this.events.log.push({
payload: this.$t("error_occurred"),
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
if (error !== null)
this.events.log.push({
payload: error,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString(),
})
},
stop() {
this.sse.onclose()
this.sse.close()
},
websocket: () => import("../components/realtime/websocket"),
sse: () => import("../components/realtime/sse"),
socketio: () => import("../components/realtime/socketio"),
mqtt: () => import("../components/realtime/mqtt"),
},
}
</script>