I played around with emscripten's socket wrapper a while ago, unfortunately closed-source work stuff, but it wasn't that difficult.
On the emscripten side, there's a simple BSD socket API wrapper, which can be initialized and used like typical TCP sockets, but actually uses WebSockets internally. The WebSocket protocol is not a stream protocol like TCP, but has implicit message framing (so basically a small header with size and checksum is added to the payload if I remember right). On the remote side, you need some wrapper code which understands the websocket protocol. You can either write your own from scratch which is really quite simple (see here: http://www.gamasutra.com/view/news/129227/Indepth_Writing_your_own_WebSocket_server.php), or use an existing proxying solution like websockify or libwebsocket. The way I did it was basically that I had a C++ server which opened 2 TCP ports, one for normal TCP traffic from 'native clients' and another port which understands the WebSocket protocol for emscripten clients. Both would decode into the same message queue, so that on server side you wouldn't need to care whether emscripten or native clients connected. I've actually been thinking to also use the WebSocket protocol for native clients, since most of the time you need some sort of message framing anyway, and WebSockets have pretty low-overhead message headers. If you need UDP instead of TCP, I think you're best off with the emscripten enet port which is based on WebRTC data channels. But I haven't tried this out yet. PS: also see this 2013 thread: https://groups.google.com/forum/#!topic/emscripten-discuss/-ErFfngAIC4 Good luck :) -Floh. Am Mittwoch, 7. Oktober 2015 19:07:39 UTC+2 schrieb Robert Goulet: > > And besides, I forgot to mention the server is running on Windows native, > not JavaScript, so that wouldn't work. > > On Wednesday, October 7, 2015 at 12:56:21 PM UTC-4, Robert Goulet wrote: >> >> I was hoping we could stay in C++ because our game engine is cross >> platforms, and that simplified things. Oh well. >> >> On Wednesday, October 7, 2015 at 12:51:47 PM UTC-4, Boris Sergeev wrote: >>> >>> Normally, you'd use networking facilities of the run-time environment >>> your JS code is executed in, then pass the received/sent data to/from your >>> emscripted C++ code. There is overhead of converting strings between C++ >>> (Emscripten heap) and native JS strings, but that's the price you pay for >>> the luxury of running C++ as JS. >>> >>> On Wednesday, October 7, 2015 at 8:53:35 AM UTC-6, Robert Goulet wrote: >>>> >>>> Hi there, >>>> >>>> is there any good documentation about how to use basic libc networking >>>> code with Emscripten? I can't find more precise documentation about it. >>>> >>>> I'm using basic libc networking functions, and when I call connect to >>>> a network location, I'm receiving an HTTP GET request on the server side. >>>> From what I understand, it's using a WebSocket protocol? So my question >>>> is, >>>> do I really have to parse the data and do the whole handshake stuff for >>>> WebSocket, even thought I'm just opening a simple socket where I just want >>>> to transfer my own private data? Isn't that a bit cumbersome? >>>> >>>> I'm not an expert of networking, so I'd like some advice about how I'm >>>> supposed to properly handle this. >>>> >>>> Thanks! >>>> >>> -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
