diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-11-06 11:50:49 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-11-06 11:50:49 -0800 |
commit | 663075274d30ae406734e7eb5ef8b07c9340e9a6 (patch) | |
tree | 18206b36b421b1a2f099284f8b619237ff15cc22 | |
parent | c0f26e65da33a6437a04d1adba19966f98fd79cb (diff) | |
parent | b1a0b6eddf91ff1c1fc85081273c6c8325541113 (diff) |
Merge pull request #1743 from fadams/fix-nodejs-socket-client
add support for node.js to act as a socket client
-rw-r--r-- | src/library_browser.js | 20 | ||||
-rw-r--r-- | src/library_sockfs.js | 6 | ||||
-rw-r--r-- | tests/test_sockets.py | 26 |
3 files changed, 42 insertions, 10 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index 71923743..b70dbc84 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -360,15 +360,19 @@ mergeInto(LibraryManager.library, { }, requestAnimationFrame: function(func) { - if (!window.requestAnimationFrame) { - window.requestAnimationFrame = window['requestAnimationFrame'] || - window['mozRequestAnimationFrame'] || - window['webkitRequestAnimationFrame'] || - window['msRequestAnimationFrame'] || - window['oRequestAnimationFrame'] || - window['setTimeout']; + if (typeof window === 'undefined') { // Provide fallback to setTimeout if window is undefined (e.g. in Node.js) + setTimeout(func, 1000/60); + } else { + if (!window.requestAnimationFrame) { + window.requestAnimationFrame = window['requestAnimationFrame'] || + window['mozRequestAnimationFrame'] || + window['webkitRequestAnimationFrame'] || + window['msRequestAnimationFrame'] || + window['oRequestAnimationFrame'] || + window['setTimeout']; + } + window.requestAnimationFrame(func); } - window.requestAnimationFrame(func); }, // generic abort-aware wrapper for an async callback diff --git a/src/library_sockfs.js b/src/library_sockfs.js index a57a51a1..bc3aa997 100644 --- a/src/library_sockfs.js +++ b/src/library_sockfs.js @@ -138,7 +138,9 @@ mergeInto(LibraryManager.library, { console.log('connect: ' + url); #endif // the node ws library API is slightly different than the browser's - var opts = ENVIRONMENT_IS_NODE ? {} : ['binary']; + 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); ws.binaryType = 'arraybuffer'; } catch (e) { @@ -573,4 +575,4 @@ mergeInto(LibraryManager.library, { } } } -});
\ No newline at end of file +}); diff --git a/tests/test_sockets.py b/tests/test_sockets.py index d2bc46a2..e1caa150 100644 --- a/tests/test_sockets.py +++ b/tests/test_sockets.py @@ -400,3 +400,29 @@ class sockets(BrowserCore): expected = '1' self.run_browser(host_outfile, '.', ['/report_result?' + e for e in expected]) + def test_nodejs_sockets_echo(self): + # This test checks that sockets work when the client code is run in Node.js + # Run with ./runner.py sockets.test_nodejs_sockets_echo + if not NODE_JS in JS_ENGINES: + return self.skip('node is not present') + + sockets_include = '-I'+path_from_root('tests', 'sockets') + + # Websockify-proxied servers can't run dgram tests + harnesses = [ + # Websockify doesn't seem to like ws.WebSocket clients TODO check if this is a ws issue or Websockify issue + #(WebsockifyServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), [sockets_include], 49160), 0), + (CompiledServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), [sockets_include, '-DTEST_DGRAM=0'], 49161), 0), + (CompiledServerHarness(os.path.join('sockets', 'test_sockets_echo_server.c'), [sockets_include, '-DTEST_DGRAM=1'], 49162), 1) + ] + + for harness, datagram in harnesses: + with harness: + Popen([PYTHON, EMCC, path_from_root('tests', 'sockets', 'test_sockets_echo_client.c'), '-o', path_from_root('tests', 'sockets', 'client.js'), '-DSOCKK=%d' % harness.listen_port, '-DREPORT_RESULT=int dummy'], stdout=PIPE, stderr=PIPE).communicate() + + self.assertContained('do_msg_read: read 14 bytes', run_js(path_from_root('tests', 'sockets', 'client.js'), engine=NODE_JS)) + + # Tidy up files that might have been created by this test. + try_delete(path_from_root('tests', 'sockets', 'client.js')) + try_delete(path_from_root('tests', 'sockets', 'client.js.map')) + |