gpt4 book ai didi

java - 多次调用 onCreate 时 MQTT 创建多个连接

转载 作者:行者123 更新时间:2023-11-29 04:34:52 25 4
gpt4 key购买 nike

我正在使用 Paho Android MQTT 客户端创建一个即时消息系统。它的实现按预期工作,但是我遇到了这些错误。

我在MainActivity 类onCreate 中调用连接类(这也要求创建与代理的连接) .

现在的问题是,假设我在 MainActivity Class 上,然后按返回键从 MainActivity Class 移动到另一个 Activity ,稍后我又回到MainActivity Class,将创建​​另一个代理连接。这意味着无论何时发布一条消息,客户端都会收到两次消息。

MainActivity.java:

公共(public)类 MainActivity 扩展 AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_intera);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

dbHelper = DatabaseManager.getInstance(context);
//mRecyclerView = (RecyclerView) findViewById(R.id.history_recycler_view);

connections = new Connections();
connections.createConnectionForPublishing(context);

}


@Override
public void onDestroy() {
super.onDestroy();
System.out.println("LOG: Service destroyed");
}

Connection.java

public class Connection {

public void createConnectionForPublishing (final Context context) {

//Instantiate the mqtt android client class
mqttAndroidClient = new MqttAndroidClient (context.getApplicationContext (), serverUri, clientId);


mqttAndroidClient.setCallback (new MqttCallbackExtended () {

@Override
public void connectComplete(boolean reconnect, String serverURI) {
if (reconnect) {
System.out.println ("Reconnected to : " + serverURI);
} else {
System.out.println ("Connected to: " + serverURI);
}
}

@Override
public void connectionLost (Throwable cause) {
System.out.println ("The Connection was lost.");
}

@Override
public void messageArrived (String topic, final MqttMessage message) throws Exception {
System.out.println ("Message received and Arrived");
}

@Override
public void deliveryComplete (IMqttDeliveryToken token) {
System.out.println("Message Delivered");
}
});

final MqttConnectOptions mqttConnectOptions = new MqttConnectOptions ();
mqttConnectOptions.setMqttVersion (MqttConnectOptions.MQTT_VERSION_3_1_1);
mqttConnectOptions.setAutomaticReconnect (true);
mqttConnectOptions.setCleanSession (false);

try {
mqttAndroidClient.connect (mqttConnectOptions, null, new IMqttActionListener () {

@Override
public void onSuccess (IMqttToken asyncActionToken) {
System.out.println ("BROKER CONNECTED");

DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions ();
disconnectedBufferOptions.setBufferEnabled (true);
disconnectedBufferOptions.setBufferSize (100);
disconnectedBufferOptions.setPersistBuffer (false);
disconnectedBufferOptions.setDeleteOldestMessages (false);

//mqttAndroidClient.setBufferOpts (disconnectedBufferOptions);

}

@Override
public void onFailure (IMqttToken asyncActionToken, Throwable exception) {
System.out.println ("Failed to connect to: " + serverUri);
}
});

} catch (MqttException ex) {
ex.printStackTrace ();
}
}

// ...
}

我是 MQTT 的新手,如果有人能提供帮助,我会很高兴。提前致谢

最佳答案

然后,如果您不想多次建立连接,则必须将其设置为单例,只有 1 个连接类实例和一次建立的连接。

所以你的代码会变成:

MainActivity.java:

public class MainActivity extends Activity {

protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.chat_intera);

Connection connection = Connection.getInstance (getApplicationContext ());
}

// ....
}

Connection.java

public class Connection {

private static Connection connInst;
private static boolean connected;

private static MqttAndroidClient mqttAndroidClient;
private static IMqttActionListener mqttActionListener;
private final static MqttConnectOptions mqttConnectOptions = new MqttConnectOptions ();
static {
mqttConnectOptions.setMqttVersion (MqttConnectOptions.MQTT_VERSION_3_1_1);
mqttConnectOptions.setAutomaticReconnect (true);
mqttConnectOptions.setCleanSession (false);
}

private Connection (Context context) {
//Instantiate the mqtt android client class
mqttAndroidClient = new MqttAndroidClient (context.getApplicationContext (), serverUri, clientId);

mqttAndroidClient.setCallback (new MqttCallbackExtended () {

@Override
public void connectComplete (boolean reconnect, String serverURI) {
connected = true;
if (reconnect) {
System.out.println ("Reconnected to : " + serverURI);
} else {
System.out.println ("Connected to: " + serverURI);
}
}

@Override
public void connectionLost (Throwable cause) {
connected = false;
System.out.println ("The Connection was lost.");
}

@Override
public void messageArrived (String topic, final MqttMessage message) throws Exception {
System.out.println ("Message received and Arrived");
}

@Override
public void deliveryComplete (IMqttDeliveryToken token) {
System.out.println("Message Delivered");
}
});

mqttActionListener = new IMqttActionListener () {

@Override
public void onSuccess (IMqttToken asyncActionToken) {
System.out.println ("BROKER CONNECTED");

DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions ();
disconnectedBufferOptions.setBufferEnabled (true);
disconnectedBufferOptions.setBufferSize (100);
disconnectedBufferOptions.setPersistBuffer (false);
disconnectedBufferOptions.setDeleteOldestMessages (false);

//mqttAndroidClient.setBufferOpts (disconnectedBufferOptions);

}

@Override
public void onFailure (IMqttToken asyncActionToken, Throwable exception) {
System.out.println ("Failed to connect to: " + serverUri);
}
});
}

public static Connection getInstance (Context context) {
if (connInst == null) {
connInst = new Connection (context);
}
createConnectionIfNeeded (context);

return connInst;
}

private static void createConnectionIfNeeded () {
if (connected) {
return;
}

try {
mqttAndroidClient.connect (mqttConnectOptions, null, mqttActionListener);

} catch (MqttException ex) {
ex.printStackTrace ();
}
}

// ...
}

关于java - 多次调用 onCreate 时 MQTT 创建多个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com