/** * Publishes a message to a topic on the server. Takes an * {@link MqttMessage} message and delivers it to the server at the * requested quality of service. * * @param topic * to deliver the message to, for example "finance/stock/ibm". * @param message * to deliver to the server * @return token used to track and wait for the publish to complete. The * token will be passed to any callback that has been set. * @throws MqttPersistenceException * when a problem occurs storing the message * @throws IllegalArgumentException * if value of QoS is not 0, 1 or 2. * @throws MqttException * for other errors encountered while publishing the message. * For instance client not connected. * @see #publish(String, MqttMessage, Object, IMqttActionListener) */ @Override public IMqttDeliveryToken publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException { return publish(topic, message, null, null); }
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException { return publish(topic, payload, qos, retained, null, null);
public void publish(Connection connection, String topic, String message, int qos, boolean retain){ try { String[] actionArgs = new String[2]; actionArgs[0] = message; actionArgs[1] = topic; final ActionListener callback = new ActionListener(this, ActionListener.Action.PUBLISH, connection, actionArgs); connection.getClient().publish(topic, message.getBytes(), qos, retain, null, callback); } catch( MqttException ex){ Log.e(TAG, "Exception occurred during publish: " + ex.getMessage()); } }
public void publishMessage(){ try { MqttMessage message = new MqttMessage(); message.setPayload(publishMessage.getBytes()); mqttAndroidClient.publish(publishTopic, message); addToHistory("Message Published"); if(!mqttAndroidClient.isConnected()){ addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer."); } } catch (MqttException e) { System.err.println("Error Publishing: " + e.getMessage()); e.printStackTrace(); } }
/** * Publishes a message to a topic on the server. Takes an * {@link MqttMessage} message and delivers it to the server at the * requested quality of service. * * @param topic * to deliver the message to, for example "finance/stock/ibm". * @param message * to deliver to the server * @return token used to track and wait for the publish to complete. The * token will be passed to any callback that has been set. * @throws MqttPersistenceException * when a problem occurs storing the message * @throws IllegalArgumentException * if value of QoS is not 0, 1 or 2. * @throws MqttException * for other errors encountered while publishing the message. * For instance client not connected. * @see #publish(String, MqttMessage, Object, IMqttActionListener) */ @Override public IMqttDeliveryToken publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException { return publish(topic, message, null, null); }
/** * Publishes a message to a topic on the server. Takes an * {@link MqttMessage} message and delivers it to the server at the * requested quality of service. * * @param topic * to deliver the message to, for example "finance/stock/ibm". * @param message * to deliver to the server * @return token used to track and wait for the publish to complete. The * token will be passed to any callback that has been set. * @throws MqttPersistenceException * when a problem occurs storing the message * @throws IllegalArgumentException * if value of QoS is not 0, 1 or 2. * @throws MqttException * for other errors encountered while publishing the message. * For instance client not connected. * @see #publish(String, MqttMessage, Object, IMqttActionListener) */ @Override public IMqttDeliveryToken publish(String topic, MqttMessage message) throws MqttException, MqttPersistenceException { return publish(topic, message, null, null); }
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException { return publish(topic, payload, qos, retained, null, null);
public IMqttDeliveryToken publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException, MqttPersistenceException { return publish(topic, payload, qos, retained, null, null);
public void publish(String topic, String payload, int qos) { if (mMqttClient != null && mMqqtClientStatus == MqqtConnectionStatus.CONNECTED && topic != null) { boolean retained = false; try { Log.d(TAG, "Mqtt: publish " + payload + " for topic " + topic + " qos:" + qos); mMqttClient.publish(topic, payload.getBytes(), qos, retained, null, null); } catch (MqttException e) { Log.e(TAG, "Mqtt:x publish error: ", e); } } }
/** * 发布消息 * @param msg * @param topic * @param qos * @param retained */ public void publish(String msg, String topic, int qos, boolean retained) { try { client.publish(topic, msg.getBytes(), qos, retained); } catch (Exception e) { e.printStackTrace(); } }
subToken.waitForCompletion(waitForCompletionTime); pubToken = mqttClient.publish(topicNames[0], message, 0, false, null, new ActionListener(notifier)); pubToken.waitForCompletion(waitForCompletionTime);
subToken.waitForCompletion(waitForCompletionTime); pubToken = mqttClient.publish(topicNames[0], message, 0, false, null, null); pubToken.waitForCompletion(waitForCompletionTime);
public void publishMessage(@NonNull MqttAndroidClient client, @NonNull String msg, int qos, @NonNull String topic) throws MqttException, UnsupportedEncodingException { byte[] encodedPayload = new byte[0]; encodedPayload = msg.getBytes("UTF-8"); MqttMessage message = new MqttMessage(encodedPayload); message.setId(320); message.setRetained(true); message.setQos(qos); client.publish(topic, message); }
@Override public void onClick(View view) { /**获取发布的主题*/ pubTopic = edPubTopic.getText().toString().trim(); /**获取发布的消息*/ pubMessage = edPubMessage.getText().toString().trim(); /**消息的服务质量*/ int qos=0; /**消息是否保持*/ boolean retain=false; /**要发布的消息内容*/ byte[] message=pubMessage.getBytes(); if(pubTopic!=null&&!"".equals(pubTopic)){ /**获取client对象*/ MqttAndroidClient client = MainActivity.getMqttAndroidClientInstace(); if(client!=null){ try { /**发布一个主题:如果主题名一样不会新建一个主题,会复用*/ client.publish(pubTopic,message,qos,retain,null,new PublishCallBackHandler(ChatActivity.this)); } catch (MqttException e) { e.printStackTrace(); } }else{ Log.e(PA,"MqttAndroidClient==null"); } }else{ Toast.makeText(ChatActivity.this,"发布的主题不能为空",Toast.LENGTH_SHORT).show(); } } });
@Override public void onClick(View view) { /**获取发布的主题*/ pubTopic = edPubTopic.getText().toString().trim(); /**获取发布的消息*/ pubMessage = edPubMessage.getText().toString().trim(); /**消息的服务质量*/ int qos=0; /**消息是否保持*/ boolean retain=false; /**要发布的消息内容*/ byte[] message=pubMessage.getBytes(); if(pubTopic!=null&&!"".equals(pubTopic)){ /**获取client对象*/ MqttAndroidClient client = MainActivity.getMqttAndroidClientInstace(); if(client!=null){ try { /**发布一个主题:如果主题名一样不会新建一个主题,会复用*/ client.publish(pubTopic,message,qos,retain,null,new PublishCallBackHandler(PublishActivity.this)); } catch (MqttException e) { e.printStackTrace(); } }else{ Log.e(PA,"MqttAndroidClient==null"); } }else{ Toast.makeText(PublishActivity.this,"发布的主题不能为空",Toast.LENGTH_SHORT).show(); } } });
.publish(topic, message.getBytes(), qos, retained, null, new ActionListener(context, Action.PUBLISH, clientHandle, args));
client.publish("messages", message);
client.publish("messages", message);
mqttMessage.setQos(qos); try { mqttAndroidClient.publish(topic, mqttMessage, null, new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) {
return mClient.publish(topic, mqttMessage, mContext, listener); } catch (MqttPersistenceException e) { e.printStackTrace();