aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-03-17 15:22:48 -0700
committerAlon Zakai <alonzakai@gmail.com>2014-03-17 15:22:48 -0700
commit4f0e8c3aed0ce2e4606c5c8facacfbd3c015d375 (patch)
tree94cdf2df79d6d8ddc428d659eb180555775b8768 /src
parent46bf009284405d352e6c9682829b4e0895b7c17f (diff)
parent4cc63f3c60ea9cc2e366671cb40de2e929c83244 (diff)
Merge pull request #2020 from fadams/improve-websocket-config-options
Provide compile time and run time options for WebSocket URL and subproto...
Diffstat (limited to 'src')
-rw-r--r--src/library.js4
-rw-r--r--src/library_sockfs.js38
-rw-r--r--src/settings.js13
3 files changed, 51 insertions, 4 deletions
diff --git a/src/library.js b/src/library.js
index e711ee98..8aac2bb2 100644
--- a/src/library.js
+++ b/src/library.js
@@ -8176,7 +8176,9 @@ LibraryManager.library = {
},
setsockopt: function(d, level, optname, optval, optlen) {
+#if SOCKET_DEBUG
console.log('ignoring setsockopt command');
+#endif
return 0;
},
@@ -8665,7 +8667,9 @@ LibraryManager.library = {
},
setsockopt: function(fd, level, optname, optval, optlen) {
+#if SOCKET_DEBUG
console.log('ignoring setsockopt command');
+#endif
return 0;
},
diff --git a/src/library_sockfs.js b/src/library_sockfs.js
index 22fd8761..23641464 100644
--- a/src/library_sockfs.js
+++ b/src/library_sockfs.js
@@ -133,12 +133,42 @@ mergeInto(LibraryManager.library, {
} else {
// 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);
diff --git a/src/settings.js b/src/settings.js
index 1c41676d..6e644a54 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -225,6 +225,19 @@ var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js).
var SOCKET_DEBUG = 0; // Log out socket/network data transfer.
var SOCKET_WEBRTC = 0; // Select socket backend, either webrtc or websockets.
+// As well as being configurable at compile time via the "-s" option the WEBSOCKET_URL and WEBSOCKET_SUBPROTOCOL
+// settings may configured at run time via the Module object e.g.
+// Module['websocket'] = {subprotocol: 'base64, binary, text'};
+// Module['websocket'] = {url: 'wss://', subprotocol: 'base64'};
+// Run time configuration may be useful as it lets an application select multiple different services.
+var WEBSOCKET_URL = 'ws://'; // A string containing either a WebSocket URL prefix (ws:// or wss://) or a complete
+ // RFC 6455 URL - "ws[s]:" "//" host [ ":" port ] path [ "?" query ].
+ // In the (default) case of only a prefix being specified the URL will be constructed from
+ // prefix + addr + ':' + port
+ // where addr and port are derived from the socket connect/bind/accept calls.
+var WEBSOCKET_SUBPROTOCOL = 'binary'; // A string containing a comma separated list of WebSocket subprotocols
+ // as would be present in the Sec-WebSocket-Protocol header.
+
var OPENAL_DEBUG = 0; // Print out debugging information from our OpenAL implementation.
var GL_ASSERTIONS = 0; // Adds extra checks for error situations in the GL library. Can impact performance.