-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsocket.js
More file actions
139 lines (123 loc) · 4.5 KB
/
Copy pathwebsocket.js
File metadata and controls
139 lines (123 loc) · 4.5 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//////////////////////////////////////////////////////////////////////////////
//
// sqlite_gui/websocket.js
//
// The class for the websocket connection to the cached data server
//
// The only method of interest to most people will be
// WS.write(argument)
// The argument will be converted to JSON and sent to the websocket.
//
// The websocket name can either be specified in the config file
// (openrvdas.json), or if not specified (or specified as ""),
// this routine will take a "best guess".
//
// This is usually placed last in the list of included scripts.
// If you want to try a different ordering, go for it, let me
// know how it works out.
//
/////////////////////////////////////////////////////////////////////
//
// Websocket code
//
/////////////////////////////////////////////////////////////////////
var WS = (function() {
// Start by chastising people with sucky browsers
if (!("WebSocket" in window)) {
alert("Warning: websockets not supported by your browser!");
return false;
}
// If we don't have a websocket server defined, guess
var retry_interval = 3000;
var parser_queue = [];
// rwc = retry_websocket_connection
var rwc;
var ws;
var sock;
var initial_send_message = {
'type':'subscribe',
'fields': {
'status:cruise_definition':{'seconds':-1},
'status:cruise_mode':{'seconds':-1},
'status:logger_status':{'seconds':-1},
'status:file_update':{'seconds':0},
// Get logger manager stderr, too.
'stderr:logger_manager':{'seconds': 60*60}
}
};
function connect_websocket() {
try {
// config object (odas) may not exist. Fallback.
var o = odas || {};
url = odas.ws || "";
if (url == "") {
var proto = 'ws://';
var p = document.location.protocol;
if (p == 'https:') {
proto = 'wss://';
}
port = "";
p = document.location.port;
if (p != "") {
port = ":" + p;
}
url = proto + document.location.host + port + '/cds-ws';
}
sock = new WebSocket(url);
} catch (err) {}
sock.onopen = function() {
clearTimeout(rwc);
write(initial_send_message);
};
sock.onclose = function() {
// Set up an alarm to sleep, then try re-opening websocket
console.warn("Websocket closed, trying to reconnect");
rwc = setTimeout(connect_websocket, retry_interval);
};
// Some socket errors won't quickly close the connection,
// To speed things up, close it ourselves. If the error
// already closed the connection, this does no harm.
sock.onerror = function(event) {
console.error("Websocket error received", event);
sock.close(1000, "Due to client receiving an error");
};
sock.onmessage = function (received_message) {
var data = received_message.data;
parser_queue.forEach(function(func){ func(data); } );
write({'type':'ready'});
};
// If we're deliberately closing, don't set a timer to
// autoreconnect. That would just be silly.
window.onbeforeunload = function(event) {
console.debug("Closing websocket");
sock.onclose = {};
sock.close();
};
}
connect_websocket();
// Used to add additional methods to message parser.
// Not sure why we'd want more than one, but I remember
// needing something like this in another project, so....
var add_parser = function(parser) {
if (parser_queue.includes(parser)) { return; }
parser_queue.push(parser);
};
// This is our default function name for the message parser.
// If it's defined, add it automatically.
if (typeof process_message === 'function') {
add_parser(process_message);
}
// While there's basically nothing wrong with the way 'send' was
// defined previously, it's pretty much better to make it a
// method of our websocket object. Makes it clear that we're
// performing a websocket operation. A bare "send" is ambiguous
// when seen elsewhere to someone not familiar with the code.
var write = function(msg) {
sock.send(JSON.stringify(msg));
};
return {
ws: sock,
add_parser: add_parser,
write: write
};
})();