浏览代码

Added unique app identifier, cached on refresh

rigwild 6 年之前
父节点
当前提交
fe2597b4d7

+ 4 - 1
server/webSocket/index.js

@@ -33,7 +33,10 @@ const createWsServer = httpServer => {
   wss.on('listening', () => wsLogger.info(formatLog('The WebSocket server was started')))
   wss.on('error', err => wsLogger.error(formatError(err)))
 
-  wss.on('connection', ws => {
+  wss.on('connection', (ws, req) => {
+    // Unique identifier passed with the request url
+    ws.uuid = req.url.replace('/?uuid=', '')
+
     wsLogger.info(formatLog('New client connected.'))
 
     ws.on('message', data => messageHandler(ws)(data).catch(err => errorHandler(ws)(err)))

+ 1 - 0
server/webSocket/messageHandler.js

@@ -22,6 +22,7 @@ const messageHandler = ws => async data => {
     throw new Error('Invalid JSON data.')
   }
 
+  json.WS_UNIQUE_UUID = ws.uuid
   await DataController.add(json)
   if (!TEST_MODE) wsLogger.info(formatLog(json, 'message'))
   ws.send('{"message":"ok"}')

+ 2 - 1
src/App.vue

@@ -103,11 +103,12 @@ export default {
     }
   },
   async mounted() {
+    this.setAppUniqueId()
     if (this.isHostConfigured) await this.loadWebSocket()
     if (this.isHostConfigured && !this.areScenesLoaded) await this.loadScenes()
   },
   methods: {
-    ...mapActions(['loadScenesList', 'connectToWs']),
+    ...mapActions(['setAppUniqueId', 'loadScenesList', 'connectToWs']),
 
     async load(fn, loadingMessage) {
       try {

+ 2 - 2
src/components/HostConfig.vue

@@ -13,7 +13,7 @@
                   <v-flex xs3>
                     <v-select
                       v-model="config.ssl"
-                      :items="[true, false]"
+                      :items="[false, true]"
                       label="SSL"
                     />
                   </v-flex>
@@ -63,7 +63,7 @@ export default {
   data() {
     return {
       config: {
-        ssl: true,
+        ssl: false,
         host: 'diran.univ-littoral.fr',
         port: '80'
       },

+ 1 - 1
src/functions.js

@@ -21,4 +21,4 @@ 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 buildWsURI = (ssl, host, port, uuid = '') => `${ssl ? 'wss' : 'ws'}://${host}:${port}?uuid=${uuid}`

+ 4 - 1
src/store/actions.js

@@ -2,10 +2,13 @@ import Vue from 'vue'
 import { API_ROUTES, buildURI, buildWsURI, delay } from '../functions'
 
 export default {
+  setAppUniqueId({ state, commit }) {
+    if (!state.uuid) commit('setAppUniqueId')
+  },
+
   resetApp({ commit }, { hostConfig = false, progression = false }) {
     commit('resetApp', { hostConfig, progression })
   },
-
   async setHostConfig({ state, commit }, { ssl, host, port }) {
     // Timeout after 1s
     const controller = new AbortController()

+ 1 - 1
src/store/getters.js

@@ -11,7 +11,7 @@ export default {
 
   getHostWsURI(state, getters) {
     if (getters.isHostConfigured)
-      return buildWsURI(state.hostConfig.ssl, state.hostConfig.host, state.hostConfig.port)
+      return buildWsURI(state.hostConfig.ssl, state.hostConfig.host, state.hostConfig.port, state.uuid)
   },
 
   areScenesLoaded(state) {

+ 1 - 0
src/store/index.js

@@ -12,6 +12,7 @@ const vuexLocal = new VuexPersistence({
   storage: window.localStorage,
   key: 'webexpe-state',
   reducer: state => ({
+    uuid: state.uuid,
     hostConfig: state.hostConfig,
     scenesList: state.scenesList,
     progression: state.progression

+ 4 - 0
src/store/mutations.js

@@ -10,6 +10,10 @@ const checkProgression = (state, experimentName, sceneName) => {
 }
 
 export default {
+  setAppUniqueId(state) {
+    state.uuid = [...Array(30)].map(() => Math.random().toString(36)[2]).join('')
+  },
+
   resetApp(state, { hostConfig, progression }) {
     if (hostConfig) {
       if (state.socket.isConnected)

+ 1 - 0
src/store/state.js

@@ -1,5 +1,6 @@
 // Deep copy to not mutate it with the store (default state is needed when reloading after a refresh)
 export default () => JSON.parse(JSON.stringify({
+  uuid: null,
   hostConfig: {
     ssl: null,
     host: null,