api-client/pages/websocket.vue

248 lines
7 KiB
Vue
Raw Normal View History

2019-08-24 14:51:03 +00:00
<template>
<div class="page">
2019-10-22 12:02:26 +00:00
<pw-section class="blue" icon="cloud_upload" label="Request" ref="request">
<ul>
<li>
<label for="url">URL</label>
2019-10-25 08:14:34 +00:00
<input
id="url"
type="url"
:class="{ error: !urlValid }"
v-model="url"
@keyup.enter="urlValid ? toggleConnection() : null"
/>
</li>
2019-09-27 09:14:15 +00:00
<li>
<label for="connect" class="hide-on-small-screen">&nbsp;</label>
<button :disabled="!urlValid" id="connect" name="connect" @click="toggleConnection">
{{ toggleConnectionVerb }}
<span>
<i class="material-icons" v-if="!connectionState">sync</i>
<i class="material-icons" v-if="connectionState">sync_disabled</i>
</span>
</button>
</li>
</ul>
</pw-section>
2019-10-25 08:14:34 +00:00
<pw-section
class="purple"
icon="cloud_download"
label="Communication"
id="response"
ref="response"
>
<ul>
<li>
<label for="log">Log</label>
2019-08-26 09:07:02 +00:00
<div id="log" name="log" class="log">
2019-08-27 11:52:24 +00:00
<span v-if="communication.log">
2019-10-25 08:14:34 +00:00
<span
v-for="(logEntry, index) in communication.log"
:style="{ color: logEntry.color }"
:key="index"
>@ {{ logEntry.ts }} {{ getSourcePrefix(logEntry.source) }} {{ logEntry.payload }}</span>
2019-08-27 11:52:24 +00:00
</span>
<span v-else>(waiting for connection)</span>
</div>
</li>
</ul>
<ul>
<li>
<label for="message">Message</label>
2019-10-25 08:14:34 +00:00
<input
id="message"
name="message"
type="text"
v-model="communication.input"
:readonly="!connectionState"
@keyup.enter="connectionState ? sendMessage() : null"
/>
</li>
2019-09-27 09:14:15 +00:00
<li>
<label for="send" class="hide-on-small-screen">&nbsp;</label>
<button id="send" name="send" :disabled="!connectionState" @click="sendMessage">
Send
<span>
<i class="material-icons">send</i>
</span>
</button>
</li>
</ul>
</pw-section>
2019-08-24 14:51:03 +00:00
</div>
</template>
<style lang="scss">
div.log {
2019-08-24 14:51:03 +00:00
margin: 4px;
padding: 8px 16px;
width: calc(100% - 8px);
2019-09-17 08:13:12 +00:00
border-radius: 8px;
background-color: var(--bg-dark-color);
2019-08-24 14:51:03 +00:00
color: var(--fg-color);
2019-08-26 09:07:02 +00:00
height: 256px;
2019-08-24 14:51:03 +00:00
overflow: auto;
2019-08-27 11:52:24 +00:00
&,
span {
2019-08-24 14:51:03 +00:00
font-size: 18px;
2019-10-25 08:14:34 +00:00
font-family: "Roboto Mono", monospace;
2019-08-24 14:51:03 +00:00
}
span {
display: block;
white-space: pre-wrap;
}
}
2019-08-27 11:52:24 +00:00
</style>
2019-10-25 08:14:34 +00:00
2019-08-24 14:51:03 +00:00
<script>
import section from "../components/section";
2019-08-24 14:51:03 +00:00
export default {
components: {
2019-10-25 08:14:34 +00:00
"pw-section": section
},
2019-08-27 11:52:24 +00:00
data() {
return {
connectionState: false,
url: "wss://echo.websocket.org",
socket: null,
communication: {
log: null,
input: ""
2019-08-24 14:51:03 +00:00
}
2019-10-25 08:14:34 +00:00
};
},
computed: {
2019-08-27 11:52:24 +00:00
toggleConnectionVerb() {
return !this.connectionState ? "Connect" : "Disconnect";
},
2019-08-27 11:52:24 +00:00
urlValid() {
2019-10-25 08:14:34 +00:00
const pattern = new RegExp(
"^(wss?:\\/\\/)?" +
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
"((\\d{1,3}\\.){3}\\d{1,3}))" +
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" +
"(\\?[;&a-z\\d%_.~+=-]*)?" +
"(\\#[-a-z\\d_]*)?$",
"i"
);
2019-08-27 11:52:24 +00:00
return pattern.test(this.url);
}
},
methods: {
2019-08-27 11:52:24 +00:00
toggleConnection() {
// If it is connecting:
2019-08-27 11:52:24 +00:00
if (!this.connectionState) return this.connect();
// Otherwise, it's disconnecting.
else return this.disconnect();
2019-08-24 14:51:03 +00:00
},
2019-08-27 11:52:24 +00:00
connect() {
2019-10-25 08:14:34 +00:00
this.communication.log = [
{
payload: `Connecting to ${this.url}...`,
source: "info",
color: "var(--ac-color)"
}
];
try {
2019-08-27 11:52:24 +00:00
this.socket = new WebSocket(this.url);
2019-10-25 08:14:34 +00:00
this.socket.onopen = event => {
2019-08-27 11:52:24 +00:00
this.connectionState = true;
2019-10-25 08:14:34 +00:00
this.communication.log = [
{
payload: `Connected to ${this.url}.`,
source: "info",
color: "var(--ac-color)",
ts: new Date().toLocaleTimeString()
}
];
this.$toast.success("Connected", {
icon: "sync"
2019-09-26 09:55:37 +00:00
});
2019-08-27 11:52:24 +00:00
};
2019-10-25 08:14:34 +00:00
this.socket.onerror = event => {
2019-08-27 11:52:24 +00:00
this.handleError();
};
2019-10-25 08:14:34 +00:00
this.socket.onclose = event => {
2019-08-27 11:52:24 +00:00
this.connectionState = false;
this.communication.log.push({
payload: `Disconnected from ${this.url}.`,
2019-10-25 08:14:34 +00:00
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString()
2019-08-27 11:52:24 +00:00
});
2019-10-25 08:14:34 +00:00
this.$toast.error("Disconnected", {
icon: "sync_disabled"
2019-09-26 09:55:37 +00:00
});
2019-08-27 11:52:24 +00:00
};
2019-10-25 08:14:34 +00:00
this.socket.onmessage = event => {
2019-08-27 11:52:24 +00:00
this.communication.log.push({
payload: event.data,
2019-10-25 08:14:34 +00:00
source: "server",
ts: new Date().toLocaleTimeString()
2019-08-27 11:52:24 +00:00
});
2019-10-25 08:14:34 +00:00
};
2019-08-27 11:52:24 +00:00
} catch (ex) {
this.handleError(ex);
2019-10-25 08:14:34 +00:00
this.$toast.error("Something went wrong!", {
icon: "error"
2019-09-26 09:55:37 +00:00
});
2019-08-27 11:52:24 +00:00
}
},
2019-08-27 11:52:24 +00:00
disconnect() {
if (this.socket != null) this.socket.close();
},
handleError(error) {
this.disconnect();
this.connectionState = false;
this.communication.log.push({
payload: `An error has occurred.`,
2019-10-25 08:14:34 +00:00
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString()
2019-08-27 11:52:24 +00:00
});
2019-10-25 08:14:34 +00:00
if (error != null)
this.communication.log.push({
payload: error,
source: "info",
color: "#ff5555",
ts: new Date().toLocaleTimeString()
});
2019-08-27 11:52:24 +00:00
},
sendMessage() {
const message = this.communication.input;
this.socket.send(message);
this.communication.log.push({
2019-08-27 11:52:24 +00:00
payload: message,
2019-10-25 08:14:34 +00:00
source: "client",
ts: new Date().toLocaleTimeString()
});
this.communication.input = "";
},
2019-10-25 08:14:34 +00:00
collapse({ target }) {
const el = target.parentNode.className;
2019-10-25 08:14:34 +00:00
document.getElementsByClassName(el)[0].classList.toggle("hidden");
},
2019-08-27 11:52:24 +00:00
getSourcePrefix(source) {
const sourceEmojis = {
// Source used for info messages.
2019-10-25 08:14:34 +00:00
info: "\t [INFO]:\t",
// Source used for client to server messages.
2019-10-25 08:14:34 +00:00
client: "\t👽 [SENT]:\t",
// Source used for server to client messages.
2019-10-25 08:14:34 +00:00
server: "\t📥 [RECEIVED]:\t"
};
2019-10-25 08:14:34 +00:00
if (Object.keys(sourceEmojis).includes(source))
return sourceEmojis[source];
return "";
2019-08-24 14:51:03 +00:00
}
},
2019-10-25 08:14:34 +00:00
updated: function() {
this.$nextTick(function() {
var divLog = document.getElementById("log");
divLog.scrollBy(0, divLog.scrollHeight + 100);
});
}
2019-10-25 08:14:34 +00:00
};
2019-08-24 14:51:03 +00:00
</script>