In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how tio-core-spring-boot-starter integrates tio into the springboot project. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
First, you need to introduce tio-core-spring-boot-starter components into pom.xml
Writing server programs with org.t-io tio-core-spring-boot-starter 3.3.5.v20190712-RELEASE
First, add @ EnableTioServerServer annotation to the SpringBoot Application main class
@ SpringBootApplication@EnableTioServerServerpublic class TioServerApplication {public static void main (String [] args) {SpringApplication.run (TioServerApplication.class, args);}}
Second, next, modify the configuration file
Tio: core: server: # websocket port default 9876 port: 6789 # heartbeat time heartbeat-timeout: 60000 # Cluster configuration is off by default cluster: enabled: false # Cluster is implemented through redis's Pub/Sub So you need to configure Redis redis: ip: 127.0.0.1 port: 6379 all: true group: true ip: true # SSL configuration ssl: enabled: false key-store: password: trust-store:
Third, write message processing classes
/ * * message handling handler is enabled by adding {@ link TioServerMsgHandler} annotation, otherwise it will not be enabled * Note: handler must be enabled, otherwise the startup will throw a {@ link TioMsgHandlerNotFoundException} exception * * @ author yangjian * / @ TioServerMsgHandlerpublic class HelloServerMsgHandler implements ServerAioHandler {/ * decode the received ByteBuffer Decoded into business message packets that can be recognized by the application * total message structure: message header + message body * message header structure: 4 bytes Length of the stored message body * message body structure: byte [] * / @ Override public HelloPacket decode (ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException {return PacketUtil.decode (buffer, limit, position, readableLength, channelContext) of the object's json string } / * Encoding: encode business message packets into ByteBuffer that can be sent * Total message structure: message header + message body * message header structure: 4 bytes Length of the stored message body * message body structure: byte [] * / @ Override public ByteBuffer encode (Packet packet, GroupContext groupContext, ChannelContext channelContext) {return PacketUtil.encode (packet, groupContext, channelContext) of the json string of the object } / * handle messages * / @ Override public void handler (Packet packet, ChannelContext channelContext) throws Exception {HelloPacket helloPacket = (HelloPacket) packet; byte [] body = helloPacket.getBody (); if (body! = null) {String str = new String (body, HelloPacket.CHARSET); System.out.println ("received message:" + str) HelloPacket resppacket = new HelloPacket (); resppacket.setBody (("received your message, your message is:" + str) .getBytes (HelloPacket.CHARSET); Tio.send (channelContext, resppacket);} return;}}
Fourth, implement the message entity package
/ * message packet entity * * @ author yangjian * / public class HelloPacket extends Packet {private static final long serialVersionUID =-172060606924066412L; public static final int HEADER_LENGTH = 4 the length of the header public static final String CHARSET = "utf-8"; private byte [] body / * * @ return the body * / public byte [] getBody () {return body;} / * @ param body the body to set * / public void setBody (byte [] body) {this.body = body;}}
Next, start the server.
Write client programs
The client uses the regular program of Tio to start, and there are only three files, so it is very simple to start.
First, write constant classes
Public interface Const {/ * server address * / public static final String SERVER = "127.0.0.1"; / * * listening port * / public static final int PORT = 6789; / * * heartbeat timeout * / public static final int TIMEOUT = 5000;}
Message processing class
Public class HelloClientAioHandler implements ClientAioHandler {private static HelloPacket heartbeatPacket = new HelloPacket () / * Decoding: decode the received ByteBuffer into a business message packet recognized by the application * Total message structure: message header + message body * message header structure: 4 bytes Length of the stored message body * message body structure: byte [] * / @ Override public HelloPacket decode (ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws AioDecodeException {return PacketUtil.decode (buffer, limit, position, readableLength, channelContext) of the object's json string } / * Encoding: encode business message packets into ByteBuffer that can be sent * Total message structure: message header + message body * message header structure: 4 bytes Length of the stored message body * message body structure: byte [] * / @ Override public ByteBuffer encode (Packet packet, GroupContext groupContext, ChannelContext channelContext) {return PacketUtil.encode (packet, groupContext, channelContext) of the json string of the object } / * processing messages * / @ Override public void handler (Packet packet, ChannelContext channelContext) throws Exception {HelloPacket helloPacket = (HelloPacket) packet; byte [] body = helloPacket.getBody () If (body! = null) {String str = new String (body, HelloPacket.CHARSET); System.out.println ("received message:" + str);} return;} / * if this method returns null, the heartbeat will not occur at the framework level If non-null is returned, the framework layer will regularly send the message packet * / @ Override public HelloPacket heartbeatPacket (ChannelContext channelContext) {return heartbeatPacket;} returned by this method.
III. Client startup class
Public class HelloClientStarter {/ / server node public static Node serverNode = new Node (Const.SERVER, Const.PORT); / / handler, including encoding, decoding, and message processing public static ClientAioHandler tioClientHandler = new HelloClientAioHandler (); / / event listener, which can be null, but it is recommended to implement the API yourself. You can refer to showcase for some interfaces public static ClientAioListener aioListener = null / / set to null private static ReconnConf reconnConf = new ReconnConf (5000L) if you don't want to connect automatically after being disconnected; / / A group of context objects shared by the connection public static ClientGroupContext clientGroupContext = new ClientGroupContext (tioClientHandler, aioListener, reconnConf); public static TioClient tioClient = null; public static ClientChannelContext clientChannelContext = null / * launch program entry * / public static void main (String [] args) throws Exception {clientGroupContext.setHeartbeatTimeout (Const.TIMEOUT); tioClient = new TioClient (clientGroupContext); clientChannelContext = tioClient.connect (serverNode); / / after connecting, send a message to play send () } private static void send () throws Exception {HelloPacket packet = new HelloPacket (); packet.setBody ("hello world" .getBytes (HelloPacket.CHARSET)); Tio.send (clientChannelContext, packet);}}
Start the client side and view the terminal output.
Server output
Native callback interface support
Like handler, the usage of other native callback APIs remains the same, and you only need to add the corresponding annotation to the corresponding implementation class to OK.
/ / the main logic processing class must be written, otherwise public class HelloServerMsgHandler implements ServerAioHandler {} / / can not be written, and can be enabled by adding @ TioServerAioListener annotation, otherwise public class HelloServerAioListener implements ServerAioListener {} / can not be enabled, but can be enabled by adding @ TioServerGroupListener annotation, otherwise public class HelloServerGroupListener implements GroupListener {} / / will not be enabled, otherwise public class HelloServerIpStatListener implements IpStatListener {} will not be enabled
Note here: each corresponding callback interface needs to be enabled manually by adding comments, otherwise it will not be enabled by default and will not be scanned automatically
Active push on server side
This is also very simple, as long as you get the TioServerBootstrap, everything else becomes very simple.
@ RestControllerpublic class HelloController {static Logger logger = LoggerFactory.getLogger (HelloController.class); @ Autowired private TioServerBootstrap bootstrap; @ GetMapping ("/") public String index () {return "Hello, tio-spring-boot-starter!!" } / * push messages to the client * @ throws Exception * / @ GetMapping ("/ push") public String pushMessage () throws Exception {HelloPacket packet = new HelloPacket (); packet.setBody ("This message is pushed by Tio Server." .getBytes (HelloPacket.CHARSET)) Tio.sendToAll (bootstrap.getServerGroupContext (), packet); logger.info ("Push a message to client successfully"); return "Push a message to client successfully";}}
Client output screenshot
SSL supports # SSL configuration ssl: enabled: true key-store: key-store path password: password trust-store: trust-store path Cluster support # Cluster configuration is off by default cluster: enabled: false # Cluster is implemented through redis's Pub/Sub So you need to configure Redis redis: ip: 127.0.0.1 port: 6379 all: true group: true ip: true user: truetio-core-spring-boot-starter source code address
Https://github.com/tywo45/t-io/tree/master/src/zoo/spring-boot/tio-core/src/main/java/org/tio/core/starter
After reading the above, do you have any further understanding of how tio-core-spring-boot-starter integrates tio into the springboot project? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.