api-client/pages/websocket.vue

200 lines
5.9 KiB
Vue
Raw Normal View History

2019-08-24 14:51:03 +00:00
<template>
<div class="page">
<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">
</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">&nbsp;</label>
2019-08-29 22:58:10 +00:00
<button :disabled="!urlValid" name="action" @click="toggleConnection">{{ toggleConnectionVerb }}</button>
</li>
</ul>
</pw-section>
<pw-section class="purple" 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">
<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>
<span v-else>(Waiting for connection...)</span>
</div>
</li>
</ul>
<ul>
<li>
<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">
</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">&nbsp;</label>
2019-08-29 22:58:10 +00:00
<button name="send" :disabled="!connectionState" @click="sendMessage">Send</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);
border-radius: 4px;
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>
import section from "../components/section";
2019-08-24 14:51:03 +00:00
export default {
components: {
'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
}
}
},
computed: {
2019-08-27 11:52:24 +00:00
toggleConnectionVerb() {
return !this.connectionState ? "Connect" : "Disconnect";
},
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);
}
},
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() {
this.communication.log = [{
payload: `Connecting to ${this.url}...`,
source: 'info',
color: 'lime'
}];
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}.`,
source: 'info',
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}.`,
source: 'info',
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,
source: 'server',
ts: (new Date()).toLocaleTimeString()
2019-08-27 11:52:24 +00:00
});
}
} catch (ex) {
this.handleError(ex);
}
},
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',
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',
color: 'red',
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,
source: 'client',
ts: (new Date()).toLocaleTimeString()
});
this.communication.input = "";
},
2019-08-27 11:52:24 +00:00
collapse({
target
}) {
const el = target.parentNode.className;
document.getElementsByClassName(el)[0].classList.toggle('hidden');
},
2019-08-27 11:52:24 +00:00
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 14:51:03 +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
}
2019-08-27 11:52:24 +00:00
2019-08-24 14:51:03 +00:00
</script>