About Monkey 2 › Forums › Monkey 2 Programming Help › Emscripten sockets
This topic contains 22 replies, has 3 voices, and was last updated by
DruggedBunny
1 year, 4 months ago.
-
AuthorPosts
-
December 5, 2017 at 1:15 am #12122
Looking at my Pastebin minimal WebSockets server code, I think the HTTP 1.1 client code probably should at least start to connect to a WebSockets server when run from a standard HTTP-supporting platform, at which point the server sends back “HTTP/1.1 101 Switching Protocols” and the server/client should then implement WebSockets protocols. (A true browser-based WebSockets client would not have to do the ‘manual’ HTTP connect-and-switch-protocol step — the browser presumably handles this part behind the scenes.)
Note that my HTTP 1.1 client actually has nothing to do with WebSockets, though if you add the check for 101 response I think it should get that far — on non-browser platforms.)
However, I think it unlikely that Emscripten translates HTTP sockets requests to WebSockets, so think it very doubtful the HTTP 1.1 client could be expected to work from the HTML/JS platforms.
Plenty links in the Pastebin source, including a proper 3rd-party test server!
December 5, 2017 at 1:26 am #12123Yeah, reading up a bit more on this I think websockets should probably be able to connect to port 80, but are unlikely to be able to perform a GET.
For now, I have asked some pertinent questions about emscripten sockets in the emscripten discussion group and will look into it further when I get some time.
Anyway, off to buy a rubber ducky.
December 5, 2017 at 1:27 am #12124Just did massive editing there for clarity.
I think in summary that WebSockets do the initial handshake over HTTP, then switch to an almost UDP-like ‘raw data’ protocol.
December 5, 2017 at 7:44 pm #12149I’ve had a look into this and I am basically WAY out of my depth here! Network stuff is not my strong point in the first place, and I find 73% of all this utterly confusing…
Concering the ‘connect’ error:
There is apparently no way to test a normal ‘connect’, because emscripten sockets must run in non-blocking mode, which means connect always fails with “in progress” errors, which is apparently ‘success’ according to some sample emscripten code I’ve seen! So I can ‘sort of’ fix connect errors (by just ignoring certain errors) but of course that doesn’t mean I’m not getting an actual error. But then what? I really have no idea what will work or, more importantly, what even should work, eg: how/where do I send data now that I have a connection?
Concerning websocket ‘servers’:
What exactly does your websocket server do James?
My knowledge of websockets is that they provide a ‘mesage based’ protocol on top on TCP, ie: a SendMsg, SendReply, RecvReply style API, and I can see how emscripten could potentially convert a plain socket stream to this using 2 message ports and ’empty’ replies. But I don’t see how your server code achieves the message protocol (although I haven’t looked that closely TBH). Does it? And if it does, how is it supposed to be used? When does the server ‘app logic’ get to process the SendMsg data (that was sent by a client) and provide a ReplyMsg reply? And if an emscripten app writes to a socket which emscripten converts to a SendMsg which gets sent to your server what happens? Or am I just way off here WRT how this all works? Ok, thinking about it more, it’ll be the websocket connection ‘peer’ that processes msgs/send replies? So does the sever code then just transfer msgs/replies between peers?
Concerning websockify:
Everyone in the emscripten discussion group who has successfuly used sockets (both of them!) recommends using something called ‘websockify’ which, AFAICT, converts websockets<->TCP sockets and allows us to treat emscripten sockets as much like ‘real’ sockets as possible. I have no idea what’s involved with setting it up, although I gather it also involves having access to a ‘real’ server.
https://linux.die.net/man/1/websockify
I’m not sure if this is 100% required, but it apparently makes things a whole lot easier.
Concerning next steps:
I’m willing to continue looking into this but I’ll definitely need help, ideas for things to try, perhaps someone to set up a websocket server, or even websockify and a plain server. I just don’t have the skills to do this alone.
December 6, 2017 at 12:14 am #12157Hi Mark, the code was nowhere near a complete WS server, just the beginnings.
Looking at it again, I think it implemented the initial HTTP handshake, then, after telling the (HTTP) client to switch to WebSocket protocol (via “”HTTP/1.1 101 Switching Protocols”), simply listened for incoming ‘data frames’ from the client, a data frame being a binary format describing the purpose of the incoming chunk of data.
As far as I can tell now, it should have printed the type of data frame (“opcode”), and (possibly?) the raw data of that frame. I didn’t get as far as implementing messaging, so just shows the guts of starting a WebSockets session from a server’s perspective, how it runs behind-the-scenes over HTTP, etc.
A Javascript app wouldn’t do all of the HTTP handshaking stuff. I think in JS you just ask for a WebSocket session, then go straight to the WebSockets messaging stuff. (The browser would handle the HTTP handshaking stuff with the WS server.)
I didn’t think a plain WS client can make requests to a HTTP server, as in download files, etc, but that websockify seems to suggest otherwise!
I could probably set up a basic server (HTTP and/or WS), but will definitely be occupied the rest of this week, and probably most of the weekend, with a house move (not mine!).
(For what it’s worth, I don’t personally have a pressing need for this, was just trying to provide some more info!)
December 6, 2017 at 12:30 am #12158Here’s my horribly bad and very quickly thrown together WebSocket module (server and client) for BlitzMax:
https://bitbucket.org/Hezkore/blitzmax-websocketIt builds upon FWeinb’s older WebSocket module and uses Vertex.BNetEx and Brucey’s Base64, but they’re included.
The “sample” folder has a working server example, combined with the same HTML chat I shared from above.
Sadly, I don’t have BlitzMax installed, nor do I have the server compiled.The whole thing is a flipping mess, but “websocket.bmx” has some good stuff in it.
December 6, 2017 at 2:02 am #12161I could probably set up a basic server (HTTP and/or WS), but will definitely be occupied the rest of this week, and probably most of the weekend, with a house move (not mine!).
No problem, whenever’s good. It’s just I suspect you know much more about this websocket server stuff than I do and I could really do with some help here.
December 6, 2017 at 11:30 am #12176Actually, this thread probably contains the sum total of my knowledge! Happy to have a go at setting up a test server, though, can’t see that being a problem.
-
AuthorPosts
You must be logged in to reply to this topic.