Class: WebSocket::MessageHandler

Inherits:
FrameHandler show all
Includes:
ChannelFutureListener, ChannelHandler
Defined in:
lib/websocket/message_handler.rb

Overview

The MessageHandler class provides a method for specifying code to handle incoming messages and respond with the results from the given code.

Constant Summary

Constants inherited from FrameHandler

FrameHandler::UnsupportedFrameTypeErrorTemplate

Instance Method Summary collapse

Methods inherited from FrameHandler

#channelRead0, #messageReceived, #unsupported_frame

Constructor Details

#initialize(&handler) ⇒ MessageHandler

Returns a new instance of MessageHandler.



31
32
33
34
# File 'lib/websocket/message_handler.rb', line 31

def initialize(&handler)
  super()
  @handler = handler
end

Instance Method Details

#handle_message(ctx, message) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/websocket/message_handler.rb', line 36

def handle_message(ctx, message)
  request = message&.to_s&.strip
  return if request.nil?
  response = @handler.call(ctx, request)
  return if response.nil?
  log.debug "#{self.class}#handle_message response: #{response.chomp}"
  ctx.channel.writeAndFlush(TextWebSocketFrame.new(response))
  response
end