123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <template>
- <div class="page">
- <pw-section class="blue" :label="$t('request')" ref="request">
- <ul>
- <li>
- <label for="websocket-url">{{ $t("url") }}</label>
- <input
- id="websocket-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"> </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>
- <log :title="$t('log')" :log="communication.log" />
- </li>
- </ul>
- <ul>
- <li>
- <label for="websocket-message">{{ $t("message") }}</label>
- <input
- id="websocket-message"
- name="message"
- type="text"
- v-model="communication.input"
- :readonly="!connectionState"
- @keyup.enter="connectionState ? sendMessage() : null"
- @keyup.up="connectionState ? walkHistory('up') : null"
- @keyup.down="connectionState ? walkHistory('down') : null"
- />
- </li>
- <div>
- <li>
- <label for="send" class="hide-on-small-screen"> </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 "~/helpers/utils/valid"
- export default {
- data() {
- return {
- connectionState: false,
- url: "wss://echo.websocket.org",
- socket: null,
- communication: {
- log: null,
- input: "",
- },
- currentIndex: -1, //index of the message log array to put in input box
- }
- },
- 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 = ({ data }) => {
- this.communication.log.push({
- payload: 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 = ""
- },
- walkHistory(direction) {
- const clientMessages = this.communication.log.filter(({ source }) => source === "client")
- const length = clientMessages.length
- switch (direction) {
- case "up":
- if (length > 0 && this.currentIndex !== 0) {
- //does nothing if message log is empty or the currentIndex is 0 when up arrow is pressed
- if (this.currentIndex === -1) {
- this.currentIndex = length - 1
- this.communication.input = clientMessages[this.currentIndex].payload
- } else if (this.currentIndex === 0) {
- this.communication.input = clientMessages[0].payload
- } else if (this.currentIndex > 0) {
- this.currentIndex = this.currentIndex - 1
- this.communication.input = clientMessages[this.currentIndex].payload
- }
- }
- break
- case "down":
- if (length > 0 && this.currentIndex > -1) {
- if (this.currentIndex === length - 1) {
- this.currentIndex = -1
- this.communication.input = ""
- } else if (this.currentIndex < length - 1) {
- this.currentIndex = this.currentIndex + 1
- this.communication.input = clientMessages[this.currentIndex].payload
- }
- }
- break
- }
- },
- },
- }
- </script>
|