Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <!-- <script type="text/javascript" src = "mqttws31.js"></script> -->
  2. <script type="text/javascript" src = "browserMqtt.js"></script>
  3. <script type="text/javascript">
  4. /*
  5. Function: runClient
  6. create and run client to receive messages from mqtt broker and post these to api server
  7. for indexing
  8.  
  9. Parameters :
  10. none
  11.  
  12. Returns:
  13. none
  14. */
  15. function runClient () {
  16. console.log('running client');
  17. var options = {
  18. // protocolId: 'MQIsdp' // or 'MQIsdp' in MQTT 3.1.1
  19. // , protocolVersion: 3 // or 3 in MQTT 3.1
  20. clean: false // or false
  21. , clientId: '13'
  22. , keepalive: 60 // seconds, 0 is the default, can be any positive number
  23. // , username: config['BROKER']['USERNAME']
  24. // , password: new Buffer(config['BROKER']['PASSWORD']) // passwords are buffers
  25. // , will: {
  26. // topic: 'mydevice/status'
  27. // , payload: new Buffer('dead') // payloads are buffers
  28. // }
  29. }
  30. var client = mqtt.connect('ws://127.0.0.1:8080/mqtt', options);
  31.  
  32. client.on('connect', function (connack) {
  33. // console.log(connack.sessionPresent)
  34. client.subscribe('/hello', {qos: 1}, function(error, granted){
  35. console.log(error)
  36. console.log(granted)
  37. })
  38. console.log('connected!')
  39. });
  40.  
  41. client.on('message', function (topic, message, packet) {
  42. message = message.toString()
  43. console.log(message)
  44. });
  45.  
  46. }
  47.  
  48. runClient()
  49.  
  50. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement