Class: Protocol::WebSocket::Connection
- Inherits:
-
Object
- Object
- Protocol::WebSocket::Connection
- Defined in:
- lib/protocol/websocket/connection.rb
Instance Attribute Summary collapse
-
#framer ⇒ Object
readonly
Returns the value of attribute framer.
-
#frames ⇒ Object
Buffered frames which form part of a complete message.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(framer) ⇒ Connection
constructor
A new instance of Connection.
-
#next_message ⇒ Array<Frame>
Sequence of frames, the first being either text or binary, optionally followed by a number of continuation frames.
- #open! ⇒ Object
- #read_frame ⇒ Object
- #receive_binary(frame) ⇒ Object
- #receive_close(frame) ⇒ Object
- #receive_continuation(frame) ⇒ Object
- #receive_frame(frame) ⇒ Object
- #receive_ping(frame) ⇒ Object
- #receive_text(frame) ⇒ Object
- #send_binary(buffer) ⇒ Object
- #send_close(code = Error::NO_ERROR, message = nil) ⇒ Object
- #send_ping(data) ⇒ Object
- #send_text(buffer) ⇒ Object
- #write_frame(frame) ⇒ Object
Constructor Details
#initialize(framer) ⇒ Connection
26 27 28 29 30 |
# File 'lib/protocol/websocket/connection.rb', line 26 def initialize(framer) @framer = framer @state = :open @frames = [] end |
Instance Attribute Details
#framer ⇒ Object (readonly)
Returns the value of attribute framer.
32 33 34 |
# File 'lib/protocol/websocket/connection.rb', line 32 def framer @framer end |
#frames ⇒ Object
Buffered frames which form part of a complete message.
35 36 37 |
# File 'lib/protocol/websocket/connection.rb', line 35 def frames @frames end |
Instance Method Details
#close ⇒ Object
41 42 43 44 45 |
# File 'lib/protocol/websocket/connection.rb', line 41 def close send_close @framer.close end |
#closed? ⇒ Boolean
37 38 39 |
# File 'lib/protocol/websocket/connection.rb', line 37 def closed? @state == :closed end |
#next_message ⇒ Array<Frame>
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/protocol/websocket/connection.rb', line 158 def @framer.flush while read_frame if @frames.last&.finished? frames = @frames @frames = [] return frames end end end |
#open! ⇒ Object
139 140 141 142 143 |
# File 'lib/protocol/websocket/connection.rb', line 139 def open! @state = :open return self end |
#read_frame ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/protocol/websocket/connection.rb', line 47 def read_frame return nil if closed? frame = @framer.read_frame yield frame if block_given? frame.apply(self) return frame rescue ProtocolError => error send_close(error.code, error.) raise rescue send_close(Error::PROTOCOL_ERROR, $!.) raise end |
#receive_binary(frame) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/protocol/websocket/connection.rb', line 79 def receive_binary(frame) if @frames.empty? @frames << frame else raise ProtocolError, "Received binary, but expecting continuation!" end end |
#receive_close(frame) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/protocol/websocket/connection.rb', line 118 def receive_close(frame) @state = :closed code, = frame.unpack if code and code != Error::NO_ERROR raise ClosedError.new , code end end |
#receive_continuation(frame) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/protocol/websocket/connection.rb', line 87 def receive_continuation(frame) if @frames.any? @frames << frame else raise ProtocolError, "Received unexpected continuation!" end end |
#receive_frame(frame) ⇒ Object
153 154 155 |
# File 'lib/protocol/websocket/connection.rb', line 153 def receive_frame(frame) warn "Unhandled frame #{frame.inspect}" end |
#receive_ping(frame) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/protocol/websocket/connection.rb', line 145 def receive_ping(frame) if @state != :closed write_frame(frame.reply) else raise ProtocolError, "Cannot receive ping in state #{@state}" end end |
#receive_text(frame) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/protocol/websocket/connection.rb', line 71 def receive_text(frame) if @frames.empty? @frames << frame else raise ProtocolError, "Received text, but expecting continuation!" end end |
#send_binary(buffer) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/protocol/websocket/connection.rb', line 102 def send_binary(buffer) frame = BinaryFrame.new frame.pack buffer write_frame(frame) end |
#send_close(code = Error::NO_ERROR, message = nil) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/protocol/websocket/connection.rb', line 109 def send_close(code = Error::NO_ERROR, = nil) frame = CloseFrame.new frame.pack(code, ) write_frame(frame) @state = :closed end |
#send_ping(data) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/protocol/websocket/connection.rb', line 128 def send_ping(data) if @state != :closed frame = PingFrame.new frame.pack data write_frame(frame) else raise ProtocolError, "Cannot send ping in state #{@state}" end end |
#send_text(buffer) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/protocol/websocket/connection.rb', line 95 def send_text(buffer) frame = TextFrame.new frame.pack buffer write_frame(frame) end |
#write_frame(frame) ⇒ Object
67 68 69 |
# File 'lib/protocol/websocket/connection.rb', line 67 def write_frame(frame) @framer.write_frame(frame) end |