aboutsummaryrefslogtreecommitdiff
path: root/src/library_sockfs.js
diff options
context:
space:
mode:
authorFraser Adams <fraser.adams@blueyonder.co.uk>2014-01-15 17:13:59 +0000
committerFraser Adams <fraser.adams@blueyonder.co.uk>2014-01-15 17:13:59 +0000
commit43ee264bab811099b3a6581f2ae87d501675a4e7 (patch)
treeb16fed20293ad8ff5276a6d1d77702aff27082f6 /src/library_sockfs.js
parent899b2b41e696ff627ee9b8690d9ef8ba04e73df3 (diff)
Provide compile time and run time options for WebSocket URL and subprotocol fields
Diffstat (limited to 'src/library_sockfs.js')
-rw-r--r--src/library_sockfs.js40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/library_sockfs.js b/src/library_sockfs.js
index 2028d841..712c6d85 100644
--- a/src/library_sockfs.js
+++ b/src/library_sockfs.js
@@ -131,14 +131,44 @@ mergeInto(LibraryManager.library, {
port = parseInt(result[2], 10);
}
} else {
- // create the actual websocket object and connect
+ // Create the actual websocket object and connect.
try {
- var url = 'ws://' + addr + ':' + port;
+ // runtimeConfig gets set to true if WebSocket runtime configuration is available.
+ var runtimeConfig = (Module['websocket'] && ('object' === typeof Module['websocket']));
+
+ // The default value is 'ws://' the replace is needed because the compiler replaces "//" comments with '#'
+ // comments without checking context, so we'd end up with ws:#, the replace swaps the "#" for "//" again.
+ var url = '{{{ WEBSOCKET_URL }}}'.replace('#', '//');
+
+ if (runtimeConfig) {
+ if ('string' === typeof Module['websocket']['url']) {
+ url = Module['websocket']['url']; // Fetch runtime WebSocket URL config.
+ }
+ }
+
+ if (url === 'ws://' || url === 'wss://') { // Is the supplied URL config just a prefix, if so complete it.
+ url = url + addr + ':' + port;
+ }
+
+ // Make the WebSocket subprotocol (Sec-WebSocket-Protocol) default to binary if no configuration is set.
+ var subProtocols = '{{{ WEBSOCKET_SUBPROTOCOL }}}'; // The default value is 'binary'
+
+ if (runtimeConfig) {
+ if ('string' === typeof Module['websocket']['subprotocol']) {
+ subProtocols = Module['websocket']['subprotocol']; // Fetch runtime WebSocket subprotocol config.
+ }
+ }
+
+ // The regex trims the string (removes spaces at the beginning and end, then splits the string by
+ // <any space>,<any space> into an Array. Whitespace removal is important for Websockify and ws.
+ subProtocols = subProtocols.replace(/^ +| +$/g,"").split(/ *, */);
+
+ // The node ws library API for specifying optional subprotocol is slightly different than the browser's.
+ var opts = ENVIRONMENT_IS_NODE ? {'protocol': subProtocols.toString()} : subProtocols;
+
#if SOCKET_DEBUG
- console.log('connect: ' + url);
+ Module.print('connect: ' + url + ', ' + subProtocols.toString());
#endif
- // the node ws library API is slightly different than the browser's
- var opts = ENVIRONMENT_IS_NODE ? {headers: {'websocket-protocol': ['binary']}} : ['binary'];
// If node we use the ws library.
var WebSocket = ENVIRONMENT_IS_NODE ? require('ws') : window['WebSocket'];
ws = new WebSocket(url, opts);