Parcourir la source

Removed client websocket, added experimentCollect instead

rigwild il y a 4 ans
Parent
commit
efcfc8da66
8 fichiers modifiés avec 32 ajouts et 100 suppressions
  1. 0 1
      package.json
  2. 6 9
      src/App.vue
  3. 4 4
      src/functions.js
  4. 20 38
      src/store/actions.js
  5. 1 7
      src/store/getters.js
  6. 0 30
      src/store/mutations.js
  7. 1 6
      src/store/state.js
  8. 0 5
      yarn.lock

+ 0 - 1
package.json

@@ -47,7 +47,6 @@
     "supertest": "^4.0.2",
     "vue": "^2.6.10",
     "vue-cli-plugin-vuetify": "^0.5.0",
-    "vue-native-websocket": "^2.0.13",
     "vue-router": "^3.0.6",
     "vue-template-compiler": "^2.6.10",
     "vuetify": "^1.5.14",

+ 6 - 9
src/App.vue

@@ -95,16 +95,19 @@ export default {
     this.APP_LOADER()
   },
   methods: {
-    ...mapActions(['loadScenesList', 'connectToWs']),
+    ...mapActions(['loadScenesList']),
 
     // Main app function that redirect the user where he needs to be at
     async APP_LOADER() {
       if (this.isGdprValidated && this.isHostConfigured) {
-        await this.loadWebSocket()
         if (!this.areScenesLoaded) await this.loadScenes()
       }
     },
 
+    loadScenes() {
+      return this.load(this.loadScenesList, 'Loading scenes list...')
+    },
+
     async load(fn, loadingMessage) {
       try {
         this.loadingMessage = loadingMessage
@@ -118,14 +121,8 @@ export default {
       finally {
         this.loadingMessage = null
       }
-    },
-
-    loadScenes() {
-      return this.load(this.loadScenesList, 'Loading scenes list...')
-    },
-    loadWebSocket() {
-      return this.load(this.connectToWs, 'Connecting to WebSocket server...')
     }
+
   }
 }
 </script>

+ 4 - 4
src/functions.js

@@ -1,10 +1,11 @@
 export const API_PREFIX = '/api'
 export const API_ROUTES = {
-  ping: () => `${API_PREFIX}/ping`,
+  ping: `${API_PREFIX}/ping`,
 
-  dataCollect: () => `${API_PREFIX}/dataCollect`,
+  dataCollect: `${API_PREFIX}/dataCollect`,
+  experimentCollect: `${API_PREFIX}/experimentCollect`,
 
-  listScenes: () => `${API_PREFIX}/listScenes`,
+  listScenes: `${API_PREFIX}/listScenes`,
 
   listSceneQualities: sceneName => `${API_PREFIX}/listSceneQualities?${new URLSearchParams({ sceneName })}`,
 
@@ -23,7 +24,6 @@ export const API_ROUTES = {
 export const delay = ms => new Promise(res => setTimeout(res, ms))
 
 export const buildURI = (ssl, host, port, route = '') => `${ssl ? 'https' : 'http'}://${host}:${port}${route}`
-export const buildWsURI = (ssl, host, port) => `${ssl ? 'wss' : 'ws'}://${host}:${port}`
 
 export const sortIntArray = intArray => intArray ? intArray.sort((a, b) => a - b) : null
 

+ 20 - 38
src/store/actions.js

@@ -1,6 +1,5 @@
-import Vue from 'vue'
 import router from '../router'
-import { API_ROUTES, buildURI, buildWsURI, delay, serialize } from '../functions'
+import { API_ROUTES, buildURI, serialize } from '../functions'
 
 export default {
   setGdprValidated({ state, commit }) {
@@ -14,19 +13,17 @@ export default {
     if (!state.uuid) commit('setAppUniqueId')
   },
 
-  resetApp({ commit, state }, { gdprConsent = false, hostConfig = false, progression = false }) {
-    if (hostConfig && state.socket.isConnected)
-      this._vm.$disconnect()
+  resetApp({ commit }, { gdprConsent = false, hostConfig = false, progression = false }) {
     commit('resetApp', { gdprConsent, hostConfig, progression })
   },
 
-  async setHostConfig({ state, commit, dispatch }, { ssl, host, port }) {
+  async setHostConfig({ commit, dispatch }, { ssl, host, port }) {
     // Timeout after 1s
     const controller = new AbortController()
     const signal = controller.signal
     setTimeout(() => controller.abort(), 1500)
 
-    const URI = buildURI(ssl, host, port, API_ROUTES.ping())
+    const URI = buildURI(ssl, host, port, API_ROUTES.ping)
     return fetch(URI, { signal })
       .then(async res => {
         if (res.status !== 200) throw new Error(`Received wrong HTTP status code : ${res.status} (Need 200).`)
@@ -34,14 +31,6 @@ export default {
         const content = await res.text()
         if (content !== 'pong') throw new Error('Received wrong web content (Need to receive "pong").')
 
-        this._vm.$connect(buildWsURI(ssl, host, port))
-
-        // $connect does not return a Promise, so we wait to know if it worked
-        await delay(300)
-        if (!state.socket.isConnected)
-          throw new Error('Could not connect to remote WebSocket server.')
-
-
         // Configuration is valid
         commit('setHostConfig', { ssl, host, port })
         router.push('/experiments')
@@ -57,23 +46,11 @@ export default {
     commit('setUserExperimentId', { userId, experimentId })
   },
 
-  async connectToWs({ state, getters }) {
-    if (state.socket.isConnected) return /*eslint-disable-line */
-    else if (getters.isHostConfigured) {
-      this._vm.$connect(getters.getHostWsURI)
-      // $connect does not return a Promise, so we wait to know if it worked
-      await delay(300)
-      if (!state.socket.isConnected)
-        throw new Error('Could not connect to remote WebSocket server.')
-    }
-    else throw new Error('Could not connect to WebSocket server. Host is not configured.')
-  },
-
   async collectUserData({ state, getters }) {
     let screen = serialize(window.screen)
     screen.orientation = serialize(window.screen.orientation)
 
-    return fetch(getters.getHostURI + API_ROUTES.dataCollect(), {
+    return fetch(getters.getHostURI + API_ROUTES.dataCollect, {
       method: 'POST',
       headers: {
         'Content-Type': 'application/json'
@@ -85,21 +62,26 @@ export default {
     })
   },
 
-  sendMessage({ state }, { msgId, msg = undefined }) {
-    Vue.prototype.$socket.send(JSON.stringify({
-      uuid: state.uuid,
-      userId: state.userId,
-      experimentId: state.experimentId,
-      msgId,
-      msg
-    }))
+  sendMessage({ state, getters: { getHostURI } }, { msgId, msg = undefined }) {
+    fetch(`${getHostURI}${API_ROUTES.experimentCollect}`, {
+      method: 'POST',
+      headers: {
+        'Content-Type': 'application/json'
+      },
+      body: JSON.stringify({
+        uuid: state.uuid,
+        userId: state.userId,
+        experimentId: state.experimentId,
+        msgId,
+        msg
+      })
+    })
   },
 
   async loadScenesList({ getters: { isHostConfigured, getHostURI }, commit }) {
     if (!isHostConfigured) throw new Error('Host is not configured.')
 
-    const URI = getHostURI
-    const scenes = await fetch(`${URI}${API_ROUTES.listScenes()}`).then(res => res.json())
+    const scenes = await fetch(`${getHostURI}${API_ROUTES.listScenes}`).then(res => res.json())
     commit('setListScenes', scenes.data)
   },
 

+ 1 - 7
src/store/getters.js

@@ -1,4 +1,4 @@
-import { buildURI, buildWsURI } from '../functions'
+import { buildURI } from '../functions'
 
 export default {
   isGdprValidated(state) {
@@ -15,12 +15,6 @@ export default {
       return buildURI(state.hostConfig.ssl, state.hostConfig.host, state.hostConfig.port)
   },
 
-  getHostWsURI(state, getters) {
-    if (!state) return
-    if (getters.isHostConfigured)
-      return buildWsURI(state.hostConfig.ssl, state.hostConfig.host, state.hostConfig.port)
-  },
-
   areScenesLoaded(state) {
     if (!state) return
     return state.scenesList !== null

+ 0 - 30
src/store/mutations.js

@@ -1,4 +1,3 @@
-import Vue from 'vue'
 import defaultState from '@/store/state'
 import Experiments from '@/router/experiments'
 
@@ -77,34 +76,5 @@ export default {
   setExperimentDone(state, { experimentName, sceneName, done }) {
     checkProgression(state, experimentName, sceneName)
     state.progression[experimentName][sceneName].done = done
-  },
-
-  SOCKET_ONOPEN(state, event) {
-    if (event === null) return
-
-    console.info('Connected to WebSocket server')
-    Vue.prototype.$socket = event.currentTarget
-    state.socket.isConnected = true
-  },
-  SOCKET_ONCLOSE(state, _event) {
-    console.info('Disconnected from WebSocket server')
-    state.hostConfig = defaultState().hostConfig
-    state.socket.isConnected = false
-  },
-  SOCKET_ONERROR(state, event) {
-    console.error('WebSocket connection error', state, event)
-  },
-  // default handler called for all methods
-  SOCKET_ONMESSAGE(state, { data: rawMessage }) {
-    const message = JSON.parse(rawMessage)
-    state.socket.message = message
-  },
-  // mutations for reconnect methods
-  SOCKET_RECONNECT(state, count) {
-    console.info('Reconnect to WebSocket server', state, count)
-  },
-  SOCKET_RECONNECT_ERROR(state) {
-    console.error('Could not reconnect to WebSocket server')
-    state.socket.reconnectError = true
   }
 }

+ 1 - 6
src/store/state.js

@@ -10,10 +10,5 @@ export default () => JSON.parse(JSON.stringify({
     port: null
   },
   scenesList: null,
-  progression: {},
-  socket: {
-    isConnected: false,
-    message: '',
-    reconnectError: false
-  }
+  progression: {}
 }))

+ 0 - 5
yarn.lock

@@ -9718,11 +9718,6 @@ vue-loader@^15.7.0:
     vue-hot-reload-api "^2.3.0"
     vue-style-loader "^4.1.0"
 
-vue-native-websocket@^2.0.13:
-  version "2.0.13"
-  resolved "https://registry.yarnpkg.com/vue-native-websocket/-/vue-native-websocket-2.0.13.tgz#5eaba0e7ba08749d7bff331e3290cdf5e61ca918"
-  integrity sha512-w91n76ZcvjCUzWRKX7SkVTqr9YXTxbdYQmf4RX1LvMdsM0RQvpRdWvzTWaY6kw/egsdRKVSceqDsJKbr+WTyMQ==
-
 vue-router@^3.0.6:
   version "3.0.6"
   resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-3.0.6.tgz#2e4f0f9cbb0b96d0205ab2690cfe588935136ac3"