WebSocket.qml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import QtWebSockets 1.0
  2. import QtQuick 2.5
  3. Item {
  4. property alias tot_slides : socket.total_slides
  5. function isOpen(){
  6. return socket.status == WebSocket.Open;
  7. }
  8. function send_obj(obj) {
  9. socket.sendTextMessage(JSON.stringify(obj))
  10. }
  11. function set_preg() {
  12. send_obj({
  13. type: "initpreg"
  14. })
  15. }
  16. function unset_preg() {
  17. send_obj({
  18. type: "stoppreg"
  19. })
  20. }
  21. function prev_diapo() {
  22. var curImage = Math.max(item1.curImage - 1, 1)
  23. send_obj({
  24. type: "command",
  25. commandtype: "diapo",
  26. PDF: item1.curPDF,
  27. slide: curImage
  28. })
  29. }
  30. function next_diapo() {
  31. var curImage = Math.min(item1.curImage + 1, tot_slides)
  32. send_obj({
  33. type: "command",
  34. commandtype: "diapo",
  35. PDF: item1.curPDF,
  36. slide: curImage
  37. })
  38. }
  39. function clear_path() {
  40. send_obj({
  41. type: "command",
  42. commandtype: "clean"
  43. })
  44. canvas.clean_path()
  45. }
  46. function send_drawing(d) {
  47. send_obj({
  48. type: "command",
  49. commandtype: "draw",
  50. points: d,
  51. color: canvas.curColor
  52. })
  53. }
  54. WebSocket {
  55. property int total_slides: 5
  56. id: socket
  57. url: "wss://wscasa.especificosba.com.ar"
  58. active: true
  59. onTextMessageReceived: {
  60. var obj = JSON.parse(message)
  61. switch (obj.type) {
  62. case "diapo":
  63. canvas.clean_path()
  64. item1.curPDF = obj.PDF
  65. item1.curImage = obj.slide //automagically change_image
  66. if (obj.total_slides === undefined){
  67. console.log(obj);
  68. total_slides = 3
  69. } else {
  70. total_slides = obj.total_slides
  71. }
  72. break
  73. case "preg":
  74. item1.addPreg(obj.text)
  75. break
  76. case "deletepreg":
  77. item1.pregs = []
  78. break
  79. case "draw":
  80. break
  81. case "snowmix":
  82. break
  83. default:
  84. console.log(JSON.stringify(obj))
  85. }
  86. }
  87. onStatusChanged: {
  88. if (socket.status == WebSocket.Open) {
  89. wsStatus.text = qsTr("Conectado")
  90. } else {
  91. wsStatus.text = qsTr("Desconectado\nReconectando..")
  92. }
  93. }
  94. }
  95. Timer {
  96. interval: 800;
  97. running: true;
  98. repeat: true;
  99. onTriggered: {
  100. if (socket.status != WebSocket.Open){//Reconnect
  101. socket.active = false
  102. socket.active = true
  103. console.log("Connecting... " +Date().toString())
  104. }
  105. }
  106. }
  107. }