Class: WebSocket::Connection
- Inherits:
-
Object
- Object
- WebSocket::Connection
- Defined in:
- lib/websocket/connection/version.rb,
lib/websocket/connection.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect ⇒ Object (also: #start)
-
#initialize(url, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #on(event, callable = nil, &block) ⇒ Object
- #open? ⇒ Boolean
- #read ⇒ Object
- #send(message) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Connection
Returns a new instance of Connection.
8 9 10 11 |
# File 'lib/websocket/connection.rb', line 8 def initialize(url, = {}) @url = url @driver = ::WebSocket::Driver.client(self, ) end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/websocket/connection.rb', line 6 def url @url end |
Class Method Details
.start(url, options = {}) ⇒ Object
13 14 15 16 17 |
# File 'lib/websocket/connection.rb', line 13 def self.start(url, = {}) connection = self.new(url, ) connection.start connection end |
Instance Method Details
#close ⇒ Object
26 27 28 29 |
# File 'lib/websocket/connection.rb', line 26 def close @driver.close @stream.close end |
#closed? ⇒ Boolean
31 32 33 |
# File 'lib/websocket/connection.rb', line 31 def closed? @stream.closed? end |
#connect ⇒ Object Also known as: start
19 20 21 22 23 |
# File 'lib/websocket/connection.rb', line 19 def connect uri = URI.parse(url) @stream = ::Socket.tcp(uri.host, uri.port) @driver.start end |
#on(event, callable = nil, &block) ⇒ Object
39 40 41 |
# File 'lib/websocket/connection.rb', line 39 def on(event, callable = nil, &block) @driver.on(event, callable, &block) end |
#open? ⇒ Boolean
35 36 37 |
# File 'lib/websocket/connection.rb', line 35 def open? !@stream.closed? end |
#read ⇒ Object
56 57 58 |
# File 'lib/websocket/connection.rb', line 56 def read @driver.parse(@stream.recv(256)) end |
#send(message) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/websocket/connection.rb', line 43 def send() case when Numeric then @driver.text(.to_s) when String then @driver.text() when Array then @driver.binary() else false end end |
#write(data) ⇒ Object
52 53 54 |
# File 'lib/websocket/connection.rb', line 52 def write(data) @stream.write(data) end |