Advertisement
Guest User

wkwk

a guest
Apr 28th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <body>
  5. <script type="text/javascript" src="{{url('assets/js/jquery-1.11.2.min.js')}}"></script>
  6. <script type="text/javascript" src="{{url('assets/js/mqttws31.js')}}"></script>
  7. <!-- <script type="text/javascript" src="mqttws31.js"></script> -->
  8. <script type="text/javascript">
  9.  
  10. // Create a client instance
  11. //client = new Paho.MQTT.Client("host", port,"client_id");
  12. client = new Paho.MQTT.Client("m10.cloudmqtt.com", 32111, "web_" + parseInt(Math.random() * 100, 10));
  13.  
  14. // set callback handlers
  15. client.onConnectionLost = onConnectionLost;
  16. client.onMessageArrived = onMessageArrived;
  17. var options = {
  18. useSSL: true,
  19. userName: "yqffttwm",
  20. password: "eNj5k4a6eN5h",
  21. onSuccess:onConnect,
  22. onFailure:doFail
  23. }
  24.  
  25. // connect the client
  26. client.connect(options);
  27.  
  28. // called when the client connects
  29. function onConnect() {
  30. // Once a connection has been made, make a subscription and send a message.
  31. console.log("onConnect");
  32. client.subscribe("/chat");
  33. client.subscribe("/typing");
  34. client.subscribe("/deltyping");
  35. }
  36.  
  37. function doFail(e){
  38. console.log(e);
  39. }
  40.  
  41. // called when the client loses its connection
  42. function onConnectionLost(responseObject) {
  43. if (responseObject.errorCode !== 0) {
  44. console.log("onConnectionLost:"+responseObject.errorMessage);
  45. }
  46. }
  47.  
  48. // called when a message arrives
  49. function onMessageArrived(message) {
  50. switch(message.destinationName){
  51. case "/chat":
  52. var user = JSON.parse(message.payloadString);
  53. $("#chat-div").append("<b>"+user.username+"</b> : "+user.msg+"<br>");
  54. $("#"+user.types).remove();
  55. break;
  56. case "/typing":
  57. var user = JSON.parse(message.payloadString);
  58. $("#typing-div").append("<span id='"+user.types+"'>"+user.text+"<br></span>");
  59. break;
  60. case "/deltyping":
  61. console.log(message.payloadString);
  62. $(message.payloadString).remove();
  63. break;
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement