all files / src/components/Connection/ index.vue

0% Statements 0/66
0% Branches 0/26
0% Functions 0/11
0% Lines 0/66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                                                                                                                                                                                                                                                                                                           
<template>
  <div></div>
</template>
 
<script>
import socketIoClient from 'socket.io-client'
import { mapActions } from 'vuex'
 
export default {
  name: 'connection',
  computed: {
    server () {
      return this.$store.state.server
    },
    active () {
      return this.$store.state.project.active
    },
    preUser () {
      return this.$store.state.chat.preUser
    },
    preMessage () {
      return this.$store.state.chat.preMessage
    }
  },
  watch: {
    active: 'handleProject',
    preUser: 'handlePreUser',
    preMessage: 'handlePreMessage'
  },
  methods: {
    ...mapActions(['setServer', 'setConnect', 'setLoading']),
    ...mapActions('project', ['setProjects']),
    ...mapActions('files', ['setFiles', 'createFile', 'deleteFile', 'createVersion']),
    ...mapActions('chat', ['emptyMessages', 'updateUser', 'createMessage']),
    handleProject (data) {
      // 无效数据退出
      if (!data) {
        return
      }
 
      // reset files & messages
      this.setFiles([])
      this.emptyMessages()
 
      // reset connection to project
      if (this.projectSocket) {
        this.projectSocket.off('connect')
        this.projectSocket.off('update_project')
        this.projectSocket.off('create_file')
        this.projectSocket.off('delete_file')
        this.projectSocket.off('createVersion')
        this.projectSocket.disconnect()
      }
 
      this.setConnect(true)
      // build a new connection of project
      const projectUrl = `${this.server.domain}/project/${data.slug}`
      this.projectSocket = socketIoClient(projectUrl)
 
      this.projectSocket.on('connect', () => {
        if (process.env.NODE_ENV !== 'production') {
          console.log('[WEB] - ' + `project ${data.slug} connected`)
        }
 
        this.setConnect(false)
      })
 
      this.projectSocket.on('update_project', (data) => {
        if (process.env.NODE_ENV !== 'production') {
          console.log('[WEB] - ' + `project ${data.name} updated`)
        }
 
        this.setLoading(true)
        this.setFiles(data.files.files)
        this.setLoading(false)
      })
 
      this.projectSocket.on('create_file', (data) => {
        if (process.env.NODE_ENV !== 'production') {
          console.log('[WEB] - ' + `${data.path.full} created`)
        }
 
        this.createFile(data)
      })
 
      this.projectSocket.on('delete_file', (data) => {
        if (process.env.NODE_ENV !== 'production') {
          console.log('[WEB] - ' + `${data.path.full} deleted`)
        }
 
        this.deleteFile(data)
      })
 
      this.projectSocket.on('createVersion', (data) => {
        if (process.env.NODE_ENV !== 'production') {
          console.log('[WEB] - ' + `${data.file} updated`)
        }
 
        this.createVersion(data)
      })
 
      // reset connection of chat
      if (this.chatSocket) {
        this.chatSocket.off('connect')
        this.chatSocket.off('user')
        this.chatSocket.off('message')
        this.projectSocket.disconnect()
      }
 
      const chatUrl = `${this.server.domain}/project/${data.slug}/chat`
      this.chatSocket = socketIoClient(chatUrl)
 
      this.chatSocket.on('connect', () => {
        console.log('[WEB] - ' + `project ${data.slug} chat connected`)
      })
 
      this.chatSocket.on('user', (data) => {
        this.updateUser(data)
      })
 
      this.chatSocket.on('message', (data) => {
        this.createMessage(data)
      })
    },
    handlePreUser (data) {
      data && this.chatSocket.emit('update_user', data)
    },
    handlePreMessage (data) {
      data && this.chatSocket.emit('message', data)
    }
  },
  created () {
    // 建立socket连接
    const projectsUrl = `${process.env.NODE_ENV === 'production' ? '' : 'http://localhost:4574'}/projects`
    this.projectsSocket = socketIoClient(projectsUrl)
 
    this.projectsSocket.on('connect', () => {
      if (process.env.NODE_ENV !== 'production') {
        console.log('[WEB] - ' + 'projects connected')
      }
    })
 
    this.projectsSocket.on('config', (data) => {
      this.setServer(data)
    })
 
    this.projectsSocket.on('update_projects', (data) => {
      if (process.env.NODE_ENV !== 'production') {
        console.log('[WEB] - ' + 'projects updated')
      }
 
      // 更新状态
      this.setProjects(data)
    })
  }
}
</script>