Class: Pssh::Socket
- Inherits:
-
Rack::WebSocket::Application
- Object
- Rack::WebSocket::Application
- Pssh::Socket
- Defined in:
- lib/pssh/socket.rb
Instance Attribute Summary collapse
-
#attach_cmd ⇒ Object
Returns the value of attribute attach_cmd.
-
#path ⇒ Object
Returns the value of attribute path.
-
#sessions ⇒ Object
Returns the value of attribute sessions.
Instance Method Summary collapse
- #close! ⇒ Object
- #close_websocket ⇒ Object
-
#initialize(opts = {}) ⇒ Socket
constructor
A new instance of Socket.
- #kill_all_sessions ⇒ Object
- #on_close(env) ⇒ Object
- #on_message(env, message) ⇒ Object
- #on_open(env) ⇒ Object
- #params(env) ⇒ Object
-
#write(data) ⇒ Object
Public: Writes the same data to all the open sessions.
Constructor Details
#initialize(opts = {}) ⇒ Socket
Returns a new instance of Socket.
8 9 10 11 12 13 14 15 16 |
# File 'lib/pssh/socket.rb', line 8 def initialize(opts={}) super # set up empty variables @sessions = {} @killed_sessions = [] @existing_socket = false end |
Instance Attribute Details
#attach_cmd ⇒ Object
Returns the value of attribute attach_cmd.
6 7 8 |
# File 'lib/pssh/socket.rb', line 6 def attach_cmd @attach_cmd end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/pssh/socket.rb', line 5 def path @path end |
#sessions ⇒ Object
Returns the value of attribute sessions.
4 5 6 |
# File 'lib/pssh/socket.rb', line 4 def sessions @sessions end |
Instance Method Details
#close! ⇒ Object
83 84 85 86 |
# File 'lib/pssh/socket.rb', line 83 def close! self.close_websocket @killed_sessions << @uuid end |
#close_websocket ⇒ Object
71 72 73 74 75 |
# File 'lib/pssh/socket.rb', line 71 def close_websocket self.send_data({ close: true }.to_json) @sessions.delete @uuid super end |
#kill_all_sessions ⇒ Object
77 78 79 80 81 |
# File 'lib/pssh/socket.rb', line 77 def kill_all_sessions @sessions.each do |k,v| v[:socket].close! end end |
#on_close(env) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pssh/socket.rb', line 50 def on_close(env) uuid = params(env)['uuid'] @sessions[uuid][:active] = false Thread.new do sleep 10 if @sessions[uuid][:active] == false @sessions.delete uuid print "\n#{user_string} detached.\n#{Pssh.prompt}" end end end |
#on_message(env, message) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pssh/socket.rb', line 88 def (env, ) uuid = params(env)['uuid'] return unless @sessions[uuid] case [0] when 's' @sessions[uuid][:started] = true size = [1..-1].split ',' @sessions[uuid][:winsize] = [size[0].to_i, size[1].to_i] send_data({ data: Pssh.pty.stream }.to_json) Pssh.pty.resize! when 'd' Pssh.pty.write [1..-1] when 'r' size = [1..-1].split ',' @sessions[uuid][:winsize] = [size[0].to_i, size[1].to_i] Pssh.pty.resize! end end |
#on_open(env) ⇒ Object
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 |
# File 'lib/pssh/socket.rb', line 22 def on_open(env) uuid = @uuid = params(env)['uuid'] if @killed_sessions.include? uuid # this session has been killed from the console and shouldn't be # restarted close_websocket and return end if @sessions[uuid] @sessions[uuid][:active] = true elsif Pssh.open_sessions.keys.include?(uuid) user_string = Pssh.open_sessions[uuid] ? "#{Pssh.open_sessions[uuid]} (#{uuid})" : uuid Pssh.pty. user_string @sessions[uuid] = { data: [], username: Pssh.open_sessions[uuid], user_string: user_string, active: true, started: false, winsize: [] } Pssh.open_sessions.delete uuid else @sessions[uuid] = {socket: self} self.close! and return end @sessions[uuid][:socket] = self end |
#params(env) ⇒ Object
18 19 20 |
# File 'lib/pssh/socket.rb', line 18 def params(env) Hash[env['QUERY_STRING'].split('&').map { |e| e.split '=' }] end |
#write(data) ⇒ Object
Public: Writes the same data to all the open sessions.
Returns nothing.
65 66 67 68 69 |
# File 'lib/pssh/socket.rb', line 65 def write(data) @sessions.each do |k,v| v[:socket].send_data({ data: data }.to_json) end end |