2019-08-24 14:51:03 +00:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="page">
|
2019-11-01 11:15:53 +00:00
|
|
|
|
<pw-section class="blue" label="Request" ref="request">
|
2019-08-24 23:42:41 +00:00
|
|
|
|
<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"
|
|
|
|
|
|
/>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</li>
|
2019-11-21 01:25:37 +00:00
|
|
|
|
<div>
|
|
|
|
|
|
<li>
|
|
|
|
|
|
<label for="connect" class="hide-on-small-screen"> </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>
|
|
|
|
|
|
</div>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</ul>
|
|
|
|
|
|
</pw-section>
|
2019-11-01 11:15:53 +00:00
|
|
|
|
|
2019-11-12 04:52:50 +00:00
|
|
|
|
<pw-section
|
|
|
|
|
|
class="purple"
|
|
|
|
|
|
label="Communication"
|
|
|
|
|
|
id="response"
|
|
|
|
|
|
ref="response"
|
|
|
|
|
|
>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
<ul>
|
|
|
|
|
|
<li>
|
2019-08-24 23:50:23 +00:00
|
|
|
|
<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"
|
2019-11-12 04:52:50 +00:00
|
|
|
|
>@ {{ logEntry.ts }} {{ getSourcePrefix(logEntry.source) }}
|
|
|
|
|
|
{{ logEntry.payload }}</span
|
|
|
|
|
|
>
|
2019-08-27 11:52:24 +00:00
|
|
|
|
</span>
|
2019-09-20 13:04:42 +00:00
|
|
|
|
<span v-else>(waiting for connection)</span>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>
|
2019-08-24 23:50:23 +00:00
|
|
|
|
<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"
|
|
|
|
|
|
/>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</li>
|
2019-11-21 01:25:37 +00:00
|
|
|
|
<div>
|
|
|
|
|
|
<li>
|
|
|
|
|
|
<label for="send" class="hide-on-small-screen"> </label>
|
|
|
|
|
|
<button
|
|
|
|
|
|
id="send"
|
|
|
|
|
|
name="send"
|
|
|
|
|
|
:disabled="!connectionState"
|
|
|
|
|
|
@click="sendMessage"
|
|
|
|
|
|
>
|
|
|
|
|
|
Send
|
|
|
|
|
|
<span>
|
|
|
|
|
|
<i class="material-icons">send</i>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</div>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</ul>
|
|
|
|
|
|
</pw-section>
|
2019-08-24 14:51:03 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<style lang="scss">
|
2019-11-02 05:32:21 +00:00
|
|
|
|
div.log {
|
|
|
|
|
|
margin: 4px;
|
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
|
width: calc(100% - 8px);
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
background-color: var(--bg-dark-color);
|
|
|
|
|
|
color: var(--fg-color);
|
|
|
|
|
|
height: 256px;
|
|
|
|
|
|
overflow: auto;
|
2019-08-24 14:51:03 +00:00
|
|
|
|
|
2019-11-02 05:32:21 +00:00
|
|
|
|
&,
|
|
|
|
|
|
span {
|
2019-11-21 01:25:37 +00:00
|
|
|
|
font-size: 16px;
|
2019-11-02 05:32:21 +00:00
|
|
|
|
font-family: "Roboto Mono", monospace;
|
2019-11-21 01:25:37 +00:00
|
|
|
|
font-weight: 400;
|
2019-11-02 05:32:21 +00:00
|
|
|
|
}
|
2019-08-24 14:51:03 +00:00
|
|
|
|
|
2019-11-02 05:32:21 +00:00
|
|
|
|
span {
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
white-space: pre-wrap;
|
2019-11-08 18:26:49 +00:00
|
|
|
|
word-wrap: break-word;
|
|
|
|
|
|
word-break: break-all;
|
2019-08-24 14:51:03 +00:00
|
|
|
|
}
|
2019-11-02 05:32:21 +00:00
|
|
|
|
}
|
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>
|
2019-11-02 05:32:21 +00:00
|
|
|
|
export default {
|
|
|
|
|
|
components: {
|
2019-11-15 19:02:27 +00:00
|
|
|
|
"pw-section": () => import("../components/section")
|
2019-11-02 05:32:21 +00:00
|
|
|
|
},
|
|
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
connectionState: false,
|
|
|
|
|
|
url: "wss://echo.websocket.org",
|
|
|
|
|
|
socket: null,
|
|
|
|
|
|
communication: {
|
|
|
|
|
|
log: null,
|
|
|
|
|
|
input: ""
|
2019-08-24 23:42:41 +00:00
|
|
|
|
}
|
2019-11-02 05:32:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
computed: {
|
|
|
|
|
|
toggleConnectionVerb() {
|
|
|
|
|
|
return !this.connectionState ? "Connect" : "Disconnect";
|
2019-08-24 23:42:41 +00:00
|
|
|
|
},
|
2019-11-02 05:32:21 +00:00
|
|
|
|
urlValid() {
|
|
|
|
|
|
const pattern = new RegExp(
|
|
|
|
|
|
"^(wss?:\\/\\/)?" +
|
|
|
|
|
|
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
|
|
|
|
|
|
"((\\d{1,3}\\.){3}\\d{1,3}))" +
|
2019-11-08 18:11:30 +00:00
|
|
|
|
"(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*" +
|
2019-11-02 05:32:21 +00:00
|
|
|
|
"(\\?[;&a-z\\d%_.~+=-]*)?" +
|
|
|
|
|
|
"(\\#[-a-z\\d_]*)?$",
|
|
|
|
|
|
"i"
|
|
|
|
|
|
);
|
|
|
|
|
|
return pattern.test(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: `Connecting to ${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: `Connected to ${this.url}.`,
|
2019-10-25 08:14:34 +00:00
|
|
|
|
source: "info",
|
2019-11-02 05:32:21 +00:00
|
|
|
|
color: "var(--ac-color)",
|
2019-10-25 08:14:34 +00:00
|
|
|
|
ts: new Date().toLocaleTimeString()
|
2019-11-02 05:32:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
];
|
|
|
|
|
|
this.$toast.success("Connected", {
|
|
|
|
|
|
icon: "sync"
|
2019-09-26 09:55:37 +00:00
|
|
|
|
});
|
2019-11-02 05:32:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
this.socket.onerror = event => {
|
|
|
|
|
|
this.handleError();
|
|
|
|
|
|
};
|
|
|
|
|
|
this.socket.onclose = event => {
|
|
|
|
|
|
this.connectionState = false;
|
2019-10-25 08:14:34 +00:00
|
|
|
|
this.communication.log.push({
|
2019-11-02 05:32:21 +00:00
|
|
|
|
payload: `Disconnected from ${this.url}.`,
|
2019-10-25 08:14:34 +00:00
|
|
|
|
source: "info",
|
|
|
|
|
|
color: "#ff5555",
|
|
|
|
|
|
ts: new Date().toLocaleTimeString()
|
|
|
|
|
|
});
|
2019-11-02 05:32:21 +00:00
|
|
|
|
this.$toast.error("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("Something went wrong!", {
|
|
|
|
|
|
icon: "error"
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
disconnect() {
|
|
|
|
|
|
if (this.socket != null) this.socket.close();
|
|
|
|
|
|
},
|
|
|
|
|
|
handleError(error) {
|
|
|
|
|
|
this.disconnect();
|
|
|
|
|
|
this.connectionState = false;
|
|
|
|
|
|
this.communication.log.push({
|
|
|
|
|
|
payload: `An error has occurred.`,
|
|
|
|
|
|
source: "info",
|
|
|
|
|
|
color: "#ff5555",
|
|
|
|
|
|
ts: new Date().toLocaleTimeString()
|
|
|
|
|
|
});
|
|
|
|
|
|
if (error != null)
|
2019-08-24 23:42:41 +00:00
|
|
|
|
this.communication.log.push({
|
2019-11-02 05:32:21 +00:00
|
|
|
|
payload: error,
|
|
|
|
|
|
source: "info",
|
|
|
|
|
|
color: "#ff5555",
|
2019-10-25 08:14:34 +00:00
|
|
|
|
ts: new Date().toLocaleTimeString()
|
2019-08-24 23:42:41 +00:00
|
|
|
|
});
|
2019-08-28 23:20:20 +00:00
|
|
|
|
},
|
2019-11-02 05:32:21 +00:00
|
|
|
|
sendMessage() {
|
|
|
|
|
|
const message = this.communication.input;
|
|
|
|
|
|
this.socket.send(message);
|
|
|
|
|
|
this.communication.log.push({
|
|
|
|
|
|
payload: message,
|
|
|
|
|
|
source: "client",
|
|
|
|
|
|
ts: new Date().toLocaleTimeString()
|
2019-10-25 08:14:34 +00:00
|
|
|
|
});
|
2019-11-02 05:32:21 +00:00
|
|
|
|
this.communication.input = "";
|
|
|
|
|
|
},
|
|
|
|
|
|
collapse({ target }) {
|
|
|
|
|
|
const el = target.parentNode.className;
|
|
|
|
|
|
document.getElementsByClassName(el)[0].classList.toggle("hidden");
|
|
|
|
|
|
},
|
|
|
|
|
|
getSourcePrefix(source) {
|
|
|
|
|
|
const sourceEmojis = {
|
|
|
|
|
|
// Source used for info messages.
|
|
|
|
|
|
info: "\tℹ️ [INFO]:\t",
|
|
|
|
|
|
// Source used for client to server messages.
|
|
|
|
|
|
client: "\t👽 [SENT]:\t",
|
|
|
|
|
|
// Source used for server to client messages.
|
|
|
|
|
|
server: "\t📥 [RECEIVED]:\t"
|
|
|
|
|
|
};
|
|
|
|
|
|
if (Object.keys(sourceEmojis).includes(source))
|
|
|
|
|
|
return sourceEmojis[source];
|
|
|
|
|
|
return "";
|
2019-08-24 23:42:41 +00:00
|
|
|
|
}
|
2019-11-02 05:32:21 +00:00
|
|
|
|
},
|
|
|
|
|
|
updated: function() {
|
|
|
|
|
|
this.$nextTick(function() {
|
|
|
|
|
|
var divLog = document.getElementById("log");
|
|
|
|
|
|
divLog.scrollBy(0, divLog.scrollHeight + 100);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2019-08-24 14:51:03 +00:00
|
|
|
|
</script>
|