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
- #flush ⇒ Object
-
#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
Returns a new instance of 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
45 46 47 48 49 |
# File 'lib/protocol/websocket/connection.rb', line 45 def close send_close @framer.close end |
#closed? ⇒ Boolean
41 42 43 |
# File 'lib/protocol/websocket/connection.rb', line 41 def closed? @state == :closed end |
#flush ⇒ Object
37 38 39 |
# File 'lib/protocol/websocket/connection.rb', line 37 def flush @framer.flush end |
#next_message ⇒ Array<Frame>
Returns sequence of frames, the first being either text or binary, optionally followed by a number of continuation frames.
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/protocol/websocket/connection.rb', line 162 def @framer.flush while read_frame if @frames.last&.finished? frames = @frames @frames = [] return frames end end end |
#open! ⇒ Object
143 144 145 146 147 |
# File 'lib/protocol/websocket/connection.rb', line 143 def open! @state = :open return self end |
#read_frame ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/protocol/websocket/connection.rb', line 51 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
83 84 85 86 87 88 89 |
# File 'lib/protocol/websocket/connection.rb', line 83 def receive_binary(frame) if @frames.empty? @frames << frame else raise ProtocolError, "Received binary, but expecting continuation!" end end |
#receive_close(frame) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/protocol/websocket/connection.rb', line 122 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
91 92 93 94 95 96 97 |
# File 'lib/protocol/websocket/connection.rb', line 91 def receive_continuation(frame) if @frames.any? @frames << frame else raise ProtocolError, "Received unexpected continuation!" end end |
#receive_frame(frame) ⇒ Object
157 158 159 |
# File 'lib/protocol/websocket/connection.rb', line 157 def receive_frame(frame) warn "Unhandled frame #{frame.inspect}" end |
#receive_ping(frame) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/protocol/websocket/connection.rb', line 149 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
75 76 77 78 79 80 81 |
# File 'lib/protocol/websocket/connection.rb', line 75 def receive_text(frame) if @frames.empty? @frames << frame else raise ProtocolError, "Received text, but expecting continuation!" end end |
#send_binary(buffer) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/protocol/websocket/connection.rb', line 106 def send_binary(buffer) frame = BinaryFrame.new frame.pack buffer write_frame(frame) end |
#send_close(code = Error::NO_ERROR, message = nil) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/protocol/websocket/connection.rb', line 113 def send_close(code = Error::NO_ERROR, = nil) frame = CloseFrame.new frame.pack(code, ) write_frame(frame) @state = :closed end |
#send_ping(data) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/protocol/websocket/connection.rb', line 132 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
99 100 101 102 103 104 |
# File 'lib/protocol/websocket/connection.rb', line 99 def send_text(buffer) frame = TextFrame.new frame.pack buffer write_frame(frame) end |
#write_frame(frame) ⇒ Object
71 72 73 |
# File 'lib/protocol/websocket/connection.rb', line 71 def write_frame(frame) @framer.write_frame(frame) end |