client.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?xml version="1.0" ?>
  2. <diagram author="Joeri Exelmans" name="client">
  3. <description>Example of a browser-based WebSocket client with heartbeats</description>
  4. <top></top>
  5. <inport name="in"/>
  6. <outport name="out"/>
  7. <class name="Main" default="true">
  8. <!-- We have our own private 'socket' input port that we use to receive events from our underlying WebSocket object: -->
  9. <inport name="socket"/>
  10. <constructor>
  11. <body>
  12. this.addr = "ws://localhost:8000";
  13. </body>
  14. </constructor>
  15. <scxml
  16. big_step_maximality="take_many"
  17. internal_event_lifeline="next_combo_step"
  18. initial="disconnected">
  19. <state id="disconnected">
  20. <transition event="connect" port="in" target="../connecting_or_connected"/>
  21. </state>
  22. <state id="connecting_or_connected" initial="connecting">
  23. <transition event="disconnect" port="in" target="../disconnected">
  24. <script>
  25. this.socket.close();
  26. </script>
  27. </transition>
  28. <state id="connecting">
  29. <onentry>
  30. <script>
  31. this.socket = new WebSocket(this.addr);
  32. // Translate socket events to statechart events:
  33. this.socket.onopen = event => {
  34. this.controller.addInput(
  35. new Event("open", this.inports["socket"], []),
  36. 0); // time offset
  37. };
  38. this.socket.onmessage = event => {
  39. let parsed;
  40. try {
  41. parsed = JSON.parse(event.data);
  42. } catch (e) {
  43. console.log("received unparsable message", e);
  44. return;
  45. }
  46. this.controller.addInput(
  47. new Event("message", this.inports["socket"], [parsed]),
  48. 0); // time offset
  49. };
  50. this.socket.onerror = event => {
  51. // could not connect
  52. this.controller.addInput(
  53. new Event("error", this.inports["socket"], []),
  54. 0); // time offset
  55. };
  56. this.socket.onclose = event => {
  57. this.controller.addInput(
  58. new Event("close", this.inports["socket"], []),
  59. 0); // time offset
  60. };
  61. </script>
  62. </onentry>
  63. <transition event="open" port="socket" target="../connected"/>
  64. <transition event="error" port="socket" target="../../disconnected">
  65. <raise event="error" port="out"/>
  66. </transition>
  67. </state>
  68. <parallel id="connected">
  69. <onentry>
  70. <raise event="connected" scope="output" port="out"/>
  71. </onentry>
  72. <onexit>
  73. <raise event="disconnected" scope="output" port="out"/>
  74. </onexit>
  75. <transition event="close" port="socket" target="../../disconnected"/>
  76. <state id="send_receive_region">
  77. <state id="ready">
  78. <transition event="send" port="in" target=".">
  79. <parameter name="data"/>
  80. <script>
  81. this.socket.send(JSON.stringify({type:"msg",data}));
  82. </script>
  83. </transition>
  84. <transition event="message" port="socket" target="." cond="parsed.type === 'msg'">
  85. <parameter name="parsed"/>
  86. <raise event="received" port="out">
  87. <parameter expr="parsed"/>
  88. </raise>
  89. </transition>
  90. </state>
  91. </state>
  92. <state id="connection_monitor_region" initial="all_good">
  93. <parallel id="all_good">
  94. <state id="send_pings">
  95. <state id="waiting">
  96. <!-- reset ping timer each time we send anything -->
  97. <transition event="send" port="in" target="."/>
  98. <!-- when not having sent anything for 1s, send a ping to let server know we're still alive -->
  99. <transition after="1" target=".">
  100. <script>
  101. this.socket.send(JSON.stringify({type:"ping"}));
  102. </script>
  103. </transition>
  104. </state>
  105. </state>
  106. <state id="receive_pongs">
  107. <state id="waiting">
  108. <!-- reset pong timer each time we receive anything -->
  109. <transition event="message" port="socket" target="."/>
  110. <!-- when not having received anything for 3s, decide the server and/or connection are (temporarily) dead -->
  111. <transition after="3" target=".">
  112. <!-- WORKAROUND: for some reason, target cannot be '../../../timeout' (limitation of 'main' SCCD, fixed in 'joeri' branch), so we generate an internal event to make the transition from higher up -->
  113. <raise event="timeout"/>
  114. </transition>
  115. </state>
  116. </state>
  117. <!-- part of WORKAROUND, see above -->
  118. <transition event="timeout" target="../timeout">
  119. <raise event="timeout" scope="output" port="out"/>
  120. </transition>
  121. </parallel>
  122. <state id="timeout">
  123. <!-- don't send pings - just wait for any observable server activity -->
  124. <transition event="message" port="socket" target="../all_good">
  125. <raise event="timeout_recovered" scope="output" port="out"/>
  126. </transition>
  127. </state>
  128. </state>
  129. </parallel>
  130. </state>
  131. </scxml>
  132. </class>
  133. </diagram>