Socketio.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <template>
  2. <Splitpanes
  3. class="smart-splitter"
  4. :dbl-click-splitter="false"
  5. :horizontal="!(windowInnerWidth.x.value >= 768)"
  6. >
  7. <Pane class="hide-scrollbar !overflow-auto">
  8. <Splitpanes class="smart-splitter" :dbl-click-splitter="false" horizontal>
  9. <Pane class="hide-scrollbar !overflow-auto">
  10. <AppSection label="request">
  11. <div class="bg-primary flex p-4 top-0 z-10 sticky">
  12. <div class="space-x-2 flex-1 inline-flex">
  13. <div class="flex flex-1">
  14. <input
  15. id="socketio-url"
  16. v-model="url"
  17. v-focus
  18. type="url"
  19. autocomplete="off"
  20. spellcheck="false"
  21. :class="{ error: !urlValid }"
  22. class="
  23. bg-primaryLight
  24. border border-divider
  25. rounded-l
  26. flex flex-1
  27. text-secondaryDark
  28. w-full
  29. py-2
  30. px-4
  31. hover:border-dividerDark
  32. focus-visible:bg-transparent
  33. focus-visible:border-dividerDark
  34. "
  35. :placeholder="$t('socketio.url')"
  36. @keyup.enter="urlValid ? toggleConnection() : null"
  37. />
  38. <input
  39. id="socketio-path"
  40. v-model="path"
  41. class="
  42. bg-primaryLight
  43. border border-divider
  44. rounded-r
  45. flex flex-1
  46. text-secondaryDark
  47. w-full
  48. py-2
  49. px-4
  50. hover:border-dividerDark
  51. focus-visible:bg-transparent
  52. focus-visible:border-dividerDark
  53. "
  54. spellcheck="false"
  55. />
  56. </div>
  57. <ButtonPrimary
  58. id="connect"
  59. :disabled="!urlValid"
  60. name="connect"
  61. class="w-32"
  62. :label="
  63. !connectionState
  64. ? $t('action.connect')
  65. : $t('action.disconnect')
  66. "
  67. :loading="connectingState"
  68. @click.native="toggleConnection"
  69. />
  70. </div>
  71. </div>
  72. </AppSection>
  73. </Pane>
  74. <Pane class="hide-scrollbar !overflow-auto">
  75. <AppSection label="response">
  76. <RealtimeLog :title="$t('socketio.log')" :log="communication.log" />
  77. </AppSection>
  78. </Pane>
  79. </Splitpanes>
  80. </Pane>
  81. <Pane
  82. v-if="RIGHT_SIDEBAR"
  83. max-size="35"
  84. size="25"
  85. min-size="20"
  86. class="hide-scrollbar !overflow-auto"
  87. >
  88. <AppSection label="messages">
  89. <div class="flex flex-col flex-1 p-4 inline-flex">
  90. <label for="events" class="font-semibold text-secondaryLight">
  91. {{ $t("socketio.events") }}
  92. </label>
  93. </div>
  94. <div class="flex px-4">
  95. <input
  96. id="event_name"
  97. v-model="communication.eventName"
  98. class="input"
  99. name="event_name"
  100. :placeholder="$t('socketio.event_name')"
  101. type="text"
  102. autocomplete="off"
  103. :disabled="!connectionState"
  104. />
  105. </div>
  106. <div class="flex flex-1 p-4 items-center justify-between">
  107. <label class="font-semibold text-secondaryLight">
  108. {{ $t("socketio.communication") }}
  109. </label>
  110. <div class="flex">
  111. <ButtonSecondary
  112. v-tippy="{ theme: 'tooltip' }"
  113. :title="$t('add.new')"
  114. svg="plus"
  115. class="rounded"
  116. @click.native="addCommunicationInput"
  117. />
  118. </div>
  119. </div>
  120. <div class="flex flex-col space-y-2 px-4">
  121. <div
  122. v-for="(input, index) of communication.inputs"
  123. :key="`input-${index}`"
  124. >
  125. <div class="flex space-x-2">
  126. <input
  127. v-model="communication.inputs[index]"
  128. class="input"
  129. name="message"
  130. :placeholder="$t('count.message', { count: index + 1 })"
  131. type="text"
  132. autocomplete="off"
  133. :disabled="!connectionState"
  134. @keyup.enter="connectionState ? sendMessage() : null"
  135. />
  136. <ButtonSecondary
  137. v-if="index + 1 !== communication.inputs.length"
  138. v-tippy="{ theme: 'tooltip' }"
  139. :title="$t('action.remove')"
  140. svg="trash"
  141. class="rounded"
  142. color="red"
  143. outline
  144. @click.native="removeCommunicationInput({ index })"
  145. />
  146. <ButtonPrimary
  147. v-if="index + 1 === communication.inputs.length"
  148. id="send"
  149. name="send"
  150. :disabled="!connectionState"
  151. :label="$t('action.send')"
  152. @click.native="sendMessage"
  153. />
  154. </div>
  155. </div>
  156. </div>
  157. </AppSection>
  158. </Pane>
  159. </Splitpanes>
  160. </template>
  161. <script>
  162. import { defineComponent } from "@nuxtjs/composition-api"
  163. import { Splitpanes, Pane } from "splitpanes"
  164. import "splitpanes/dist/splitpanes.css"
  165. import { io as Client } from "socket.io-client"
  166. import wildcard from "socketio-wildcard"
  167. import debounce from "~/helpers/utils/debounce"
  168. import { logHoppRequestRunToAnalytics } from "~/helpers/fb/analytics"
  169. import { useSetting } from "~/newstore/settings"
  170. import useWindowSize from "~/helpers/utils/useWindowSize"
  171. export default defineComponent({
  172. components: { Splitpanes, Pane },
  173. setup() {
  174. return {
  175. windowInnerWidth: useWindowSize(),
  176. RIGHT_SIDEBAR: useSetting("RIGHT_SIDEBAR"),
  177. }
  178. },
  179. data() {
  180. return {
  181. url: "wss://main-daxrc78qyb411dls-gtw.qovery.io",
  182. path: "/socket.io",
  183. isUrlValid: true,
  184. connectingState: false,
  185. connectionState: false,
  186. io: null,
  187. communication: {
  188. log: null,
  189. eventName: "",
  190. inputs: [""],
  191. },
  192. }
  193. },
  194. computed: {
  195. urlValid() {
  196. return this.isUrlValid
  197. },
  198. },
  199. watch: {
  200. url() {
  201. this.debouncer()
  202. },
  203. },
  204. mounted() {
  205. if (process.browser) {
  206. this.worker = this.$worker.createRejexWorker()
  207. this.worker.addEventListener("message", this.workerResponseHandler)
  208. }
  209. },
  210. destroyed() {
  211. this.worker.terminate()
  212. },
  213. methods: {
  214. debouncer: debounce(function () {
  215. this.worker.postMessage({ type: "socketio", url: this.url })
  216. }, 1000),
  217. workerResponseHandler({ data }) {
  218. if (data.url === this.url) this.isUrlValid = data.result
  219. },
  220. removeCommunicationInput({ index }) {
  221. this.$delete(this.communication.inputs, index)
  222. },
  223. addCommunicationInput() {
  224. this.communication.inputs.push("")
  225. },
  226. toggleConnection() {
  227. // If it is connecting:
  228. if (!this.connectionState) return this.connect()
  229. // Otherwise, it's disconnecting.
  230. else return this.disconnect()
  231. },
  232. connect() {
  233. this.connectingState = true
  234. this.communication.log = [
  235. {
  236. payload: this.$t("state.connecting_to", { name: this.url }),
  237. source: "info",
  238. color: "var(--accent-color)",
  239. },
  240. ]
  241. try {
  242. if (!this.path) {
  243. this.path = "/socket.io"
  244. }
  245. this.io = new Client(this.url, {
  246. path: this.path,
  247. })
  248. // Add ability to listen to all events
  249. wildcard(Client.Manager)(this.io)
  250. this.io.on("connect", () => {
  251. this.connectingState = false
  252. this.connectionState = true
  253. this.communication.log = [
  254. {
  255. payload: this.$t("state.connected_to", { name: this.url }),
  256. source: "info",
  257. color: "var(--accent-color)",
  258. ts: new Date().toLocaleTimeString(),
  259. },
  260. ]
  261. this.$toast.success(this.$t("state.connected"), {
  262. icon: "sync",
  263. })
  264. })
  265. this.io.on("*", ({ data }) => {
  266. const [eventName, message] = data
  267. this.communication.log.push({
  268. payload: `[${eventName}] ${message ? JSON.stringify(message) : ""}`,
  269. source: "server",
  270. ts: new Date().toLocaleTimeString(),
  271. })
  272. })
  273. this.io.on("connect_error", (error) => {
  274. this.handleError(error)
  275. })
  276. this.io.on("reconnect_error", (error) => {
  277. this.handleError(error)
  278. })
  279. this.io.on("error", () => {
  280. this.handleError()
  281. })
  282. this.io.on("disconnect", () => {
  283. this.connectingState = false
  284. this.connectionState = false
  285. this.communication.log.push({
  286. payload: this.$t("state.disconnected_from", { name: this.url }),
  287. source: "info",
  288. color: "#ff5555",
  289. ts: new Date().toLocaleTimeString(),
  290. })
  291. this.$toast.error(this.$t("state.disconnected"), {
  292. icon: "sync_disabled",
  293. })
  294. })
  295. } catch (e) {
  296. this.handleError(e)
  297. this.$toast.error(this.$t("error.something_went_wrong"), {
  298. icon: "error_outline",
  299. })
  300. }
  301. logHoppRequestRunToAnalytics({
  302. platform: "socketio",
  303. })
  304. },
  305. disconnect() {
  306. this.io.close()
  307. },
  308. handleError(error) {
  309. this.disconnect()
  310. this.connectingState = false
  311. this.connectionState = false
  312. this.communication.log.push({
  313. payload: this.$t("error.something_went_wrong"),
  314. source: "info",
  315. color: "#ff5555",
  316. ts: new Date().toLocaleTimeString(),
  317. })
  318. if (error !== null)
  319. this.communication.log.push({
  320. payload: error,
  321. source: "info",
  322. color: "#ff5555",
  323. ts: new Date().toLocaleTimeString(),
  324. })
  325. },
  326. sendMessage() {
  327. const eventName = this.communication.eventName
  328. const messages = (this.communication.inputs || [])
  329. .map((input) => {
  330. try {
  331. return JSON.parse(input)
  332. } catch (e) {
  333. return input
  334. }
  335. })
  336. .filter((message) => !!message)
  337. if (this.io) {
  338. this.io.emit(eventName, ...messages, (data) => {
  339. // receive response from server
  340. this.communication.log.push({
  341. payload: `[${eventName}] ${JSON.stringify(data)}`,
  342. source: "server",
  343. ts: new Date().toLocaleTimeString(),
  344. })
  345. })
  346. this.communication.log.push({
  347. payload: `[${eventName}] ${JSON.stringify(messages)}`,
  348. source: "client",
  349. ts: new Date().toLocaleTimeString(),
  350. })
  351. this.communication.inputs = [""]
  352. }
  353. },
  354. },
  355. })
  356. </script>