Class: Protocol::WebSocket::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#framerObject (readonly)

Returns the value of attribute framer.



32
33
34
# File 'lib/protocol/websocket/connection.rb', line 32

def framer
  @framer
end

#framesObject

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

#closeObject



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_messageArray<Frame>



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/protocol/websocket/connection.rb', line 158

def next_message
  @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_frameObject



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.message)
  
  raise
rescue
  send_close(Error::PROTOCOL_ERROR, $!.message)
  
  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, message = frame.unpack
  
  if code and code != Error::NO_ERROR
    raise ClosedError.new message, 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, message = nil)
  frame = CloseFrame.new
  frame.pack(code, message)
  
  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