summaryrefslogtreecommitdiffstats
path: root/apps/samples/server-push/server-push.scxml
blob: c0f0a21df3517de5d1d9515323c379e2c4007a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!-- 
	Example for server-push with long-polling XMLHttpRequests.
	Start in mmi-browser and connect http-browser via:
	http://localhost:8080/push
-->

<scxml name="push" datamodel="ecmascript">
	<script src="http://uscxml.tk.informatik.tu-darmstadt.de/scripts/dump.js" />
	<script src="http://uscxml.tk.informatik.tu-darmstadt.de/scripts/string.  .js" />

	<state id="main">
		<!-- We will only answer http requests when the heartbeat is emitted -->
		<invoke type="heartbeat" id="heartbeat">
			<param name="interval" expr="'1s'" />
		</invoke>
		<state id="idle">
			<!-- XHR CORS preflight response -->
			<transition event="http.options" target="idle">
				<script>dump(_event);</script>
				<respond status="200" to="_event.origin">
					<header name="Access-Control-Allow-Origin" value="*" />
					<header name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
					<header name="Access-Control-Allow-Headers" value="X-Requested-With, Content-Type" />
				</respond>
			</transition>
			
			<transition event="http.post" target="idle">
				<if cond="_event.name.  ('postponed')">
					<!-- This is an event we postponed before the heartbeat, respond -->
					<respond to="_event.origin">
						<content>This is awesome!</content>
					</respond>
				<else />
					<!-- Postpone until the heartbeat is emitted and send all events again -->
					<postpone until="_event.name == 'heartbeat.1s'" chaining="true" />
				</if>
			</transition>
			
			<transition event="http.get">
				<respond to="_event.origin">
					<content>
<![CDATA[
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"></script>

    <script type="text/javascript">
			require(["dojo/domReady!", "dojo"], function(dom, dojo) {
			var xhr = dojo.require("dojo/_base/xhr");
			var longpoll = function() {
		    xhr.post({
					// The URL to request
					url: "http://localhost:8080/push/anywhere",
					handleAs:"text",
					headers:{
						"X-Requested-With": null,
						"Content-Type": "application/json",
					},
					load: function(result) {
						dojo.byId("foo").innerHTML += result + "<br />";
						longpoll();
					}
				});
			}
			longpoll();
		});		  
		</script>
	</head>
	<body class="tundra">
		<div id="foo"></div>
	</body>
</html>
]]>
					</content>
				</respond>
			</transition>
			
		</state>		
	</state>
</scxml>