diff --git a/lib/websocket/driver/hybi.rb b/lib/websocket/driver/hybi.rb index 3db3ee1..d906a91 100644 --- a/lib/websocket/driver/hybi.rb +++ b/lib/websocket/driver/hybi.rb @@ -181,17 +181,16 @@ module WebSocket reason ||= '' code ||= ERRORS[:normal_closure] - case @ready_state - when 0 then - @ready_state = 3 - emit(:close, CloseEvent.new(code, reason)) - true - when 1 then - frame(reason, :close, code) - @ready_state = 2 - true - else - false + if @ready_state <= 0 + @ready_state = 3 + emit(:close, CloseEvent.new(code, reason)) + true + elsif @ready_state == 1 + frame(reason, :close, code) + @ready_state = 2 + true + else + false end end diff --git a/spec/websocket/driver/client_spec.rb b/spec/websocket/driver/client_spec.rb index dba7d21..27fb8c6 100644 --- a/spec/websocket/driver/client_spec.rb +++ b/spec/websocket/driver/client_spec.rb @@ -54,6 +54,14 @@ describe WebSocket::Driver::Client do expect(driver.state).to eq nil end + describe :close do + it "changes the state to closed" do + driver.close + expect(driver.state).to eq :closed + expect(@close).to eq [1000, ""] + end + end + describe :start do it "writes the handshake request to the socket" do expect(socket).to receive(:write).with(