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 message Authentication Mode of SocketD Protocol

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to realize the SocketD protocol message authentication mode". In the daily operation, I believe that many people have doubts about how to realize the SocketD protocol message authentication mode. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the SocketD protocol message authentication mode". Next, please follow the editor to study!

SocketD is a binary point-to-point communication protocol and a new network communication layer 7 protocol. Designed to be used in distributed applications. In this sense, SocketD can be an alternative to other similar protocols such as RSocket. Its message protocol specification has the characteristics of asynchronism, two-way back pressure, multiplexing, disconnection reconnection, message-based and so on. Only Java implementation is provided for the time being, and it is currently used as the sockte channel protocol for Solon rpc.

In this case, the right of signing is added on the basis of the news reporting mode as an example:

Server / / start server public class ServerApp {public static void main (String [] args) {/ / launch Solon container (SocketD bean&plugin is managed by solon container) Solon.start (ServerApp.class, args, app-> app.enableSocketD (true)) }} / / define server snooping @ ServerEndpointpublic class ServerListener implements Listener {@ Override public void onOpen (Session session) {System.out.println ("there is a client link...") } @ Override public void onMessage (Session session, Message message) throws IOException {if (session.getHandshaked () = = false) {/ / messages are distributed by the thread pool. Make sure the messages during authentication are sequentially locked synchronized (session) {onMessage0 (session, message);}} else {onMessage0 (session, message) }} private void onMessage0 (Session session, Message message) throws IOException {/ / if it is a handshake, do authentication if (message.flag () = = MessageFlag.handshake) {if (Utils.isEmpty (message.header () {session.close ();} else {Map headers = HeaderUtil.decodeHeaderMap (message.header ()) If ("1" .equals (headers.get ("token")) {/ / if token is 1, set to successful handshake System.out.println ("signature successful"); session.setHandshaked (true);} else {session.close () }} return;} / / nothing else can be done until the handshake is successful And automatically break the link if (session.getHandshaked () = = false) {System.out.println ("this client is very bad, want to send a packet without signing: ("); session.close ();} / / if it is a heartbeat, then almost slightly if (message.flag () = = MessageFlag.heartbeat) {System.out.println ("server: I received a heartbeat") Return;} / / Business processing System.out.println ("Server: I received:" + message.bodyAsString ());}} client / / launch client public class ClientApp {public static void main (String [] args) throws Throwable {/ / launch Solon container (SocketD bean&plugin is managed by solon container) Solon.start (ClientApp.class, args) / / create a session (if the backend is WebSocekt, the protocol header is: ws) Session session = SocketD.createSession ("tcp://localhost:28080"); / / send the service handshake (to sign the right) session.sendHandshake (Message.wrapHandshake ("sn=1&token=1")); / / set 30 seconds to automatically send the heartbeat (if disconnected, also try automatic heavy chain) session.sendHeartbeatAuto (30) / / report the message session.send ("Helloworld server!");}} at this point, the study on "how to implement the authentication mode of SocketD protocol messages" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report