Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the long connection between client and server in Netty

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What this article shares to you is about how to achieve a long connection between the client and the server in Netty. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Netty realizes the long connection between the server and the client through webSocket, and Tomcat8,Spring,Netty provides support for websocket, realizing a real long connection!

Let's experience this long connection through a simple implementation!

Before experiencing, let's talk about the relationship between Http and websocket:

Http is a stateless, collaborative responsive link. The so-called statelessness is reflected in that the server cannot track which client the root message comes from, and saves the message in the form of cookei,session.

In web chat, short-time links are achieved in the form of polling and commit, which is inefficient and the most important thing is that it can not be real-time, that is, quasi-real-time.

The way of continuous link is reflected in that when the server has no result, the client cannot perceive it, and it is a waste of resources and bandwidth.

Websocket realizes a real long connection between the client and the server, saving bandwidth and focusing on the content itself.

Websocket:1--: bidirectional data transfer

2Murray: based on Http

3Murray: non-browser situations

Let's experience it through the running of the program!

Establish the server end!

We found that both the support for Http and the socket connection set up and run in the same way, adding handler (new LoggingHnadler) on the server side to print logs.

Binding port number 8899 new InetSocketAddress is equivalent to direct binding 8899

Establish serverInnitializer

The ws here is interpreted as websocketPath, that is, pathcoment on request

For example, this time: ws://localhost:8899/ws

Ws refers to the ws at the end of the text, while the ws at the beginning is a fixed websocket way of writing.

HttpObjectAggregator is an aggregation process. Click in to have a look.

Create an example, the text length of the parameter max, can be aggregated bytes, equivalent to the concept of segmentation

Create your own handler

In order to experience the real-time performance of the message, print the system time with localdateTime.now ()

Here let's take a look at the source code of asLongText

ChannelId inherits serializable

AsShortText: returns a short text but not the only channelId'

Where asLongText is: returns a long text but a unique channelId'

Websocket supports the operation of Html or third-party libraries. Let's write the page.

A text box that sends data, and a text box that receives data

Js part

Let's start the server first.

Restart Html

When the link is open, it shows that the link is open! Let's look at the changes on the server side.

The handleradd method is called when the link is established, and a string similar to UUID is printed

Let's send data to experience.

Let's compare it with the normal corresponding content, let's pay attention to the time.

2019-01-20T13:09:14.322

2019-01-20T13:09:14.321

Arrive within millisecond deviation!

We open the developer tools and refresh the browser.

At the same time, server-side changes:

Test response code 200. there's nothing to say.

Let's take another look at ws:

Make a comparison

In addition to the special 101response code (Switching), we see that the frame (frame) is also reflected in the message.

And upgrade (promotion): websocket

This reflects the process of upgrading from Http to websocket!

Prepare to disconnect

The above is how to achieve the long-term connection between the client and the server in Netty. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report