mirror of
https://github.com/faye/websocket-driver-ruby.git
synced 2025-11-01 13:59:38 +00:00
Allow event listeners to be passed as normal rather than block arguments.
This discourages the inappropriate use of 'return' inside event listeners. Also, we return the listener, so that the caller has a reference to it in case they do pass a block.
This commit is contained in:
@@ -146,7 +146,7 @@ module Connection
|
||||
def initialize
|
||||
@driver = WebSocket::Driver.server(self)
|
||||
|
||||
@driver.on(:connect) do
|
||||
@driver.on :connect, -> (event) do
|
||||
if WebSocket::Driver.websocket?(@driver.env)
|
||||
@driver.start
|
||||
else
|
||||
@@ -154,8 +154,8 @@ module Connection
|
||||
end
|
||||
end
|
||||
|
||||
@driver.on(:message) { |e| @driver.text(e.data) }
|
||||
@driver.on(:close) { |e| close_connection_after_writing }
|
||||
@driver.on :message, -> (e) { @driver.text(e.data) }
|
||||
@driver.on :close, -> (e) { close_connection_after_writing }
|
||||
end
|
||||
|
||||
def receive_data(data)
|
||||
@@ -214,7 +214,7 @@ start sending incoming data to `driver.parse(data)` as normal, and call
|
||||
```rb
|
||||
proxy = driver.proxy('http://username:password@proxy.example.com')
|
||||
|
||||
proxy.on :connect do
|
||||
proxy.on :connect, -> (event) do
|
||||
driver.start
|
||||
end
|
||||
```
|
||||
@@ -226,7 +226,7 @@ In the event that proxy connection fails, `proxy` will emit an `:error`. You can
|
||||
inspect the proxy's response via `proxy.status` and `proxy.headers`.
|
||||
|
||||
```rb
|
||||
proxy.on :error do |error|
|
||||
proxy.on :error, -> (error) do
|
||||
puts error.message
|
||||
puts proxy.status
|
||||
puts proxy.headers.inspect
|
||||
@@ -274,23 +274,23 @@ Note that most of these methods are commands: if they produce data that should
|
||||
be sent over the socket, they will give this to you by calling
|
||||
`socket.write(string)`.
|
||||
|
||||
#### `driver.on('open') { |event| }`
|
||||
#### `driver.on 'open', -> (event) { }`
|
||||
|
||||
Sets the callback block to execute when the socket becomes open.
|
||||
|
||||
#### `driver.on('message') { |event| }`
|
||||
#### `driver.on 'message', -> (event) { }`
|
||||
|
||||
Sets the callback block to execute when a message is received. `event` will have
|
||||
a `data` attribute containing either a string in the case of a text message or
|
||||
an array of integers in the case of a binary message.
|
||||
|
||||
#### `driver.on('error') { |event| }`
|
||||
#### `driver.on 'error', -> (event) { }`
|
||||
|
||||
Sets the callback to execute when a protocol error occurs due to the other peer
|
||||
sending an invalid byte sequence. `event` will have a `message` attribute
|
||||
describing the error.
|
||||
|
||||
#### `driver.on('close') { |event| }`
|
||||
#### `driver.on 'close', -> (event) { }`
|
||||
|
||||
Sets the callback block to execute when the socket becomes closed. The `event`
|
||||
object has `code` and `reason` attributes.
|
||||
|
||||
@@ -6,16 +6,20 @@ module WebSocket
|
||||
@listeners = Hash.new { |h,k| h[k] = [] }
|
||||
end
|
||||
|
||||
def add_listener(event, &listener)
|
||||
def add_listener(event, callable = nil, &block)
|
||||
listener = callable || block
|
||||
@listeners[event.to_s] << listener
|
||||
listener
|
||||
end
|
||||
|
||||
def on(event, &listener)
|
||||
add_listener(event, &listener)
|
||||
def on(event, callable = nil, &block)
|
||||
add_listener(event, callable, &block)
|
||||
end
|
||||
|
||||
def remove_listener(event, &listener)
|
||||
def remove_listener(event, callable = nil, &block)
|
||||
listener = callable || block
|
||||
@listeners[event.to_s].delete(listener)
|
||||
listener
|
||||
end
|
||||
|
||||
def remove_all_listeners(event = nil)
|
||||
@@ -26,6 +30,8 @@ module WebSocket
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def emit(event, *args)
|
||||
@listeners[event.to_s].dup.each do |listener|
|
||||
listener.call(*args)
|
||||
|
||||
@@ -56,7 +56,7 @@ module WebSocket
|
||||
@headers = Headers.new(@http.headers)
|
||||
|
||||
if @status == 200
|
||||
emit(:connect)
|
||||
emit(:connect, ConnectEvent.new)
|
||||
else
|
||||
message = "Can't establish a connection to the server at #{@socket.url}"
|
||||
emit(:error, ProtocolError.new(message))
|
||||
|
||||
@@ -30,7 +30,7 @@ describe WebSocket::Driver::Hybi do
|
||||
|
||||
let :driver do
|
||||
driver = WebSocket::Driver::Hybi.new(socket, options)
|
||||
driver.on(:open) { |e| @open = true }
|
||||
driver.on :open, -> e { @open = true }
|
||||
driver.on(:message) { |e| @message += e.data }
|
||||
driver.on(:error) { |e| @error = e }
|
||||
driver.on(:close) { |e| @close = [e.code, e.reason] }
|
||||
|
||||
Reference in New Issue
Block a user