diff --git a/README.md b/README.md index 43c7926..96b4961 100644 --- a/README.md +++ b/README.md @@ -274,33 +274,33 @@ 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) {}` Adds a callback block to execute when the socket becomes open. -#### `driver.on :message, -> (event) { }` +#### `driver.on :message, -> (event) {}` Adds a 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) {}` Adds a 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) {}` Adds a callback block to execute when the socket becomes closed. The `event` object has `code` and `reason` attributes. -#### `driver.on :ping, -> (event) { }` +#### `driver.on :ping, -> (event) {}` Adds a callback block to execute when a ping is received. You do not need to handle this by sending a pong frame yourself; the driver handles this for you. -#### `driver.on :pong, -> (event) { }` +#### `driver.on :pong, -> (event) {}` Adds a callback block to execute when a pong is received. If this was in response to a ping you sent, you can also handle this event via the diff --git a/examples/tcp_client.rb b/examples/tcp_client.rb index d69432d..44519db 100644 --- a/examples/tcp_client.rb +++ b/examples/tcp_client.rb @@ -5,7 +5,7 @@ require 'uri' class WSClient - DEFAULT_PORTS = {'ws' => 80, 'wss' => 443} + DEFAULT_PORTS = { 'ws' => 80, 'wss' => 443 } attr_reader :url, :thread diff --git a/lib/websocket/driver.rb b/lib/websocket/driver.rb index 9f699a1..6f440de 100644 --- a/lib/websocket/driver.rb +++ b/lib/websocket/driver.rb @@ -215,7 +215,7 @@ def self.encode(string, encoding = nil) def self.validate_options(options, valid_keys) options.keys.each do |key| unless valid_keys.include?(key) - raise ConfigurationError, "Unrecognized option: #{key.inspect}" + raise ConfigurationError, "Unrecognized option: #{ key.inspect }" end end end diff --git a/lib/websocket/driver/client.rb b/lib/websocket/driver/client.rb index dee6f5a..465f1da 100644 --- a/lib/websocket/driver/client.rb +++ b/lib/websocket/driver/client.rb @@ -20,10 +20,10 @@ def initialize(socket, options = {}) uri = URI.parse(@socket.url) unless VALID_SCHEMES.include?(uri.scheme) - raise URIError, "#{socket.url} is not a valid WebSocket URL" + raise URIError, "#{ socket.url } is not a valid WebSocket URL" end - host = uri.host + (uri.port ? ":#{uri.port}" : '') + host = uri.host + (uri.port ? ":#{ uri.port }" : '') path = (uri.path == '') ? '/' : uri.path @pathname = path + (uri.query ? '?' + uri.query : '') @@ -44,7 +44,7 @@ def initialize(socket, options = {}) end def version - "hybi-#{VERSION}" + "hybi-#{ VERSION }" end def proxy(origin, options = {}) @@ -73,19 +73,19 @@ def parse(chunk) parse(@http.body) end - private + private def handshake_request extensions = @extensions.generate_offer @headers['Sec-WebSocket-Extensions'] = extensions if extensions - start = "GET #{@pathname} HTTP/1.1" + start = "GET #{ @pathname } HTTP/1.1" headers = [start, @headers.to_s, ''] headers.join("\r\n") end def fail_handshake(message) - message = "Error during WebSocket handshake: #{message}" + message = "Error during WebSocket handshake: #{ message }" @ready_state = 3 emit(:error, ProtocolError.new(message)) emit(:close, CloseEvent.new(ERRORS[:protocol_error], message)) @@ -96,7 +96,7 @@ def validate_handshake @headers = Headers.new(@http.headers) unless @http.code == 101 - return fail_handshake("Unexpected response code: #{@http.code}") + return fail_handshake("Unexpected response code: #{ @http.code }") end upgrade = @http['Upgrade'] || '' diff --git a/lib/websocket/driver/headers.rb b/lib/websocket/driver/headers.rb index ca92c1b..769dd49 100644 --- a/lib/websocket/driver/headers.rb +++ b/lib/websocket/driver/headers.rb @@ -25,7 +25,7 @@ def []=(name, value) return if value.nil? key = HTTP.normalize_header(name) return unless @sent.add?(key) or ALLOWED_DUPLICATES.include?(key) - @lines << "#{name.strip}: #{value.to_s.strip}\r\n" + @lines << "#{ name.strip }: #{ value.to_s.strip }\r\n" end def inspect diff --git a/lib/websocket/driver/hybi.rb b/lib/websocket/driver/hybi.rb index 96be39e..1d57366 100644 --- a/lib/websocket/driver/hybi.rb +++ b/lib/websocket/driver/hybi.rb @@ -52,7 +52,7 @@ def self.generate_accept(key) MIN_RESERVED_ERROR = 3000 MAX_RESERVED_ERROR = 4999 - PACK_FORMATS = {2 => 'n', 8 => 'Q>'} + PACK_FORMATS = { 2 => 'n', 8 => 'Q>' } def initialize(socket, options = {}) super @@ -78,7 +78,7 @@ def initialize(socket, options = {}) end def version - "hybi-#{VERSION}" + "hybi-#{ VERSION }" end def add_extension(extension) @@ -228,7 +228,7 @@ def handshake_response version = @socket.env['HTTP_SEC_WEBSOCKET_VERSION'] unless version == VERSION - raise ProtocolError.new("Unsupported WebSocket version: #{VERSION}") + raise ProtocolError.new("Unsupported WebSocket version: #{ VERSION }") end unless sec_key @@ -281,17 +281,17 @@ def parse_opcode(octet) unless @extensions.valid_frame_rsv?(@frame) return fail(:protocol_error, - "One or more reserved bits are on: reserved1 = #{@frame.rsv1 ? 1 : 0}" + - ", reserved2 = #{@frame.rsv2 ? 1 : 0 }" + - ", reserved3 = #{@frame.rsv3 ? 1 : 0 }") + "One or more reserved bits are on: reserved1 = #{ @frame.rsv1 ? 1 : 0 }" + + ", reserved2 = #{ @frame.rsv2 ? 1 : 0 }" + + ", reserved3 = #{ @frame.rsv3 ? 1 : 0 }") end unless OPCODES.values.include?(@frame.opcode) - return fail(:protocol_error, "Unrecognized frame opcode: #{@frame.opcode}") + return fail(:protocol_error, "Unrecognized frame opcode: #{ @frame.opcode }") end unless MESSAGE_OPCODES.include?(@frame.opcode) or @frame.final - return fail(:protocol_error, "Received fragmented control frame: opcode = #{@frame.opcode}") + return fail(:protocol_error, "Received fragmented control frame: opcode = #{ @frame.opcode }") end if @message and OPENING_OPCODES.include?(@frame.opcode) @@ -321,7 +321,7 @@ def parse_extended_length(buffer) @stage = @frame.masked ? 3 : 4 unless MESSAGE_OPCODES.include?(@frame.opcode) or @frame.length <= 125 - return fail(:protocol_error, "Received control frame having too long payload: #{@frame.length}") + return fail(:protocol_error, "Received control frame having too long payload: #{ @frame.length }") end return unless check_frame_length diff --git a/lib/websocket/driver/proxy.rb b/lib/websocket/driver/proxy.rb index e4891e3..af9e316 100644 --- a/lib/websocket/driver/proxy.rb +++ b/lib/websocket/driver/proxy.rb @@ -4,7 +4,7 @@ class Driver class Proxy include EventEmitter - PORTS = {'ws' => 80, 'wss' => 443} + PORTS = { 'ws' => 80, 'wss' => 443 } attr_reader :status, :headers @@ -20,7 +20,7 @@ def initialize(client, origin, options) @state = 0 @headers = Headers.new - @headers['Host'] = @origin.host + (@origin.port ? ":#{@origin.port}" : '') + @headers['Host'] = @origin.host + (@origin.port ? ":#{ @origin.port }" : '') @headers['Connection'] = 'keep-alive' @headers['Proxy-Connection'] = 'keep-alive' @@ -41,7 +41,7 @@ def start @state = 1 port = @origin.port || PORTS[@origin.scheme] - start = "CONNECT #{@origin.host}:#{port} HTTP/1.1" + start = "CONNECT #{ @origin.host }:#{ port } HTTP/1.1" headers = [start, @headers.to_s, ''] @socket.write(headers.join("\r\n")) @@ -58,7 +58,7 @@ def parse(chunk) if @status == 200 emit(:connect, ConnectEvent.new) else - message = "Can't establish a connection to the server at #{@socket.url}" + message = "Can't establish a connection to the server at #{ @socket.url }" emit(:error, ProtocolError.new(message)) end end diff --git a/lib/websocket/driver/server.rb b/lib/websocket/driver/server.rb index f13fde9..71ec613 100644 --- a/lib/websocket/driver/server.rb +++ b/lib/websocket/driver/server.rb @@ -17,9 +17,9 @@ def env def url return nil unless e = env - url = "ws://#{e['HTTP_HOST']}" + url = "ws://#{ e['HTTP_HOST'] }" url << e['PATH_INFO'] - url << "?#{e['QUERY_STRING']}" unless e['QUERY_STRING'] == '' + url << "?#{ e['QUERY_STRING'] }" unless e['QUERY_STRING'] == '' url end diff --git a/lib/websocket/http/request.rb b/lib/websocket/http/request.rb index 1358326..664670d 100644 --- a/lib/websocket/http/request.rb +++ b/lib/websocket/http/request.rb @@ -30,11 +30,11 @@ def complete super @headers.each do |name, value| rack_name = name.upcase.gsub(/-/, '_') - rack_name = "HTTP_#{rack_name}" unless RESERVED_HEADERS.include?(name) + rack_name = "HTTP_#{ rack_name }" unless RESERVED_HEADERS.include?(name) @env[rack_name] = value end if host = @env['HTTP_HOST'] - uri = URI.parse("http://#{host}") + uri = URI.parse("http://#{ host }") @env['SERVER_NAME'] = uri.host @env['SERVER_PORT'] = uri.port.to_s end diff --git a/spec/websocket/driver/client_spec.rb b/spec/websocket/driver/client_spec.rb index 75b7f4f..3588670 100644 --- a/spec/websocket/driver/client_spec.rb +++ b/spec/websocket/driver/client_spec.rb @@ -11,7 +11,7 @@ end let :options do - {:protocols => protocols} + { :protocols => protocols } end let :protocols do @@ -65,7 +65,7 @@ describe :start do it "writes the handshake request to the socket" do expect(socket).to receive(:write).with( - "GET /socket HTTP/1.1\r\n" + + "GET /socket HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + @@ -84,7 +84,7 @@ it "writes the handshake with Sec-WebSocket-Protocol" do expect(socket).to receive(:write).with( - "GET /socket HTTP/1.1\r\n" + + "GET /socket HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + @@ -101,7 +101,7 @@ it "writes the handshake with Sec-WebSocket-Protocol" do expect(socket).to receive(:write).with( - "GET /socket HTTP/1.1\r\n" + + "GET /socket HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + @@ -128,7 +128,7 @@ it "writes the handshake with custom headers" do expect(socket).to receive(:write).with( - "GET /socket HTTP/1.1\r\n" + + "GET /socket HTTP/1.1\r\n" + "Host: www.example.com\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + @@ -272,7 +272,7 @@ expect(driver.state).to eq :closed end end - + describe "with a bad Accept header" do before do resp = response.gsub(/QV3/, "wrong") diff --git a/spec/websocket/driver/hybi_spec.rb b/spec/websocket/driver/hybi_spec.rb index 3a415c4..1152cb9 100644 --- a/spec/websocket/driver/hybi_spec.rb +++ b/spec/websocket/driver/hybi_spec.rb @@ -18,7 +18,7 @@ end let :options do - {:masking => false} + { :masking => false } end let :socket do