Class: Async::WebSocket::Connection
- Inherits:
-
Object
- Object
- Async::WebSocket::Connection
- Defined in:
- lib/async/websocket.rb
Constant Summary collapse
- READ_BUFFER_SIZE =
1024*8
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(env, io) ⇒ Connection
constructor
A new instance of Connection.
- #read ⇒ Object
- #run(&handler) ⇒ Object
- #write(string) ⇒ Object
Constructor Details
#initialize(env, io) ⇒ Connection
Returns a new instance of Connection.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/async/websocket.rb', line 35 def initialize(env, io) @env = env @io = io scheme = Rack::Request.new(env).ssl? ? 'wss:' : 'ws:' @url = "#{scheme}//#{env['HTTP_HOST']}#{env['REQUEST_URI']}" @driver = ::WebSocket::Driver.rack(self) @running = false end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
33 34 35 |
# File 'lib/async/websocket.rb', line 33 def env @env end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
33 34 35 |
# File 'lib/async/websocket.rb', line 33 def url @url end |
Instance Method Details
#read ⇒ Object
50 51 52 |
# File 'lib/async/websocket.rb', line 50 def read @driver.parse(@io.read(READ_BUFFER_SIZE)) end |
#run(&handler) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/async/websocket.rb', line 54 def run(&handler) @running = true @driver.on(:close) do @running = false end @driver.on(:open) do yield @driver if block_given? end @driver.start while @running self.read end end |
#write(string) ⇒ Object
46 47 48 |
# File 'lib/async/websocket.rb', line 46 def write(string) @io.write(string) end |