Method: #run_websocket

Defined in:
lib/easel/websocket.rb

#run_websocket(socket, initial_request) ⇒ Object

run_websocket



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
# File 'lib/easel/websocket.rb', line 24

def run_websocket(socket, initial_request)

  accept_connection(socket, initial_request[:fields][:Sec_WebSocket_Key][0..-3])
  child_threads = {}

  loop {
    msg = receive_msg socket
    break if msg.nil? # The socket was closed by the client.

    case msg.split(":")[0]
    when "RUN"
      cmd_id = msg.match(/^RUN:(.*)$/)[1].to_i

      unless child_threads[cmd_id]
        child_threads[cmd_id] = Thread.new do
          run_command_and_stream(socket, cmd_id)
          child_threads[cmd_id] = nil
        end
      end
    when "STOP"

      cmd_id = msg.match(/^STOP:(.*)$/)[1].to_i
      unless child_threads[cmd_id].nil?
        child_threads[cmd_id].kill
        child_threads[cmd_id] = nil
      end

    else
      log_error "Received an unrecognized message over the websocket: #{msg}"
    end
  }

end