Allow multiple arguments with socket.io (#1234)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
Léo Martin
2020-10-05 01:41:56 +02:00
committed by GitHub
parent 881d7fa5ee
commit 0f31259c97

View File

@@ -54,19 +54,35 @@
</ul> </ul>
<ul> <ul>
<li> <li>
<label for="socketio-message">{{ $t("message") }}</label> <div class="row-wrapper">
<label>{{ $t("message") }}s</label>
</div>
</li>
</ul>
<ul v-for="(input, index) of communication.inputs" :key="`input-${index}`">
<li>
<input <input
id="socketio-message"
name="message" name="message"
type="text" type="text"
v-model="communication.input" v-model="communication.inputs[index]"
:readonly="!connectionState" :readonly="!connectionState"
@keyup.enter="connectionState ? sendMessage() : null" @keyup.enter="connectionState ? sendMessage() : null"
/> />
</li> </li>
<div> <div v-if="index + 1 !== communication.inputs.length">
<li>
<button
class="icon"
@click="removeCommunicationInput({ index })"
v-tooltip.bottom="$t('delete')"
:id="`delete-socketio-message-${index}`"
>
<deleteIcon class="material-icons" />
</button>
</li>
</div>
<div v-if="index + 1 === communication.inputs.length">
<li> <li>
<label for="send" class="hide-on-small-screen">&nbsp;</label>
<button id="send" name="send" :disabled="!connectionState" @click="sendMessage"> <button id="send" name="send" :disabled="!connectionState" @click="sendMessage">
{{ $t("send") }} {{ $t("send") }}
<span> <span>
@@ -76,6 +92,14 @@
</li> </li>
</div> </div>
</ul> </ul>
<ul>
<li>
<button class="icon" @click="addCommunicationInput">
<i class="material-icons">add</i>
<span>{{ $t("add_new") }}</span>
</button>
</li>
</ul>
</pw-section> </pw-section>
</div> </div>
</template> </template>
@@ -84,8 +108,12 @@
import { socketioValid } from "~/helpers/utils/valid" import { socketioValid } from "~/helpers/utils/valid"
import io from "socket.io-client" import io from "socket.io-client"
import wildcard from "socketio-wildcard" import wildcard from "socketio-wildcard"
import deleteIcon from "~/static/icons/delete-24px.svg?inline"
export default { export default {
components: {
deleteIcon,
},
data() { data() {
return { return {
url: "ws://", url: "ws://",
@@ -95,7 +123,7 @@ export default {
communication: { communication: {
log: null, log: null,
eventName: "", eventName: "",
input: "", inputs: [""],
}, },
} }
}, },
@@ -105,6 +133,12 @@ export default {
}, },
}, },
methods: { methods: {
removeCommunicationInput({ index }) {
this.$delete(this.communication.inputs, index)
},
addCommunicationInput() {
this.communication.inputs.push("")
},
toggleConnection() { toggleConnection() {
// If it is connecting: // If it is connecting:
if (!this.connectionState) return this.connect() if (!this.connectionState) return this.connect()
@@ -201,18 +235,18 @@ export default {
}, },
sendMessage() { sendMessage() {
const eventName = this.communication.eventName const eventName = this.communication.eventName
let message const messages = (this.communication.inputs || [])
.map((input) => {
try { try {
message = JSON.parse(this.communication.input) return JSON.parse(input)
} catch (err) { } catch (err) {
message = this.communication.input return input
} }
})
.filter((message) => !!message)
if (this.io) { if (this.io) {
// TODO: support only one argument now this.io.emit(eventName, ...messages, (data) => {
// maybe should support more argument
this.io.emit(eventName, message, (data) => {
// receive response from server // receive response from server
this.communication.log.push({ this.communication.log.push({
payload: `[${eventName}] ${JSON.stringify(data)}`, payload: `[${eventName}] ${JSON.stringify(data)}`,
@@ -222,11 +256,11 @@ export default {
}) })
this.communication.log.push({ this.communication.log.push({
payload: `[${eventName}] ${JSON.stringify(message)}`, payload: `[${eventName}] ${JSON.stringify(messages)}`,
source: "client", source: "client",
ts: new Date().toLocaleTimeString(), ts: new Date().toLocaleTimeString(),
}) })
this.communication.input = "" this.communication.inputs = [""]
} }
}, },
}, },