Class: Pytty::Daemon::Api::WebSockets
- Inherits:
-
Object
- Object
- Pytty::Daemon::Api::WebSockets
- Defined in:
- lib/pytty/daemon/api/web_sockets.rb
Constant Summary collapse
- @@connections =
[]
Instance Method Summary collapse
- #close ⇒ Object
- #handle ⇒ Object
-
#initialize(env) ⇒ WebSockets
constructor
A new instance of WebSockets.
- #read ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize(env) ⇒ WebSockets
Returns a new instance of WebSockets.
8 9 10 11 12 13 14 15 16 |
# File 'lib/pytty/daemon/api/web_sockets.rb', line 8 def initialize(env) @env = env @connection = Async::WebSocket::Server.open(env) @@connections << @connection @messages = Async::Queue.new @writes = Async::Queue.new end |
Instance Method Details
#close ⇒ Object
18 19 20 21 22 23 |
# File 'lib/pytty/daemon/api/web_sockets.rb', line 18 def close @@connections.each do |connection| connection.close end @@connections = [] end |
#handle ⇒ Object
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 |
# File 'lib/pytty/daemon/api/web_sockets.rb', line 33 def handle Async::Task.current.async do while = @connection.next_event type = case when WebSocket::Driver::OpenEvent puts "ws: open #{@env["REMOTE_ADDR"]}" when WebSocket::Driver::CloseEvent puts "ws: close #{@env["REMOTE_ADDR"]}" @@connections.delete @connection when WebSocket::Driver::MessageEvent puts "ws: message #{@env["REMOTE_ADDR"]}" @messages.enqueue .data else raise "ws: unknown #{.inspect}" end end end Async::Task.current.async do |task| while = @writes.dequeue @@connections.each do |connection| connection.() rescue Exception => ex #TODO end end end end |
#read ⇒ Object
25 26 27 |
# File 'lib/pytty/daemon/api/web_sockets.rb', line 25 def read @messages.dequeue end |
#write(message) ⇒ Object
29 30 31 |
# File 'lib/pytty/daemon/api/web_sockets.rb', line 29 def write() @writes.enqueue end |