From 0f31259c97844f368d84258e3e8138e6af5f5921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Martin?= Date: Mon, 5 Oct 2020 01:41:56 +0200 Subject: [PATCH] Allow multiple arguments with socket.io (#1234) Co-authored-by: Liyas Thomas --- components/realtime/socketio.vue | 70 ++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/components/realtime/socketio.vue b/components/realtime/socketio.vue index a012bc0e7..b5966c2aa 100644 --- a/components/realtime/socketio.vue +++ b/components/realtime/socketio.vue @@ -54,19 +54,35 @@ + + @@ -84,8 +108,12 @@ import { socketioValid } from "~/helpers/utils/valid" import io from "socket.io-client" import wildcard from "socketio-wildcard" +import deleteIcon from "~/static/icons/delete-24px.svg?inline" export default { + components: { + deleteIcon, + }, data() { return { url: "ws://", @@ -95,7 +123,7 @@ export default { communication: { log: null, eventName: "", - input: "", + inputs: [""], }, } }, @@ -105,6 +133,12 @@ export default { }, }, methods: { + removeCommunicationInput({ index }) { + this.$delete(this.communication.inputs, index) + }, + addCommunicationInput() { + this.communication.inputs.push("") + }, toggleConnection() { // If it is connecting: if (!this.connectionState) return this.connect() @@ -201,18 +235,18 @@ export default { }, sendMessage() { const eventName = this.communication.eventName - let message - - try { - message = JSON.parse(this.communication.input) - } catch (err) { - message = this.communication.input - } + const messages = (this.communication.inputs || []) + .map((input) => { + try { + return JSON.parse(input) + } catch (err) { + return input + } + }) + .filter((message) => !!message) if (this.io) { - // TODO: support only one argument now - // maybe should support more argument - this.io.emit(eventName, message, (data) => { + this.io.emit(eventName, ...messages, (data) => { // receive response from server this.communication.log.push({ payload: `[${eventName}] ${JSON.stringify(data)}`, @@ -222,11 +256,11 @@ export default { }) this.communication.log.push({ - payload: `[${eventName}] ${JSON.stringify(message)}`, + payload: `[${eventName}] ${JSON.stringify(messages)}`, source: "client", ts: new Date().toLocaleTimeString(), }) - this.communication.input = "" + this.communication.inputs = [""] } }, },