111,094
社区成员




private void sendMessage()
{
string strURL = string.Format("http://192.168.201.7:3333/send_mqtt/send_mqtt.php?target={0}&message={1}", textBoxTarget.Text, textBoxMessage.Text);
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "GET";
System.Net.HttpWebResponse response;
// 获得响应流
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseText = myreader.ReadToEnd();
myreader.Close();
MessageBox.Show(responseText);
}
<?php
require('etc/config.php');
require('lib/php_sam.php');
//create a new connection object
$conn = new SAMConnection();
//start initialise the connection
$conn->connect(SAM_MQTT, array(SAM_HOST => MQTT_SERVER_HOST, SAM_PORT => MQTT_SERVER_POST));
//create a new MQTT message with the output of the shell command as the body
$msgCpu = new SAMMessage($_REQUEST['message']);
//send the message on the topic cpu
$conn->send('topic://'.$_REQUEST['target'], $msgCpu);
$conn->disconnect();
echo 'MQTT Message to ' . $_REQUEST['target'] . ' sent: ' . $_REQUEST['message'];
?>
<script type="text/javascript">
$(function() {
$('label.messageLabel').labelOver('labelover');
$('label.targetLabel').labelOver('labelover');
$("#button").click(function() {
var target = 'tokudu/' + $('#messageTarget').val();
var message = $('#messageBody').val();
$('.sent').hide();
$('.loading').slideToggle('fast');
$.ajax({
url: 'send_mqtt.php',
type: 'POST',
data: {'target': target, 'message': message},
dataType: 'text',
timeout: 20000,
error: function(){
$('.loading').slideToggle('fast');
alert('Failed to communicate to the server. Try again!')
},
success: function(text){
$('.loading').slideToggle('fast');
if (text == '') {
alert('Failed to send the message. Try again!')
} else {
$('.sent').slideToggle('fast');
}
}
});
});
})
</script>