Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sockets support in emscripten #6347

Closed
dudiko opened this issue Mar 19, 2018 · 4 comments
Closed

sockets support in emscripten #6347

dudiko opened this issue Mar 19, 2018 · 4 comments

Comments

@dudiko
Copy link

dudiko commented Mar 19, 2018

Hi,

I'm trying to write a simple client-server app in c/c++ and compile it to javascript using emscripten.
I read a lot about sockets in emscripten and I have to admit, there is a big mess!
Can please someone clarify the situation?

I understand that javascript language doesn't allow to use raw sockets and requires us to work with either HTTP or WebSocket.
So if I will write code in c/c++ that uses 'connect', 'listen' (for raw sockets) and will try to compile it using emscripten, it won't work, right? (already tried but just want to verify)
And what if I will write code in c/c++ that uses WebSocket library and will try to compile it using emscripten, I guess it won't work as well because WebSocket library internally uses raw sockets.
So, I'm wondering how should it be done? Do I need to write a pure javascript code for that mission?

Thanks,
Dudi

@dudiko
Copy link
Author

dudiko commented Mar 20, 2018

BTW, I noticed there are tests for simple client-server under:
https://github.com/kripken/emscripten/blob/master/tests/sockets/
but it doesn't work for me!

I compiled both client and server:

emcc test_sockets_echo_client.c -o client.js -D SOCKK=9991
emcc test_sockets_echo_server.c -o server.js -D SOCKK=9991

but for the client when I run:
nodejs client.js
I get:

connect failed: Host is unreachable
emscripten_force_exit cannot actually shut down the runtime, as the build has NO_EXIT_RUNTIME set

and nothing is being sent, not even SYN packet (I recorded on loopback interface)

and for the server when I run:
nodejs server.js
I get:

atexit() called, but NO_EXIT_RUNTIME is set, so atexits() will not be called. set NO_EXIT_RUNTIME to 0 (see the FAQ)
Calling stub instead of signal()
{ [Error: Cannot find module 'ws'] code: 'MODULE_NOT_FOUND' }
{ [Error: Cannot find module 'ws'] code: 'MODULE_NOT_FOUND' }
exception thrown: abort({"code":"MODULE_NOT_FOUND"}) at Error
    at jsStackTrace (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:1033:13)
    at stackTrace (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:1050:12)
    at abort (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15788:44)
    at ___syscall102 (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:5636:69)
    at _listen (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:14576:8)
    at _main (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:7534:9)
    at Object.asm._main (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15331:21)
    at Object.callMain (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15614:30)
    at doRun (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15672:60)
    at run (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15686:5)

/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:113
      throw ex;
      ^
abort({"code":"MODULE_NOT_FOUND"}) at Error
    at jsStackTrace (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:1033:13)
    at stackTrace (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:1050:12)
    at abort (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15788:44)
    at ___syscall102 (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:5636:69)
    at _listen (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:14576:8)
    at _main (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:7534:9)
    at Object.asm._main (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15331:21)
    at Object.callMain (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15614:30)
    at doRun (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15672:60)
    at run (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/1.js:15686:5)

which sounds legit cause there is no listen for javascript.

So, how should I make it work?

And of course, when I'm compiling with gcc, everything work just perfect

@kripken
Copy link
Member

kripken commented Mar 20, 2018

Do they pass when running in the test suite? ./tests/runner.py sockets.test_... (the test runner sets up the server with websockify etc. in a way that should work)

@dudiko
Copy link
Author

dudiko commented Mar 21, 2018

emscripten@dudi-Latitude-E7450:~/emsdk-portable/emscripten/tag-1.37.35/tests$ ./runner.py sockets
WARNING:root:use EM_ALL_ENGINES=1 in the env to run against all JS engines, which is slower but provides more coverage
Test suites:
['test_sockets']
Running test_sockets: (21 tests)
ERROR

======================================================================
ERROR: setUpClass (test_sockets.sockets)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/test_sockets.py", line 132, in setUpClass
    super(sockets, self).setUpClass()
  File "/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/runner.py", line 788, in setUpClass
    self.harness_queue = multiprocessing.Queue()
  File "/usr/lib/python2.7/multiprocessing/__init__.py", line 218, in Queue
    return Queue(maxsize)
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 63, in __init__
    self._rlock = Lock()
  File "/usr/lib/python2.7/multiprocessing/synchronize.py", line 147, in __init__
    SemLock.__init__(self, SEMAPHORE, 1, 1)
  File "/usr/lib/python2.7/multiprocessing/synchronize.py", line 75, in __init__
    sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
OSError: [Errno 38] Function not implemented

----------------------------------------------------------------------
Ran 0 tests in 0.002s

FAILED (errors=1)

BUT, I succeed! Apparently what was missing for me was to install ws library for nodejs...
and there is a significant progress...

Now, the client is sending traffic but the server crushes:

emscripten@dudi-Latitude-E7450:~/emsdk-portable/emscripten/tag-1.37.35/tests/sockets$ nodejs server.js 
atexit() called, but NO_EXIT_RUNTIME is set, so atexits() will not be called. set NO_EXIT_RUNTIME to 0 (see the FAQ)
Calling stub instead of signal()
/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/server.js:113
      throw ex;
      ^

TypeError: Cannot read property 'binary' of undefined
    at WebSocket.<anonymous> (/home/emscripten/emsdk-portable/emscripten/tag-1.37.35/tests/sockets/server.js:4713:26)
    at WebSocket.emit (events.js:180:13)
    at Receiver.receiverOnMessage (/home/node_modules/ws/lib/websocket.js:715:20)
    at Receiver.emit (events.js:180:13)
    at Receiver.dataMessage (/home/node_modules/ws/lib/receiver.js:405:14)
    at Receiver.getData (/home/node_modules/ws/lib/receiver.js:346:17)
    at Receiver.startLoop (/home/node_modules/ws/lib/receiver.js:133:22)
    at Receiver._write (/home/node_modules/ws/lib/receiver.js:69:10)
    at doWrite (_stream_writable.js:406:12)
    at writeOrBuffer (_stream_writable.js:392:5)

and the wireshark shows:

GET / HTTP/1.1
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: SXJSB2nb4zZlEnjGiZAexg==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: 127.0.0.1:1337

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: hEKNs+pm+aMovfHFBLsgEVAbK9k=

..y. .w. ....aPf..>...$...?..a

Any explanation?

@dudiko dudiko closed this as completed Mar 22, 2018
@ternaam
Copy link

ternaam commented Oct 8, 2018

Seems related to #5971
@dudiko did you find a workaround eventually?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants