Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

Introduction to the

In an era of rapidly increasing Internet speeds, browsers have become our gateway to all kinds of services, and it’s hard to imagine how our web world would function without them. Now I want to move the operating system to the browser. However, not all applications need a browser to execute, such as the communication between servers, you need to use a custom client to interact with the server.

This article will introduce the principle and concrete implementation of using Netty client to connect websocket.

Browser client

Before introducing the Netty client, let’s look at a simple example of a browser client connecting to a Websocket:

// create connection const socket = new WebSocket('ws://localhost:8000'); Socket. addEventListener('open', function (event) {socket.send(' Yes, yes! '); }); // Listen for messages socket.adDeventListener ('message', function (event) {console.log(' listen to server messages ', event.data); });Copy the code

Here uses javascript, the most common language of the browser, and uses the WebSocket API provided by the browser to operate, very simple.

Is using netty client to connect to Websocket the same as using javascript? Let’s explore.

Netty support for Websocket clients

Take a look at netty’s webSocket support classes and see how to use them in detail.

WebSocketClientHandshaker

Like the websocket server, client is the core of the class handshaker, called WebSocketClientHandshaker here. What does this class do? Take a look.

This class mainly implements the handshake between the client and server.

Let’s look at the constructor class for its longest argument:

   protected WebSocketClientHandshaker(URI uri, WebSocketVersion version, String subprotocol,
                                        HttpHeaders customHeaders, int maxFramePayloadLength,
                                        long forceCloseTimeoutMillis, boolean absoluteUpgradeUrl) 
Copy the code

The parameter contains the WEBSocket connection URI, such as: “ws://flydean.com/mypath”.

The request subprotocol type is subprotocol. HTTP headers: customHeaders has the maximum frame payload length. MaxFramePayloadLength, with mandatory timeout to close, and URI address to upgrade using HTTP.

How do I create handshaker? Similarly, netty provides a WebSocketClientHandshakerFactory method.

WebSocketClientHandshakerFactory provides a newHandshaker method, can easily create a variety of different versions of handshaker:

        if (version == V13) {
            return new WebSocketClientHandshaker13(
                    webSocketURL, V13, subprotocol, allowExtensions, customHeaders,
                    maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis);
        }
        if (version == V08) {
            return new WebSocketClientHandshaker08(
                    webSocketURL, V08, subprotocol, allowExtensions, customHeaders,
                    maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis);
        }
        if (version == V07) {
            return new WebSocketClientHandshaker07(
                    webSocketURL, V07, subprotocol, allowExtensions, customHeaders,
                    maxFramePayloadLength, performMasking, allowMaskMismatch, forceCloseTimeoutMillis);
        }
        if (version == V00) {
            return new WebSocketClientHandshaker00(
                    webSocketURL, V00, subprotocol, customHeaders, maxFramePayloadLength, forceCloseTimeoutMillis);
        }
Copy the code

As you can see, depending on the version of the protocol passed in, Can be divided into WebSocketClientHandshaker13, WebSocketClientHandshaker08, WebSocketClientHandshaker07, WebSocketClientHandshaker00 this a few kinds.

WebSocketClientCompressionHandler

Generally speaking, for webSocket protocol, in order to improve the transmission performance and speed, reduce the network bandwidth usage, usually in the use of additional compression extension. To handle such compression extensions, Netty provides both server-side and client-side support.

The corresponding handler is called WebSocketServerCompressionHandler for server side, the corresponding handler is called WebSocketClientCompressionHandler for the client.

Support for compression protocol extensions in WebSocket can be achieved by adding these two handlers to the corresponding pipline.

There are two levels of extension to the protocol called permessage-deflate and perframe-deflate, Corresponding PerMessageDeflateClientExtensionHandshaker and DeflateFrameClientExtensionHandshaker respectively.

As for the specific compression, here is not detailed explanation, interested partners can understand.

Netty client processing flow

After looking at Netty’s support for webSocket clients, this section explains how Netty uses these tools for message processing.

The first step is to follow the normal logic to create the Bootstrap client and add the handler. The handler in this case is a client-side handler specifically tailored for WebSocket.

In addition to the above mentioned WebSocketClientCompressionHandler, is the custom handler.

In the custom handler, we need to handle two things, one of which is to create the handshaker when the channel is ready. The other thing is the handling of specific Websocket messages.

Create handshaker

The first to use WebSocketClientHandshakerFactory create handler:

TestSocketClientHandler handler =
     new TestSocketClientHandler(
        WebSocketClientHandshakerFactory.newHandshaker(
              uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));

Copy the code

Then use handshaker for handshake connection while channel active:

    public void channelActive(ChannelHandlerContext ctx) {
        handshaker.handshake(ctx.channel());
    }
Copy the code

Then in news receiving process also need to determine whether handshaker state is complete, if not completed calls handshaker. FinishHandshake method is done manually:

if (! handshaker.isHandshakeComplete()) { try { handshaker.finishHandshake(ch, (FullHttpResponse) msg); Log.info (" Websocket Handshake done!" ); handshakeFuture.setSuccess(); {the} the catch (WebSocketHandshakeException e) info (" connection failed!" ); handshakeFuture.setFailure(e); } return; }Copy the code

Once the handshake is complete, it is ready to read and write webSocket messages.

Processing of WebSocket messages

The message processing of WebSocket is relatively simple. You can convert the received message into a WebSocketFrame for processing.

WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; Log.info (" Received TXT message: "+ textframe.text ()); } else if (frame Instanceof PongWebSocketFrame) {log.info(" Received pong message "); } else if (frame instanceof CloseWebSocketFrame) {log.info(" Received closing message "); ch.close(); }Copy the code

conclusion

This article explains netty websocket client support and specific docking process, we can once again on the basis of expansion, to achieve their own business logic.

Learn -netty4 for an example of this article

This article is available at www.flydean.com/25-netty-we…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!