aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFraser Adams <fraser.adams@blueyonder.co.uk>2013-10-27 20:30:13 +0000
committerFraser Adams <fraser.adams@blueyonder.co.uk>2013-10-27 20:30:13 +0000
commit7a902d9e4d79e370f44f94a9a7f7ed45ba3dfbb2 (patch)
treead4d6721e331bdaf5ecfc8e77052014743f163d1
parent14c6628d28648cd069a9e0e519cc69513e3d470b (diff)
add support for node.js to act as a socket client
-rw-r--r--src/library_browser.js6
-rw-r--r--src/library_sockfs.js6
-rw-r--r--tests/test_sockets.py29
3 files changed, 37 insertions, 4 deletions
diff --git a/src/library_browser.js b/src/library_browser.js
index 59d2945e..5ec02eee 100644
--- a/src/library_browser.js
+++ b/src/library_browser.js
@@ -782,7 +782,11 @@ mergeInto(LibraryManager.library, {
}
} else {
Browser.mainLoop.scheduler = function() {
- Browser.requestAnimationFrame(Browser.mainLoop.runner);
+ if (typeof window === 'undefined') { // requestAnimationFrame will fail if window is undefined (e.g. in Node.js)
+ setTimeout(Browser.mainLoop.runner, 1000/60);
+ } else {
+ Browser.requestAnimationFrame(Browser.mainLoop.runner);
+ }
}
}
Browser.mainLoop.scheduler();
diff --git a/src/library_sockfs.js b/src/library_sockfs.js
index af29d11b..0e083bfd 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.
+ WebSocket = ENVIRONMENT_IS_NODE ? require('ws') : 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..36e2dfb6 100644
--- a/tests/test_sockets.py
+++ b/tests/test_sockets.py
@@ -1,5 +1,5 @@
import os, multiprocessing, subprocess
-from runner import BrowserCore, path_from_root
+from runner import BrowserCore, RunnerCore, path_from_root
from tools.shared import *
def clean_pids(pids):
@@ -400,3 +400,30 @@ class sockets(BrowserCore):
expected = '1'
self.run_browser(host_outfile, '.', ['/report_result?' + e for e in expected])
+class nodejs_sockets(RunnerCore):
+ def test_sockets_echo(self):
+ # This test checks that sockets work when the client code is run in Node.js
+ # Run with ./runner.py nodejs_sockets.test_sockets_echo
+ if not NODE_JS in JS_ENGINES:
+ return
+
+ 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'))
+