summaryrefslogtreecommitdiff
path: root/app/client/socket.js
blob: 627694cc7a0c0186529784bba639082547fa9104 (plain)
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
import { store } from './store'

let socket = io.connect('/client')

// SOCKET ACTIONS

socket.on('res', (data) => {
  console.log(data.cmd)
  switch (data.cmd) {
    case 'get_last_frame':
      if (data.res !== 'working') {
        socket.emit('cmd', {
          cmd: 'get_last_frame',
        })
      }
      break
    case 'get_params':
      store.dispatch({
        type: 'LOAD_PARAMS',
        opt: data.res,
      })
      break
    case 'list_checkpoints':
      store.dispatch({
        type: 'LIST_CHECKPOINTS',
        checkpoints: data.res,
      })
      break
    case 'list_sequences':
      store.dispatch({
        type: 'LIST_SEQUENCES',
        sequences: data.res,
      })
      break
    case 'list_epochs':
      store.dispatch({
        type: 'LIST_EPOCHS',
        epochs: data.res,
      })
      break
    default:
      break
  }
  console.log(data)
})

let fps = 0;

socket.on('frame', (data) => {
  const blob = new Blob([data.frame], { type: 'image/jpg' })
  const url = URL.createObjectURL(blob)
  const img = new Image ()
  img.onload = function() {
    store.dispatch({
      type: 'CURRENT_FRAME',
      meta: data.meta,
    })
    URL.revokeObjectURL(url)
    const player = document.querySelector('.player canvas')
    const ctx = player.getContext('2d')
    ctx.drawImage(img, 0, 0, player.width, player.height)
    // player.innerHTML = ''
    // player.appendChild(img)
    fps += 1
  }
  img.src = url
})

setInterval(() => {
  store.dispatch({
    type: 'SET_FPS',
    fps: fps,
  })
  fps = 0
}, 1000)

export function list_checkpoints() {
  socket.emit('cmd', {
    cmd: 'list_checkpoints',
  })
}
export function list_epochs(checkpoint_name) {
  socket.emit('cmd', {
    cmd: 'list_epochs',
    payload: checkpoint_name,
  })
}
export function list_sequences() {
  socket.emit('cmd', {
    cmd: 'list_sequences',
  })
}
export function load_epoch(checkpoint_name, epoch) {
  console.log(">> SWITCH CHECKPOINT", checkpoint_name, epoch)
  socket.emit('cmd', {
    cmd: 'load_epoch',
    payload: checkpoint_name + ':' + epoch,
  })
}
export function load_sequence(sequence) {
  socket.emit('cmd', {
    cmd: 'load_sequence',
    payload: sequence,
  })
}
export function get_params() {
  socket.emit('cmd', {
    cmd: 'get_params',
  })
}
export function set_param(key, value) {
  socket.emit('cmd', {
    cmd: 'set_param',
    payload: {
      'key': key,
      'value': value,
    }
  })
}
export { socket }