Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const socket = io()
  2.  
  3. new Vue({
  4.   el: '#app',
  5.   data: {
  6.     sessionId: null,
  7.     nickname: localStorage.getItem('nickname'),
  8.     text: null,
  9.     rooms: [],
  10.     currentRoom: null,
  11.     selectedCount: 0,
  12.     status: 'Wait',
  13.     users: []
  14.   },
  15.   methods: {
  16.     setNickname() {
  17.       let nickname = prompt("Enter nickname:")
  18.       if (nickname) {
  19.         localStorage.setItem('nickname', nickname)
  20.         this.nickname = nickname
  21.         socket.emit('updateUser', this.sessionId, this.nickname)
  22.       }
  23.     },
  24.     createRoom() {
  25.       let name = prompt('Enter room name:')
  26.       if (name) {
  27.         socket.emit('createRoom', name)
  28.         socket.emit('getRooms')
  29.       }
  30.     },
  31.     openRoom(room) {
  32.       this.currentRoom = room
  33.       socket.emit('openRoom', this.sessionId, room)
  34.     },
  35.     onChanged() {
  36.       socket.emit('locked', this.sessionId)
  37.       socket.emit('onChanged', this.sessionId, this.nickname, this.currentRoom, this.text)
  38.     },
  39.     getOnline() {
  40.       let usersNames = this.users.map(u => u.nickname ? `${u.nickname} (${u.room})` : `${u.sessionId} (${u.room})`).join(',')
  41.       alert(usersNames)
  42.     }
  43.   },
  44.   mounted() {
  45.     socket.on('connect', () => {
  46.       this.sessionId = socket.id
  47.       socket.emit('sign', this.sessionId, this.nickname)
  48.     })
  49.     socket.on('getUsers', users => this.users = users)
  50.     socket.on('getRooms', rooms => this.rooms = rooms)
  51.     socket.on('getRoomTextById', (sessionId, text) => sessionId == this.sessionId ? this.text = text : null)
  52.     socket.on('onUpdated', (sessionId, nickname, text) => {
  53.       this.status = `${nickname ? nickname : sessionId} typing...`
  54.       if (sessionId != this.sessionId) {
  55.         this.text = text
  56.       }
  57.     })
  58.     socket.on('locked', sessionId => {
  59.       if (sessionId != this.sessionId) {
  60.         this.$refs.textarea.disabled = true
  61.       }
  62.     })
  63.     socket.on('onlocked', () => {
  64.       this.status = 'Wait'
  65.       this.$refs.textarea.disabled = false
  66.     })
  67.     document.addEventListener('selectionchange', () => {
  68.       if (this.$refs.textarea) {
  69.         this.selectedCount = this.$refs.textarea.selectionEnd - this.$refs.textarea.selectionStart
  70.       }
  71.     })
  72.   }
  73. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement