Tio. Introduction to Java
- In order to reduce the time for users to find APIs, t-io collects common APIs into a class in the form of static methods, which is TiO java
- Tio.java itself does not implement complex services, and the business implementations are still distributed in other classes, TiO Java just centralizes the API s that users care about, which is easy to find with the IDE
- Please refer to: https://www.tiocloud.com/doc/tio/245
Business data binding
- Resource binding refers to associating business-related data with Tcp connection (i.e. ChannelContext). For example, ChannelContext-A represents user Zhang San. Zhang San's userid is 333, so bind with the following code
Tio.bindUser(ChannelContext-A, "333") t-io currently has four built-in resource bindings. Of course, users can use these bindings flexibly to solve all resource binding problems in the business layer. For example, group can be prefixed with "ios -" to mark that the user is using ios, such as
Tio.bindGroup(ChannelContext-A, "333"); Tio.bindGroup(ChannelContext-A, "ios-" + "333");
- Among the four built-in resource binding methods, one ChannelContext can be bound to multiple groupid s, and the other three bindings are one-to-one or many to one, that is, a ChannelContext can belong to group-a, group-b... group-n at the same time
Next, post the source code of these four resource bindings
/** * Binding service id * @param channelContext * @param bsId * @author tanyaowu */ public static void bindBsId(ChannelContext channelContext, String bsId) { channelContext.tioConfig.bsIds.bind(channelContext, bsId); } /** * Bind group * @param channelContext * @param group * @author tanyaowu */ public static void bindGroup(ChannelContext channelContext, String group) { channelContext.tioConfig.groups.bind(group, channelContext); } /** * Binding token * @param channelContext * @param token * @author tanyaowu */ public static void bindToken(ChannelContext channelContext, String token) { channelContext.tioConfig.tokens.bind(token, channelContext); } /** * Bind user * @param channelContext * @param userid * @author tanyaowu */ public static void bindUser(ChannelContext channelContext, String userid) { channelContext.tioConfig.users.bind(userid, channelContext); }
Business data unbinding
Since there is binding, there must be unbinding. This is the opposite operation to binding, so don't say much and directly post the short source code
/** * Unbind service id * @param channelContext * @author tanyaowu */ public static void unbindBsId(ChannelContext channelContext) { channelContext.tioConfig.bsIds.unbind(channelContext); } /** * Unbind all groups * @param channelContext * @author tanyaowu */ public static void unbindGroup(ChannelContext channelContext) { channelContext.tioConfig.groups.unbind(channelContext); } /** * Unbind from specified group * @param group * @param channelContext * @author tanyaowu */ public static void unbindGroup(String group, ChannelContext channelContext) { channelContext.tioConfig.groups.unbind(group, channelContext); } /** * token to unbind channelContext * @param channelContext * @author tanyaowu */ public static void unbindToken(ChannelContext channelContext) { channelContext.tioConfig.tokens.unbind(channelContext); } // org.tio.core.TioConfig.ipBlacklist /** * userid to unbind channelContext * @param channelContext * @author tanyaowu */ public static void unbindUser(ChannelContext channelContext) { channelContext.tioConfig.users.unbind(channelContext); } /** * Unbind userid. It is generally used to log in multiple places and kick out the previous login scenario * @param tioConfig * @param userid * @author: tanyaowu */ public static void unbindUser(TioConfig tioConfig, String userid) { tioConfig.users.unbind(tioConfig, userid); }
Asynchronous transmission
- Asynchronous sending means that the business layer returns immediately after dropping the Packet to t-io. When it returns, the Packet is not sent, but only submitted to the queue to be sent
- Asynchronous sending starts with send. There are a lot of API s and the posting code is a little long. It's more comfortable to show it with pictures
Blocking transmission - Blocking sending: t-io sends the Packet to the other party before returning
- Blocking sending starts with bSend. There are a lot of API s and the posting code is a little long. It's more comfortable to show it with pictures
How to get ChannelContext - An important purpose of the previous business data binding is to obtain the ChannelContext according to those business IDs. For example, if you bind a userid, you can obtain the ChannelContext through this userid later
- The APIs for obtaining ChannelContext start with get. This API is a little more, and the paste code is a little longer. It's more comfortable to show it with pictures
Disconnect and remove connections - Disconnect is a method that starts with close, which means to disconnect the currently connected TCP connection
- Removing a connection is a method that starts with remove, which means discarding the connection completely
be careful - If t-io is used as TCP server
The above two methods are equivalent because there is no reconnection on the server - If t-io is used as the TCP client and reconnection is not configured
The above two methods are also equivalent, because reconnection is not required. After the tio disconnects, all resources of the connection will be released - If t-io is used as TCP client and reconnection rules are configured, the above two methods are different. The differences are as follows
close(): the related resources are not released, and reconnection is performed
remove(): completely release related resources and do not reconnect
Cluster notification - T-io has built-in clustering function. As an IO framework not related to business, built-in clustering function is laborious but not pleasing (why not pleasing? For example, it will increase code complexity and reduce the score ranking of TiO MVC on TFB). However, considering the voice of the majority of users, t-io has built-in clustering function
- At present, the message sending function provided by t-io supports clusters, which starts from tio You can see it in the Java source code
- The cluster notification API starts with notifyCluster, as shown in the figure below
t-io pull Black IP Simple to the extreme, just one line of code
Tio.IpBlacklist.add(tioConfig, channelContext.getClientNode().getIp());