| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?xml version="1.0" ?>
- <diagram author="Joeri Exelmans" name="client">
- <description>Example of a browser-based WebSocket client with heartbeats</description>
- <top></top>
- <inport name="in"/>
- <outport name="out"/>
- <class name="Main" default="true">
- <!-- We have our own private 'socket' input port that we use to receive events from our underlying WebSocket object: -->
- <inport name="socket"/>
- <constructor>
- <body>
- this.addr = "ws://localhost:8000";
- </body>
- </constructor>
- <scxml
- big_step_maximality="take_many"
- internal_event_lifeline="next_combo_step"
- initial="disconnected">
- <state id="disconnected">
- <transition event="connect" port="in" target="../connecting_or_connected"/>
- </state>
- <state id="connecting_or_connected" initial="connecting">
- <transition event="disconnect" port="in" target="../disconnected">
- <script>
- this.socket.close();
- </script>
- </transition>
- <state id="connecting">
- <onentry>
- <script>
- this.socket = new WebSocket(this.addr);
- // Translate socket events to statechart events:
- this.socket.onopen = event => {
- this.controller.addInput(
- new Event("open", this.inports["socket"], []),
- 0); // time offset
- };
- this.socket.onmessage = event => {
- let parsed;
- try {
- parsed = JSON.parse(event.data);
- } catch (e) {
- console.log("received unparsable message", e);
- return;
- }
- this.controller.addInput(
- new Event("message", this.inports["socket"], [parsed]),
- 0); // time offset
- };
- this.socket.onerror = event => {
- // could not connect
- this.controller.addInput(
- new Event("error", this.inports["socket"], []),
- 0); // time offset
- };
- this.socket.onclose = event => {
- this.controller.addInput(
- new Event("close", this.inports["socket"], []),
- 0); // time offset
- };
- </script>
- </onentry>
- <transition event="open" port="socket" target="../connected"/>
- <transition event="error" port="socket" target="../../disconnected">
- <raise event="error" port="out"/>
- </transition>
- </state>
- <parallel id="connected">
- <onentry>
- <raise event="connected" scope="output" port="out"/>
- </onentry>
- <onexit>
- <raise event="disconnected" scope="output" port="out"/>
- </onexit>
- <transition event="close" port="socket" target="../../disconnected"/>
- <state id="send_receive_region">
- <state id="ready">
- <transition event="send" port="in" target=".">
- <parameter name="data"/>
- <script>
- this.socket.send(JSON.stringify({type:"msg",data}));
- </script>
- </transition>
- <transition event="message" port="socket" target="." cond="parsed.type === 'msg'">
- <parameter name="parsed"/>
- <raise event="received" port="out">
- <parameter expr="parsed"/>
- </raise>
- </transition>
- </state>
- </state>
- <state id="connection_monitor_region" initial="all_good">
- <parallel id="all_good">
- <state id="send_pings">
- <state id="waiting">
- <!-- reset ping timer each time we send anything -->
- <transition event="send" port="in" target="."/>
- <!-- when not having sent anything for 1s, send a ping to let server know we're still alive -->
- <transition after="1" target=".">
- <script>
- this.socket.send(JSON.stringify({type:"ping"}));
- </script>
- </transition>
- </state>
- </state>
- <state id="receive_pongs">
- <state id="waiting">
- <!-- reset pong timer each time we receive anything -->
- <transition event="message" port="socket" target="."/>
- <!-- when not having received anything for 3s, decide the server and/or connection are (temporarily) dead -->
- <transition after="3" target=".">
- <!-- 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 -->
- <raise event="timeout"/>
- </transition>
- </state>
- </state>
- <!-- part of WORKAROUND, see above -->
- <transition event="timeout" target="../timeout">
- <raise event="timeout" scope="output" port="out"/>
- </transition>
- </parallel>
- <state id="timeout">
- <!-- don't send pings - just wait for any observable server activity -->
- <transition event="message" port="socket" target="../all_good">
- <raise event="timeout_recovered" scope="output" port="out"/>
- </transition>
- </state>
- </state>
- </parallel>
- </state>
- </scxml>
- </class>
- </diagram>
|