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
|
# File 'lib/lds-cf-plugin/loggregator/tailling_logs_client.rb', line 22
def make_websocket_request(app, websocket_address)
redirect_uri = nil
@em_client_thread = Thread.new do
EM.run {
ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
ws.on :open do |event|
output.puts("Connected to server.")
EventMachine.add_periodic_timer(keep_alive_interval) do
ws.send("I'm alive!")
end
end
ws.on :message do |event|
begin
received_message = LogMessage.decode(event.data.pack("C*"))
write(app, output, received_message)
rescue Exception => e
output.puts e.message
output.puts e.backtrace
output.puts("Error parsing loggregator data. Please make sure you have the latest lds-cf-plugin.")
ws.close
end
end
ws.on :error do |event|
if !redirect_uri
if event.current_target.status == 302
redirect_uri = event.current_target.["location"]
ws.close
@em_client_thread.kill
ws = nil
else
output.puts("Server error: #{websocket_address}")
output.puts(event.data.inspect) if trace
end
end
end
ws.on :close do |event|
unless redirect_uri
ws.close
case event.code
when 4000
output.puts("Error: No space given.")
when 4001
output.puts("Error: No authorization token given.")
when 4002
output.puts("Error: Not authorized.")
end
output.puts("Server connection closed.")
@em_client_thread.kill
ws = nil
end
end
}
end
wait_for_em_thread_to_finish
make_websocket_request(app, redirect_uri) if redirect_uri
end
|