Netty測(cè)試程序

2018-08-08 10:57 更新

使用下面命令啟動(dòng)服務(wù)器:

mvn -PChatServer clean package exec:exec

其中項(xiàng)目中的 pom.xml 是配置了 9999 端口。你也可以通過(guò)下面的方法修改屬性

mvn -PChatServer -Dport=1111 clean package exec:exec

下面是控制臺(tái)的主要輸出(刪除了部分行)

Listing 11.5 Compile and start the ChatServer

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ChatServer 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ netty-in-action ---
[INFO] Building jar: D:/netty-in-action/chapter11/target/chat-server-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ chat-server ---
Starting ChatServer on port 9999

可以在瀏覽器中通過(guò) http://localhost:9999 地址訪問(wèn)程序。圖11.5展示了此程序在Chrome瀏覽器下的用戶界面。

Figure 11.5 WebSockets ChatServer demonstration

Figure%2011

圖中顯示了兩個(gè)已經(jīng)連接了的客戶端。第一個(gè)客戶端是通過(guò)上面的圖形界面連接的,第二個(gè)是通過(guò)Chrome瀏覽器底部的命令行連接的。 你可以注意到,這兩個(gè)客戶端都在發(fā)送消息,每條消息都會(huì)顯示在兩個(gè)客戶端上。

如何加密?

在實(shí)際場(chǎng)景中,加密是必不可少的。在Netty中實(shí)現(xiàn)加密并不麻煩,你只需要向 ChannelPipeline 中添加 SslHandler ,然后配置一下即可。如下:

Listing 11.6 Add encryption to the ChannelPipeline

public class SecureChatServerIntializer extends ChatServerInitializer {    //1
    private final SslContext context;

    public SecureChatServerIntializer(ChannelGroup group, SslContext context) {
        super(group);
        this.context = context;
    }

    @Override
    protected void initChannel(Channel ch) throws Exception {
        super.initChannel(ch);
        SSLEngine engine = context.newEngine(ch.alloc());
        engine.setUseClientMode(false);
        ch.pipeline().addFirst(new SslHandler(engine)); //2
    }
}

1.擴(kuò)展 ChatServerInitializer 來(lái)實(shí)現(xiàn)加密

2.向 ChannelPipeline 中添加SslHandler

最后修改 ChatServer,使用 SecureChatServerInitializer 并傳入 SSLContext

Listing 11.7 Add encryption to the ChatServer

public class SecureChatServer extends ChatServer {//1

    private final SslContext context;

    public SecureChatServer(SslContext context) {
        this.context = context;
    }

    @Override
    protected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {
        return new SecureChatServerIntializer(group, context);    //2
    }

    public static void main(String[] args) throws Exception{
        if (args.length != 1) {
            System.err.println("Please give port as argument");
            System.exit(1);
        }
        int port = Integer.parseInt(args[0]);
        SelfSignedCertificate cert = new SelfSignedCertificate();
        SslContext context = SslContext.newServerContext(cert.certificate(), cert.privateKey());
        final SecureChatServer endpoint = new SecureChatServer(context);
        ChannelFuture future = endpoint.start(new InetSocketAddress(port));

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                endpoint.destroy();
            }
        });
        future.channel().closeFuture().syncUninterruptibly();
    }
}

1.擴(kuò)展 ChatServer

2.返回先前創(chuàng)建的 SecureChatServerInitializer 來(lái)啟用加密

這樣,就在所有的通信中使用了 SSL/TLS 加密。和前面一樣,你可以使用Maven拉取應(yīng)用需要的所有依賴,并啟動(dòng)它,如下所示。

Listing 11.8 Start the SecureChatServer

$ mvn -PSecureChatServer clean package exec:exec
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ChatServer 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ netty-in-action ---
[INFO] Building jar: D:/netty-in-action/chapter11/target/chat-server-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ chat-server ---
Starting SecureChatServer on port 9999

現(xiàn)在你可以通過(guò) HTTPS 地址: https://localhost:9999 來(lái)訪問(wèn)SecureChatServer 了。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)