summaryrefslogtreecommitdiffstats
path: root/apps/samples/websockets/websockets.html
blob: 9a21f4629a462b1af26704f64cb05052a2609afd (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
<!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" />

		<style type="text/css">
		</style>

		<script type="text/javascript">
		</script>

		<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.9.1/dojo/dojo.js"></script>
    
    <script type="text/javascript">
			require([
				"dojo/domReady!",
				"dojox/socket",
      ], function(
				dom,
				Socket
			) {
				var socket = new Socket("ws://localhost:8080/websockets");

				function send(data){
					return socket.send(json.stringify(data));
				}

				socket.on("connect", function(){
					// send a handshake
					send([
						{
							"channel": "/meta/handshake",
							"version": "1.0",
							"minimumVersion": "1.0beta",
							"supportedConnectionTypes": ["long-polling"] // or ["callback-polling"] for x-domain
						}
					])
					socket.on("message", function(data){
						// wait for the response so we can connect with the provided client id
						data = json.parse(data);
						if(data.error){
							throw new Error(error);
						}
						// get the client id for all future messages
						clientId = data.clientId;
						// send a connect message
						send([
							{
								"channel": "/meta/connect",
								"clientId": clientId,
								"connectionType": "long-polling"
							},
							{ // also send a subscription message
								"channel": "/meta/subscribe",
								"clientId": clientId,
								"subscription": "/foo/**"
							}
						]);
						socket.on("message", function(data){
							console.log(data);
						});
					});
				});
			});		  
		</script>
	</head>
	<body class="tundra">
		<table>
			<tr>
				<td>
					<div id="websockets"></div>
				</td>
				<td>
			</tr>
		</table>
	</body>
</html>