Class: WebSocket::FrameHandler

Inherits:
SimpleChannelInboundHandler
  • Object
show all
Includes:
ChannelFutureListener, ChannelHandler
Defined in:
lib/websocket/frame_handler.rb

Overview

The FrameHandler class implements a handler for incoming WebSocket request messages. This handler invokes a #handle_message method which should be implemented by a user provided subclass. The MessageHandler class is an example of one such subclass.

Direct Known Subclasses

MessageHandler

Constant Summary collapse

UnsupportedFrameTypeErrorTemplate =
'%<handler>s encountered unsupported frame type: %<frame>s'.freeze

Instance Method Summary collapse

Constructor Details

#initializeFrameHandler

Returns a new instance of FrameHandler.



34
35
36
# File 'lib/websocket/frame_handler.rb', line 34

def initialize
  super(WebSocketFrame.java_class)
end

Instance Method Details

#channelRead0(ctx, frame) ⇒ Object

Please keep in mind that this method will be renamed to messageReceived(ChannelHandlerContext, I) in 5.0.

java_signature ‘protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception’



42
43
44
# File 'lib/websocket/frame_handler.rb', line 42

def channelRead0(ctx, frame)
  messageReceived(ctx, frame)
end

#handle_message(ctx, msg) ⇒ Object



46
47
48
49
# File 'lib/websocket/frame_handler.rb', line 46

def handle_message(ctx, msg)
  # Send the uppercase string back.
  ctx.channel.writeAndFlush(TextWebSocketFrame.new(msg.to_s.strip.upcase))
end

#messageReceived(ctx, frame) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/websocket/frame_handler.rb', line 57

def messageReceived(ctx, frame)
  # ping and pong frames already handled
  case frame
  when TextWebSocketFrame
    unsupported_frame(frame, self) unless frame.respond_to?(:text)
    handle_message(ctx, frame.text)
  when FullHttpRequest
    # Let another handler handle it.
  else unsupported_frame(frame, self)
  end
end

#unsupported_frame(frame, handler) ⇒ Object

Raises:

  • (java.lang.UnsupportedOperationException)


51
52
53
54
55
# File 'lib/websocket/frame_handler.rb', line 51

def unsupported_frame(frame, handler)
  raise java.lang.UnsupportedOperationException, format(
    UnsupportedFrameTypeErrorTemplate, handler: handler.class, frame: frame.class
  )
end