Method: VoxelammingGem::BuildBox#send_data

Defined in:
lib/voxelamming_gem.rb

#send_dataObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/voxelamming_gem.rb', line 288

def send_data
  puts 'send_data'
  now = DateTime.now
  data_to_send = {
    "translation": @translation,
    "frameTranslations": @frame_translations,
    "globalAnimation": @global_animation,
    "animation": @animation,
    "boxes": @boxes,
    "frames": @frames,
    "sentence": @sentence,
    "lights": @lights,
    "commands": @commands,
    "size": @size,
    "shape": @shape,
    "interval": @build_interval,
    "isMetallic": @is_metallic,
    "roughness": @roughness,
    "isAllowedFloat": @is_allowed_float,
    "date": now.to_s
  }.to_json

  EM.run do
    ws = Faye::WebSocket::Client.new('wss://websocket.voxelamming.com')

    ws.on :open do |_event|
      p [:open]
      puts 'WebSocket connection open'
      ws.send(@room_name)
      puts "Joined room: #{@room_name}"
      ws.send(data_to_send)
      puts data_to_send
      puts 'Sent data to server'

      EM.add_timer(1) do
        ws.close
        EM.stop
      end
    end

    ws.on :error do |event|
      puts "WebSocket error: #{event.message}"
      EM.stop
    end

    ws.on :close do |_event|
      puts 'WebSocket connection closed'
      EM.stop
    end
  end
end