websocket.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <div class="page">
  3. <pw-section class="blue" :label="$t('request')" ref="request">
  4. <ul>
  5. <li>
  6. <label for="websocket-url">{{ $t("url") }}</label>
  7. <input
  8. id="websocket-url"
  9. type="url"
  10. spellcheck="false"
  11. :class="{ error: !urlValid }"
  12. v-model="url"
  13. @keyup.enter="urlValid ? toggleConnection() : null"
  14. />
  15. </li>
  16. <div>
  17. <li>
  18. <label for="connect" class="hide-on-small-screen">&nbsp;</label>
  19. <button :disabled="!urlValid" id="connect" name="connect" @click="toggleConnection">
  20. {{ !connectionState ? $t("connect") : $t("disconnect") }}
  21. <span>
  22. <i class="material-icons">
  23. {{ !connectionState ? "sync" : "sync_disabled" }}
  24. </i>
  25. </span>
  26. </button>
  27. </li>
  28. </div>
  29. </ul>
  30. </pw-section>
  31. <pw-section class="purple" :label="$t('communication')" id="response" ref="response">
  32. <ul>
  33. <li>
  34. <log :title="$t('log')" :log="communication.log" />
  35. </li>
  36. </ul>
  37. <ul>
  38. <li>
  39. <label for="websocket-message">{{ $t("message") }}</label>
  40. <input
  41. id="websocket-message"
  42. name="message"
  43. type="text"
  44. v-model="communication.input"
  45. :readonly="!connectionState"
  46. @keyup.enter="connectionState ? sendMessage() : null"
  47. @keyup.up="connectionState ? walkHistory('up') : null"
  48. @keyup.down="connectionState ? walkHistory('down') : null"
  49. />
  50. </li>
  51. <div>
  52. <li>
  53. <label for="send" class="hide-on-small-screen">&nbsp;</label>
  54. <button id="send" name="send" :disabled="!connectionState" @click="sendMessage">
  55. {{ $t("send") }}
  56. <span>
  57. <i class="material-icons">send</i>
  58. </span>
  59. </button>
  60. </li>
  61. </div>
  62. </ul>
  63. </pw-section>
  64. </div>
  65. </template>
  66. <script>
  67. import { wsValid } from "~/helpers/utils/valid"
  68. export default {
  69. data() {
  70. return {
  71. connectionState: false,
  72. url: "wss://echo.websocket.org",
  73. socket: null,
  74. communication: {
  75. log: null,
  76. input: "",
  77. },
  78. currentIndex: -1, //index of the message log array to put in input box
  79. }
  80. },
  81. computed: {
  82. urlValid() {
  83. return wsValid(this.url)
  84. },
  85. },
  86. methods: {
  87. toggleConnection() {
  88. // If it is connecting:
  89. if (!this.connectionState) return this.connect()
  90. // Otherwise, it's disconnecting.
  91. else return this.disconnect()
  92. },
  93. connect() {
  94. this.communication.log = [
  95. {
  96. payload: this.$t("connecting_to", { name: this.url }),
  97. source: "info",
  98. color: "var(--ac-color)",
  99. },
  100. ]
  101. try {
  102. this.socket = new WebSocket(this.url)
  103. this.socket.onopen = (event) => {
  104. this.connectionState = true
  105. this.communication.log = [
  106. {
  107. payload: this.$t("connected_to", { name: this.url }),
  108. source: "info",
  109. color: "var(--ac-color)",
  110. ts: new Date().toLocaleTimeString(),
  111. },
  112. ]
  113. this.$toast.success(this.$t("connected"), {
  114. icon: "sync",
  115. })
  116. }
  117. this.socket.onerror = (event) => {
  118. this.handleError()
  119. }
  120. this.socket.onclose = (event) => {
  121. this.connectionState = false
  122. this.communication.log.push({
  123. payload: this.$t("disconnected_from", { name: this.url }),
  124. source: "info",
  125. color: "#ff5555",
  126. ts: new Date().toLocaleTimeString(),
  127. })
  128. this.$toast.error(this.$t("disconnected"), {
  129. icon: "sync_disabled",
  130. })
  131. }
  132. this.socket.onmessage = ({ data }) => {
  133. this.communication.log.push({
  134. payload: data,
  135. source: "server",
  136. ts: new Date().toLocaleTimeString(),
  137. })
  138. }
  139. } catch (ex) {
  140. this.handleError(ex)
  141. this.$toast.error(this.$t("something_went_wrong"), {
  142. icon: "error",
  143. })
  144. }
  145. },
  146. disconnect() {
  147. this.socket.close()
  148. },
  149. handleError(error) {
  150. this.disconnect()
  151. this.connectionState = false
  152. this.communication.log.push({
  153. payload: this.$t("error_occurred"),
  154. source: "info",
  155. color: "#ff5555",
  156. ts: new Date().toLocaleTimeString(),
  157. })
  158. if (error !== null)
  159. this.communication.log.push({
  160. payload: error,
  161. source: "info",
  162. color: "#ff5555",
  163. ts: new Date().toLocaleTimeString(),
  164. })
  165. },
  166. sendMessage() {
  167. const message = this.communication.input
  168. this.socket.send(message)
  169. this.communication.log.push({
  170. payload: message,
  171. source: "client",
  172. ts: new Date().toLocaleTimeString(),
  173. })
  174. this.communication.input = ""
  175. },
  176. walkHistory(direction) {
  177. const clientMessages = this.communication.log.filter(({ source }) => source === "client")
  178. const length = clientMessages.length
  179. switch (direction) {
  180. case "up":
  181. if (length > 0 && this.currentIndex !== 0) {
  182. //does nothing if message log is empty or the currentIndex is 0 when up arrow is pressed
  183. if (this.currentIndex === -1) {
  184. this.currentIndex = length - 1
  185. this.communication.input = clientMessages[this.currentIndex].payload
  186. } else if (this.currentIndex === 0) {
  187. this.communication.input = clientMessages[0].payload
  188. } else if (this.currentIndex > 0) {
  189. this.currentIndex = this.currentIndex - 1
  190. this.communication.input = clientMessages[this.currentIndex].payload
  191. }
  192. }
  193. break
  194. case "down":
  195. if (length > 0 && this.currentIndex > -1) {
  196. if (this.currentIndex === length - 1) {
  197. this.currentIndex = -1
  198. this.communication.input = ""
  199. } else if (this.currentIndex < length - 1) {
  200. this.currentIndex = this.currentIndex + 1
  201. this.communication.input = clientMessages[this.currentIndex].payload
  202. }
  203. }
  204. break
  205. }
  206. },
  207. },
  208. }
  209. </script>