-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add websocket multiprotocol support (http1, http2) (#17)
- Loading branch information
1 parent
70998e9
commit f1dce3d
Showing
19 changed files
with
1,629 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ subprojects { | |
|
||
repositories { | ||
mavenCentral() | ||
mavenLocal() | ||
} | ||
|
||
plugins.withType(JavaPlugin) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
./gradlew netty-websocket-http2-example:runMultiProtocolCallbacksServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
./gradlew netty-websocket-http2-example:runMultiProtocolDefaultServer |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...etty/handler/codec/http2/websocketx/example/multiprotocol/server/callbackscodec/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.jauntsdn.netty.handler.codec.http2.websocketx.example.multiprotocol.server.callbackscodec; | ||
|
||
import com.jauntsdn.netty.handler.codec.http.websocketx.WebSocketCallbacksHandler; | ||
import com.jauntsdn.netty.handler.codec.http.websocketx.WebSocketFrameListener; | ||
import com.jauntsdn.netty.handler.codec.http2.websocketx.Http2WebSocketEvent.Http2WebSocketHandshakeSuccessEvent; | ||
import com.jauntsdn.netty.handler.codec.http2.websocketx.example.Security; | ||
import com.jauntsdn.netty.handler.codec.websocketx.multiprotocol.MultiProtocolWebSocketServerHandler; | ||
import com.jauntsdn.netty.handler.codec.websocketx.multiprotocol.MultiprotocolWebSocketServerBuilder; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler.HandshakeComplete; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslHandler; | ||
import io.netty.util.ReferenceCountUtil; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Main { | ||
static final Logger logger = LoggerFactory.getLogger(Main.class); | ||
|
||
public static void main(String[] args) throws Exception { | ||
String host = "localhost"; | ||
int port = 8099; | ||
SslContext sslContext = Security.serverSslContext("localhost.p12", "localhost"); | ||
|
||
Channel server = | ||
new ServerBootstrap() | ||
.group(new NioEventLoopGroup()) | ||
.channel(NioServerSocketChannel.class) | ||
.childHandler( | ||
new ChannelInitializer<SocketChannel>() { | ||
|
||
@Override | ||
protected void initChannel(SocketChannel ch) { | ||
SslHandler sslHandler = sslContext.newHandler(ch.alloc()); | ||
MultiProtocolWebSocketServerHandler multiprotocolHandler = | ||
MultiprotocolWebSocketServerBuilder.create() | ||
.path("/echo") | ||
.subprotocols("echo.jauntsdn.com") | ||
.callbacksCodec() | ||
.handler( | ||
new ChannelInitializer<Channel>() { | ||
@Override | ||
protected void initChannel(Channel ch) { | ||
ch.pipeline().addLast(new CallbacksServerHandler()); | ||
} | ||
}) | ||
.build(); | ||
ch.pipeline().addLast(sslHandler, multiprotocolHandler); | ||
} | ||
}) | ||
.bind(host, port) | ||
.sync() | ||
.channel(); | ||
logger.info("\n==> Websocket server (callbacks codec) is listening on {}:{}", host, port); | ||
server.closeFuture().await(); | ||
} | ||
|
||
private static class CallbacksServerHandler extends ChannelInboundHandlerAdapter { | ||
|
||
@Override | ||
public void userEventTriggered(ChannelHandlerContext c, Object evt) throws Exception { | ||
if (evt instanceof HandshakeComplete || evt instanceof Http2WebSocketHandshakeSuccessEvent) { | ||
WebSocketCallbacksHandler.exchange( | ||
c, | ||
(ctx, webSocketFrameFactory) -> | ||
new WebSocketFrameListener() { | ||
@Override | ||
public void onChannelRead( | ||
ChannelHandlerContext context, | ||
boolean finalFragment, | ||
int rsv, | ||
int opcode, | ||
ByteBuf payload) { | ||
ByteBuf textFrame = | ||
webSocketFrameFactory.mask( | ||
webSocketFrameFactory.createTextFrame( | ||
ctx.alloc(), payload.readableBytes())); | ||
textFrame.writeBytes(payload); | ||
payload.release(); | ||
ctx.write(textFrame); | ||
} | ||
|
||
@Override | ||
public void onChannelReadComplete(ChannelHandlerContext ctx1) { | ||
ctx1.flush(); | ||
} | ||
|
||
@Override | ||
public void onExceptionCaught(ChannelHandlerContext ctx1, Throwable cause) { | ||
if (cause instanceof IOException) { | ||
return; | ||
} | ||
logger.error("Unexpected websocket error", cause); | ||
ctx1.close(); | ||
} | ||
}); | ||
} | ||
super.userEventTriggered(c, evt); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) { | ||
ReferenceCountUtil.safeRelease(msg); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
.../netty/handler/codec/http2/websocketx/example/multiprotocol/server/defaultcodec/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.jauntsdn.netty.handler.codec.http2.websocketx.example.multiprotocol.server.defaultcodec; | ||
|
||
import com.jauntsdn.netty.handler.codec.http2.websocketx.example.Security; | ||
import com.jauntsdn.netty.handler.codec.websocketx.multiprotocol.MultiProtocolWebSocketServerHandler; | ||
import com.jauntsdn.netty.handler.codec.websocketx.multiprotocol.MultiprotocolWebSocketServerBuilder; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; | ||
import io.netty.handler.ssl.SslContext; | ||
import io.netty.handler.ssl.SslHandler; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Main { | ||
static final Logger logger = LoggerFactory.getLogger(Main.class); | ||
|
||
public static void main(String[] args) throws Exception { | ||
String host = "localhost"; | ||
int port = 8099; | ||
SslContext sslContext = Security.serverSslContext("localhost.p12", "localhost"); | ||
|
||
Channel server = | ||
new ServerBootstrap() | ||
.group(new NioEventLoopGroup()) | ||
.channel(NioServerSocketChannel.class) | ||
.childHandler( | ||
new ChannelInitializer<SocketChannel>() { | ||
|
||
@Override | ||
protected void initChannel(SocketChannel ch) { | ||
SslHandler sslHandler = sslContext.newHandler(ch.alloc()); | ||
MultiProtocolWebSocketServerHandler multiprotocolHandler = | ||
MultiprotocolWebSocketServerBuilder.create() | ||
.path("/echo") | ||
.subprotocols("echo.jauntsdn.com") | ||
.defaultCodec() | ||
.handler(new DefaultEchoWebSocketHandler()) | ||
.build(); | ||
ch.pipeline().addLast(sslHandler, multiprotocolHandler); | ||
} | ||
}) | ||
.bind(host, port) | ||
.sync() | ||
.channel(); | ||
logger.info("\n==> Websocket server (default codec) is listening on {}:{}", host, port); | ||
server.closeFuture().await(); | ||
} | ||
|
||
@ChannelHandler.Sharable | ||
private static class DefaultEchoWebSocketHandler | ||
extends SimpleChannelInboundHandler<TextWebSocketFrame> { | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame webSocketFrame) { | ||
ctx.write(webSocketFrame.retain()); | ||
} | ||
|
||
@Override | ||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { | ||
ctx.flush(); | ||
super.channelReadComplete(ctx); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
if (cause instanceof IOException) { | ||
return; | ||
} | ||
logger.error("Unexpected websocket error", cause); | ||
ctx.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.