2019-08-24 14:51:03 +00:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="page">
|
2019-08-24 23:42:41 +00:00
|
|
|
|
<pw-section class="blue" label="Request" ref="request">
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>
|
|
|
|
|
|
<label for="url">URL</label>
|
2019-08-29 22:58:10 +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-08-26 09:07:02 +00:00
|
|
|
|
<li>
|
2019-08-28 04:32:40 +00:00
|
|
|
|
<label for="action" class="hide-on-small-screen"> </label>
|
2019-08-29 22:58:10 +00:00
|
|
|
|
<button :disabled="!urlValid" name="action" @click="toggleConnection">{{ toggleConnectionVerb }}</button>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</pw-section>
|
|
|
|
|
|
<pw-section class="purple" label="Communication" id="response" ref="response">
|
|
|
|
|
|
<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-08-28 23:20:20 +00:00
|
|
|
|
<span v-for="logEntry in communication.log" :style="{ color: logEntry.color }">@ {{ logEntry.ts }} {{ getSourcePrefix(logEntry.source) }} {{ logEntry.payload }}</span>
|
2019-08-27 11:52:24 +00:00
|
|
|
|
</span>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
<span v-else>(Waiting for connection...)</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>
|
2019-08-24 23:50:23 +00:00
|
|
|
|
<label for="message">Message</label>
|
2019-08-29 22:58:10 +00:00
|
|
|
|
<input id="message" name="message" type="text" v-model="communication.input" :disabled="!connectionState" @keyup.enter="connectionState ? sendMessage() : null">
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</li>
|
2019-08-26 09:07:02 +00:00
|
|
|
|
<li>
|
2019-08-28 04:32:40 +00:00
|
|
|
|
<label for="send" class="hide-on-small-screen"> </label>
|
2019-08-29 22:58:10 +00:00
|
|
|
|
<button name="send" :disabled="!connectionState" @click="sendMessage">Send</button>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</pw-section>
|
2019-08-24 14:51:03 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<style lang="scss">
|
2019-08-24 23:50:23 +00:00
|
|
|
|
div.log {
|
2019-08-24 14:51:03 +00:00
|
|
|
|
margin: 4px;
|
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
|
width: calc(100% - 8px);
|
|
|
|
|
|
border-radius: 4px;
|
2019-08-24 23:42:41 +00:00
|
|
|
|
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-weight: 700;
|
|
|
|
|
|
font-size: 18px;
|
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
span {
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-27 11:52:24 +00:00
|
|
|
|
</style>
|
2019-08-24 14:51:03 +00:00
|
|
|
|
<script>
|
2019-08-24 23:42:41 +00:00
|
|
|
|
import section from "../components/section";
|
2019-08-24 14:51:03 +00:00
|
|
|
|
export default {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
components: {
|
|
|
|
|
|
'pw-section': section
|
|
|
|
|
|
},
|
2019-08-27 11:52:24 +00:00
|
|
|
|
data() {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
return {
|
|
|
|
|
|
connectionState: false,
|
|
|
|
|
|
url: "wss://echo.websocket.org",
|
|
|
|
|
|
socket: null,
|
|
|
|
|
|
communication: {
|
|
|
|
|
|
log: null,
|
|
|
|
|
|
input: ""
|
2019-08-24 14:51:03 +00:00
|
|
|
|
}
|
2019-08-24 23:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
computed: {
|
2019-08-27 11:52:24 +00:00
|
|
|
|
toggleConnectionVerb() {
|
|
|
|
|
|
return !this.connectionState ? "Connect" : "Disconnect";
|
2019-08-24 23:42:41 +00:00
|
|
|
|
},
|
2019-08-27 11:52:24 +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}))' +
|
|
|
|
|
|
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
|
|
|
|
|
|
'(\\?[;&a-z\\d%_.~+=-]*)?' +
|
|
|
|
|
|
'(\\#[-a-z\\d_]*)?$', 'i');
|
|
|
|
|
|
return pattern.test(this.url);
|
2019-08-24 23:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
2019-08-27 11:52:24 +00:00
|
|
|
|
toggleConnection() {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
// If it is connecting:
|
2019-08-27 11:52:24 +00:00
|
|
|
|
if (!this.connectionState) return this.connect();
|
2019-08-24 23:42:41 +00:00
|
|
|
|
// 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() {
|
|
|
|
|
|
this.communication.log = [{
|
|
|
|
|
|
payload: `Connecting to ${this.url}...`,
|
|
|
|
|
|
source: 'info',
|
|
|
|
|
|
color: 'lime'
|
|
|
|
|
|
}];
|
2019-08-24 23:42:41 +00:00
|
|
|
|
try {
|
2019-08-27 11:52:24 +00:00
|
|
|
|
this.socket = new WebSocket(this.url);
|
|
|
|
|
|
this.socket.onopen = (event) => {
|
|
|
|
|
|
this.connectionState = true;
|
|
|
|
|
|
this.communication.log = [{
|
|
|
|
|
|
payload: `Connected to ${this.url}.`,
|
2019-08-24 23:42:41 +00:00
|
|
|
|
source: 'info',
|
2019-08-28 23:20:20 +00:00
|
|
|
|
color: 'lime',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-27 11:52:24 +00:00
|
|
|
|
}];
|
|
|
|
|
|
};
|
|
|
|
|
|
this.socket.onerror = (event) => {
|
|
|
|
|
|
this.handleError();
|
|
|
|
|
|
};
|
|
|
|
|
|
this.socket.onclose = (event) => {
|
|
|
|
|
|
this.connectionState = false;
|
|
|
|
|
|
this.communication.log.push({
|
|
|
|
|
|
payload: `Disconnected from ${this.url}.`,
|
2019-08-24 23:42:41 +00:00
|
|
|
|
source: 'info',
|
2019-08-28 23:20:20 +00:00
|
|
|
|
color: 'red',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-27 11:52:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
this.socket.onmessage = (event) => {
|
|
|
|
|
|
this.communication.log.push({
|
|
|
|
|
|
payload: event.data,
|
2019-08-28 23:20:20 +00:00
|
|
|
|
source: 'server',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-27 11:52:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
|
this.handleError(ex);
|
|
|
|
|
|
}
|
2019-08-24 23:42:41 +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.`,
|
|
|
|
|
|
source: 'info',
|
2019-08-28 23:20:20 +00:00
|
|
|
|
color: 'red',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-27 11:52:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
if (error != null) this.communication.log.push({
|
|
|
|
|
|
payload: error,
|
|
|
|
|
|
source: 'info',
|
2019-08-28 23:20:20 +00:00
|
|
|
|
color: 'red',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-27 11:52:24 +00:00
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
sendMessage() {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
const message = this.communication.input;
|
|
|
|
|
|
this.socket.send(message);
|
|
|
|
|
|
this.communication.log.push({
|
2019-08-27 11:52:24 +00:00
|
|
|
|
payload: message,
|
2019-08-28 23:20:20 +00:00
|
|
|
|
source: 'client',
|
|
|
|
|
|
ts: (new Date()).toLocaleTimeString()
|
2019-08-24 23:42:41 +00:00
|
|
|
|
});
|
|
|
|
|
|
this.communication.input = "";
|
|
|
|
|
|
},
|
2019-08-27 11:52:24 +00:00
|
|
|
|
collapse({
|
|
|
|
|
|
target
|
|
|
|
|
|
}) {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
const el = target.parentNode.className;
|
|
|
|
|
|
document.getElementsByClassName(el)[0].classList.toggle('hidden');
|
|
|
|
|
|
},
|
2019-08-27 11:52:24 +00:00
|
|
|
|
getSourcePrefix(source) {
|
2019-08-24 23:42:41 +00:00
|
|
|
|
const sourceEmojis = {
|
|
|
|
|
|
// Source used for info messages.
|
2019-08-28 23:20:20 +00:00
|
|
|
|
'info': '\tℹ️ [INFO]:\t',
|
2019-08-24 23:42:41 +00:00
|
|
|
|
// Source used for client to server messages.
|
2019-08-28 23:20:20 +00:00
|
|
|
|
'client': '\t👽 [SENT]:\t',
|
2019-08-24 23:42:41 +00:00
|
|
|
|
// Source used for server to client messages.
|
2019-08-28 23:20:20 +00:00
|
|
|
|
'server': '\t📥 [RECEIVED]:\t'
|
2019-08-24 23:42:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
if (Object.keys(sourceEmojis).includes(source)) return sourceEmojis[source];
|
|
|
|
|
|
return '';
|
2019-08-24 14:51:03 +00:00
|
|
|
|
}
|
2019-08-28 23:20:20 +00:00
|
|
|
|
},
|
|
|
|
|
|
updated: function () {
|
|
|
|
|
|
this.$nextTick(function () {
|
|
|
|
|
|
var divLog = document.getElementById("log")
|
|
|
|
|
|
divLog.scrollBy(0, divLog.scrollHeight + 100)
|
|
|
|
|
|
})
|
2019-08-24 23:42:41 +00:00
|
|
|
|
}
|
2019-08-24 14:51:03 +00:00
|
|
|
|
}
|
2019-08-27 11:52:24 +00:00
|
|
|
|
|
2019-08-24 14:51:03 +00:00
|
|
|
|
</script>
|