基于Mqtt的即时通信

本文介绍了如何使用Mqtt协议实现即时通信,详细讲解了接收消息的回调接口及其在实际应用中的工作原理。
摘要由CSDN通过智能技术生成
Mqtt是IBM开发的开源的及时通信协议,基于Tcp通信协议

/**
 * Created by 95 on 2016/5/18.
 */
public class MyMqttClient {
    private boolean cleanSession = true;//表示是否清除session
    private int timeout = 10;表示连接超时时间单位为秒
    private int keepalive = 30;表示30秒发送一次心跳包,保持长连接
    private static int qos = 2;表示发送质量
    private static boolean retained = false;表示是否保持记录
    public static MyMqttClient myMqttClient;
    private Context context;
    private final MqttAndroidClient mqttAndroidClient;
    private final MqttConnectOptions connectOptions;
    private MyMqttListener myPublishListener;

    public static MyMqttClient getInstance() {
        if (myMqttClient == null) {
            myMqttClient = new MyMqttClient();
        }
        return myMqttClient;
    }

    public static MyMqttClient getMyMqttClient() {
        return myMqttClient;
    }

    private MyMqttClient() {
        String ClientId = MyApplication.getInstance().getCurrentAccount().getClientId();
        Log.e(ApplicationParams.TAG, "clientid===" + ClientId);
        context = MyApplication.getInstance().getContext();
        mqttAndroidClient = new MqttAndroidClient(context, ApplicationParams.CONNECTMQTTURI, ClientId);创建mqtt客户端连接
        connectOptions = new MqttConnectOptions();创建mqtt属性
        SslUtility.newInstance(context);
        connectOptions.setSocketFactory(SslUtility.getInstance().getSocketFactory(R.raw.kalamodo, ApplicationParams.SSLSECRET));
        connectOptions.setCleanSession(cleanSession);
        connectOptions.setConnectionTimeout(timeout);
        connectOptions.setKeepAliveInterval(keepalive);

    }

    public static MyMqttClient CreateNewClient() {

        return new MyMqttClient();

    }

    public void ClearMqtt() {
        myMqttClient = null;
    }

    /**
     * 连接Mqtt
     */
    public void ConnectMqtt(String username, String pwd, MyLogginListener myLogginListener) {
        try {

            Log.e(ApplicationParams.TAG, "username==" + username + "pwd==" + pwd);
            MyMqttListener myMqttListener = new MyMqttListener(context, MyMqttListener.Action.CONNECT);
            mqttAndroidClient.setCallback(MyMqttCallback.getInstance());
            connectOptions.setUserName(username);设置用户名或密码,如果服务器端不设置,那就不需要
            connectOptions.setPassword(pwd.toCharArray());
            myMqttListener.setOnLogginListener(myLogginListener);
            mqttAndroidClient.connect(connectOptions, null, myMqttListener);
        } catch (MqttException e) {
            Log.e(ApplicationParams.TAG, "连接异常");
            e.printStackTrace();
        }
    }

    public void setOnAiConnectListener(AiConnecttion onAiConnectListener) {
        MyMqttCallback.getInstance().setAiConnecttion(onAiConnectListener);
    }

    /**
     * 关闭释放资源
     */
    public void CloseMqtt() {
        mqttAndroidClient.unregisterResources();
//        mqttAndroidClient.close();
    }

    public void disconnect(IMqttActionListener iMqttActionListener) throws MqttException {
        mqttAndroidClient.disconnect(null, iMqttActionListener);
    }

    /**
     * 断开连接
     *
     * @param
     */
    public void disconnect(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值