spring boot 中autobahn-java(wamp)的使用

        最近在後端開發中遇到了須要和前端進行通訊的問題。由於之前用到過crossbar的 wamp (此處的wamp可不是apache+Mysql/MariaDB+Perl/PHP/Python,在搜索wamp時會有混淆,建議搜索 wamp protocol), 可是國內用這個東西的真的是太少了,最多就是用在android上進行消息推送(又或許本人看的太少)。 網上關於spring 調用crossbar 提供的官方包 antobahn-java的資料能夠說沒有(我反正是沒搜到過)。因此在完成功能後,特地將調用方式貼出來,以備本身使用和方便其餘有須要的猿們:sunglasses:。 crossbar官方網站前端

wamp簡單介紹

        不說廢話,wamp在消息轉發這方面使用 發佈/訂閱 模式 即 publish/subscribe。 在wamp中有 topic的概念。發佈者,訂閱者訂閱相同主題纔可達到消息轉發的功能。關於服務器的搭建就不作敘述,跟着官網很容易搭建出來。 wamp有域的概念 即下文提到的realm,發佈訂閱雙發需在同一realm中才可正常通訊。java

乾貨

        在簡單介紹裏,咱們提到了 wamp使用 發佈/訂閱模式來進行消息轉發,那麼咱們接下用spring boot來實現官方包 autobahn-java的調用。 maven地址android

<dependency>
            <groupId>io.crossbar.autobahn</groupId>
            <artifactId>autobahn-java</artifactId>
            <version>18.5.1</version>
        </dependency>
複製代碼

wamp配置

代碼以下:spring

package com.lichaobao.springwamp.config;

import io.crossbar.autobahn.wamp.Client;
import io.crossbar.autobahn.wamp.Session;
import io.crossbar.autobahn.wamp.interfaces.ISession;
import io.crossbar.autobahn.wamp.types.CloseDetails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** * @author lichaobao * @date 2019/1/14 * @QQ 1527563274 */
@Configuration
public class WAMPConfig {
    @Value("${wamp.url}")
    private String url;
    @Value("${wamp.realm}")
    private String realm;
    private static volatile boolean canSend = false;
    @Bean
    public Session getSession(){
        Session session = new Session();
        session.addOnConnectListener(new ISession.OnConnectListener() {
            @Override
            public void onConnect(Session session) {
                int a = 0;
                if(session.isConnected()){
                    canSend = true;//鏈接後 檢查是否能發送消息 並把canSend置爲true
                }
                System.out.println("connect");
            }
        });
        session.addOnLeaveListener(new ISession.OnLeaveListener() {
            @Override
            public void onLeave(Session session, CloseDetails closeDetails) {
                System.out.println("onLeave");
                canSend = false;
            }
        });
        session.addOnDisconnectListener(new ISession.OnDisconnectListener() {
            @Override
            public void onDisconnect(Session session, boolean b) {
                System.out.println("disconnect");
            }
        });
        Client client = new Client(session,url,realm);
        client.connect();
        return session;
    }

    /** * 檢查是否能發送消息 * @return */
    public static boolean isCanSend() {
        return canSend;
    }
}

複製代碼

工具示例

package com.wugeek.deepassserver.utils;

import com.wugeek.deepassserver.config.WAMPConfig;
import io.crossbar.autobahn.wamp.Session;
import io.crossbar.autobahn.wamp.interfaces.ISession;
import io.crossbar.autobahn.wamp.types.CloseDetails;
import io.crossbar.autobahn.wamp.types.Publication;
import io.crossbar.autobahn.wamp.types.SessionDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/** * @author lichaobao * @date 2019/1/14 * @QQ 1527563274 */
@Component
public class WAMPUtils {
    @Autowired
    Session session;
    public String sendMsg(String topic,String content){
        if(WAMPConfig.isCanSend()){
          CompletableFuture<Publication> pubFuture =  session.publish(topic,content);
          pubFuture.thenAccept(accept->{
              System.out.println("publish to "+topic);
          });
          return "ok";
        }else{
            return "error";
        }
    }
}
public String subscribe(String topic){
        if(WAMPConfig.isCanSend()){
            CompletableFuture<Subscription> subFuture = session.subscribe(topic,
                    this::onEvent);
            subFuture.whenComplete((subscription, throwable) -> {
                if (throwable == null) {
                    // We have successfully subscribed.
                    System.out.println("Subscribed to topic " + subscription.topic);
                } else {
                    // Something went bad.
                    throwable.printStackTrace();
                }
            });
            return "ok";
        }else{
            return "error";
        }
    }
    private void onEvent(List<Object> args) {
        System.out.println(String.format("Got event: %s", args.get(0)));
    }

複製代碼

-完-sql

相關文章
相關標籤/搜索