Module: WebSocket::ClientInstanceMethods
- Included in:
- Client
- Defined in:
- lib/websocket_client.rb
Overview
The ClientInstanceMethods module
Instance Method Summary collapse
- #channelActive(ctx) ⇒ Object
- #channelInactive(ctx) ⇒ Object
- #client_has_shut_down ⇒ Object
- #close(channel = @channel) ⇒ Object
- #connect ⇒ Object
- #execute_command(str, websocket = self) ⇒ Object
- #gets ⇒ Object
- #invoke_user_app ⇒ Object
- #messageReceived(ctx, msg) ⇒ Object
- #ping ⇒ Object
- #puts(msg) ⇒ Object
- #read_user_commands(websocket = self) ⇒ Object
- #session ⇒ Object
- #shut_down_callbacks ⇒ Object
- #shutdown ⇒ Object
- #when_client_has_shut_down(*args, &block) ⇒ Object
Instance Method Details
#channelActive(ctx) ⇒ Object
212 213 214 |
# File 'lib/websocket_client.rb', line 212 def channelActive(ctx) log.info "Connected to #{ctx.channel().remoteAddress0()}" end |
#channelInactive(ctx) ⇒ Object
216 217 218 |
# File 'lib/websocket_client.rb', line 216 def channelInactive(ctx) log.info "Disconnected from #{ctx.channel().remoteAddress0()}" end |
#client_has_shut_down ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/websocket_client.rb', line 204 def client_has_shut_down shut_down_callbacks.take_while do |callback| callback[:block]&.call(*callback.fetch(:args, [])) end rescue StandardError => e log.error e. end |
#close(channel = @channel) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/websocket_client.rb', line 160 def close(channel = @channel) log.debug 'Closing primary channel' channel.writeAndFlush(CloseWebSocketFrame.new) channel.closeFuture().sync() ensure shutdown end |
#connect ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/websocket_client.rb', line 151 def connect @channel = bootstrap.connect(@host, @port).sync().channel() channel_initializer.default_handler.handshake_future.sync() rescue AbstractChannel::AnnotatedConnectException => e raise e. rescue StandardError => e raise "Connection failure: #{e.}" end |
#execute_command(str, websocket = self) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/websocket_client.rb', line 229 def execute_command(str, websocket = self) return if str.empty? case str when /bye/i, /quit/i websocket.puts "#{str}\r\n" close when /ping/i then ping else websocket.puts "#{str}\r\n" end end |
#gets ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/websocket_client.rb', line 143 def gets log.debug 'Waiting for response from server' @queue.take rescue StandardError => e warn "Unexpected error waiting for message: #{e.}" nil end |
#invoke_user_app ⇒ Object
186 187 188 189 190 |
# File 'lib/websocket_client.rb', line 186 def invoke_user_app @user_app&.call(self) ensure close end |
#messageReceived(ctx, msg) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/websocket_client.rb', line 220 def (ctx, msg) log.trace "Received message: #{msg}" case msg when TextWebSocketFrame then @application_handler.call(ctx, msg.text()) when PongWebSocketFrame then puts 'pong!' when CloseWebSocketFrame then ctx.channel().close() end end |
#ping ⇒ Object
175 176 177 |
# File 'lib/websocket_client.rb', line 175 def ping @channel.writeAndFlush(PingWebSocketFrame.new(Unpooled.wrappedBuffer(PING_MSG_CONTENT))) end |
#puts(msg) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/websocket_client.rb', line 135 def puts(msg) sleep 0.1 until @channel.isActive() msg.chomp! log.trace "#puts msg: #{msg.inspect}" raise 'Message is empty!' if msg.nil? || msg.empty? @last_write_future = @channel.writeAndFlush(TextWebSocketFrame.new(msg)) end |
#read_user_commands(websocket = self) ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/websocket_client.rb', line 240 def read_user_commands(websocket = self) loop do print @options[:prompt] input = $stdin.gets.chomp raise 'Poll failure from stdin' if input.nil? break unless @channel.active? break if execute_command(input).is_a?(AbstractChannel::CloseFuture) $stdout.puts websocket.gets end end |
#session ⇒ Object
179 180 181 182 183 184 |
# File 'lib/websocket_client.rb', line 179 def session when_client_has_shut_down(@client_group) do |group| log.debug "Channel group has shut down: #{group.inspect}" end @user_app.nil? ? read_user_commands : invoke_user_app end |
#shut_down_callbacks ⇒ Object
192 193 194 |
# File 'lib/websocket_client.rb', line 192 def shut_down_callbacks @shut_down_callbacks ||= [] end |
#shutdown ⇒ Object
168 169 170 171 172 173 |
# File 'lib/websocket_client.rb', line 168 def shutdown log.debug 'Shutting down gracefully' @client_group&.shutdownGracefully() ensure client_has_shut_down end |
#when_client_has_shut_down(*args, &block) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/websocket_client.rb', line 196 def when_client_has_shut_down(*args, &block) shut_down_callbacks << { block: block, args: args } self end |