From 266cb83ba9d9d571a9a827e3202cba99dff937c0 Mon Sep 17 00:00:00 2001 From: Dan Fey Date: Tue, 28 Apr 2020 13:20:47 -0700 Subject: [PATCH] Trying to json parse user input so that the socket io library can properly emit json data. Falls back to string input --- components/realtime/socketio.vue | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/components/realtime/socketio.vue b/components/realtime/socketio.vue index 207980536..1f57cdb1c 100644 --- a/components/realtime/socketio.vue +++ b/components/realtime/socketio.vue @@ -144,13 +144,13 @@ export default { ts: new Date().toLocaleTimeString(), }) }) - this.io.on("connect_error", error => { + this.io.on("connect_error", (error) => { this.handleError(error) }) - this.io.on("reconnect_error", error => { + this.io.on("reconnect_error", (error) => { this.handleError(error) }) - this.io.on("error", data => { + this.io.on("error", (data) => { this.handleError() }) this.io.on("disconnect", () => { @@ -194,12 +194,18 @@ export default { }, sendMessage() { const eventName = this.communication.eventName - const message = this.communication.input + let message + + try { + message = JSON.parse(this.communication.input) + } catch (err) { + message = this.communication.input + } if (this.io) { // TODO: support only one argument now // maybe should support more argument - this.io.emit(eventName, message, data => { + this.io.emit(eventName, message, (data) => { // receive response from server this.communication.log.push({ payload: `[${eventName}] ${JSON.stringify(data)}`, @@ -209,7 +215,7 @@ export default { }) this.communication.log.push({ - payload: `[${eventName}] ${message}`, + payload: `[${eventName}] ${JSON.stringify(message)}`, source: "client", ts: new Date().toLocaleTimeString(), })