-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSDK-Deltas-WebSocket-Example-V1.html
76 lines (60 loc) · 1.93 KB
/
SDK-Deltas-WebSocket-Example-V1.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SK WebSocket Example</title>
<script type="text/javascript">
// First ask user what Address iKommunicate has or address of another server
var addr = prompt("Please enter iKommunicate or Server Address", "i.e. 192.168.1.20 or demo.signalk.org");
// Ideally you would get the IP address automatically using Bonjour/mDNS
function WebSocketTest()
{
// Construct the Websocket URL by adding IP Address to Signal K path
var signalKWebSocket = new WebSocket("ws://" + addr + "/signalk/v1/stream");
// Open WebSocket Callback
signalKWebSocket.onopen = function()
{
console.log("WebSocket Open");
// Web Socket is connected and data should be being received
// In the future more types of subscriptions will be supported and you
// would send a command like the one below, to subscribe to the specific
// data you want. iKommunicate currently ignores all subscription commands.
var subscriptionObject = {
"context": "vessels.self",
"subscribe": [{
"path": "*"
}]
};
var subscriptionMessage = JSON.stringify(subscriptionObject);
console.log("Sending subscription:" + subscriptionMessage)
signalKWebSocket.send(subscriptionMessage);
}
// Any Key Press Callback
document.onkeypress = function()
{
signalKWebSocket.close()
}
// Close WebSocket Callback
signalKWebSocket.onclose = function()
{
// Web Socket is Closed
console.log("WebSocket Closed");
}
// SignalK Message Received Callback
var dataDiv = document.getElementById('data');
signalKWebSocket.onmessage = function(event) {
dataDiv.innerHTML = JSON.stringify(JSON.parse(event.data), null, 2);
}
}
</script>
</head>
<body>
<div id="top"/>
<a href="javascript:WebSocketTest()">Click to Open Web Socket and stream Signal K Deltas </a>
<p>Press any key to close Web Socket</p>
</div>
<div id="data"/>
</div>
</body>
</html>