use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project sockjs-netty by cgbystrom.
the class WebSocketClient method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (!wsHandshaker.isHandshakeComplete()) {
wsHandshaker.finishHandshake(e.getChannel(), (HttpResponse) e.getMessage());
callback.onOpen(WebSocketClient.this);
return;
}
if (e.getMessage() instanceof TextWebSocketFrame) {
TextWebSocketFrame wf = (TextWebSocketFrame) e.getMessage();
String frame = wf.getText();
char frameType = frame.charAt(0);
if (!sockJsHandshakeDone) {
if (frameType == 'o') {
sockJsHandshakeDone = true;
} else {
throw new IllegalStateException("Expected open frame 'o' as first frame. Got " + frame);
}
return;
}
switch(frameType) {
case 'h':
break;
case 'a':
try {
List<String> messages = objectMapper.readValue(frame.substring(1), List.class);
for (String msg : messages) {
try {
callback.onMessage(msg);
} catch (Exception ex) {
callback.onError(ex);
}
}
} catch (Exception ex) {
throw new IllegalArgumentException("Unable to decode frame: " + frame);
}
break;
case 'c':
disconnect();
break;
default:
throw new IllegalArgumentException("Received unknown frame type '" + frameType + "'");
}
}
}
use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project sockjs-netty by cgbystrom.
the class WebSocketClient method send.
@Override
public void send(String message) {
ChannelBuffer cb = Frame.messageFrame(new SockJsMessage(message)).getData();
// Skip the framing char
cb.readerIndex(1);
channel.write(new TextWebSocketFrame(cb));
}
use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project load-balancer by RestComm.
the class HttpRequestHandler method handleWebSocketFrame.
private void handleWebSocketFrame(ChannelHandlerContext ctx, final MessageEvent e) {
Object msg = e.getMessage();
final String request = ((TextWebSocketFrame) msg).getText();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s received WebSocket request %s", ctx.getChannel().getId(), request));
}
// Modify the Client Pipeline - Phase 2
Channel channel = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel())).getChannel();
ChannelPipeline p = channel.getPipeline();
websocketServerPipelineFactory.upgradeClientPipelineFactoryPhase2(p, wsVersion);
if (channel != null) {
channel.write(new TextWebSocketFrame(request));
}
}
use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project load-balancer by RestComm.
the class HttpResponseHandler method handleWebSocketFrame.
private void handleWebSocketFrame(ChannelHandlerContext ctx, MessageEvent e) {
Object msg = e.getMessage();
final String response = ((TextWebSocketFrame) msg).getText();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s received WebSocket response %s", ctx.getChannel().getId(), response));
}
Channel channel = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel())).getChannel();
if (channel != null) {
channel.write(new TextWebSocketFrame(response));
}
}
Aggregations