I've made some updates to the webSockets lib ws.js #569
Replies: 18 comments
-
Posted at 2015-12-19 by tve Thank you! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-12-21 by @gfwilliams Thanks! Were you using the latest (Git) builds of Espruino? The current (1v84) version had a bug where it didn't sent the header immediately (why you needed the Still, if that fixed it for everyone without needing a tweaked firmware it's probably worth putting in the main websockets code :) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-12-21 by @gfwilliams Ok, I just pulled your changes into GitHub, and tweaked a few things around. It works great! I'll try and get the changes actually on the website today. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-05 by user7249 Hello Gordon, My espruino is monitoring some temperatures which I would like to stream via websocket to my dataserver. I tried some firmwares (also the latest cutting edge espruino_1v84.155_espruino_1r3_wiznet.bin) but have issues with it. Generally this latest version feels better then the older ones, but it freezes my espruino console on the left side of the Chrome IDE (it is not possible to type something into it, I use ist for reset() sometimes). My test script (as attached) is basically websocket trying to use setInterval every 2 seconds. If you run it with commented websocket part (line 17 - 37) the timer works and the script gets read in completely. If you uncomment lint 17-37 the setInterval timer never gets to run and the last console.log on line 40 sometimes shows up sometimes not (on startup). I really tried a lot of things and for me it looks like there is a problem with the event loop, maybe this ws.on('open',... ) never finishes and blocks the event loop (would explain the odd behaviour of the timer/console.logs). It would be great if you have an idea to put me in the right direction to solve this issue (I hope I haven't missed something and the problem is all mine). Many thanks and a happy / successful 2016! Best regards, Manuel
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-05 by DrAzzy Where is the code? I see no attachment |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by @gfwilliams As @drazzy says, I don't see any code - but if the console on the left-hand side isn't responding it's usually a problem with the upload. On Espruino, code is executed as it is uploaded, and if you write code that takes a long time to execute then it can cause the PC to wait too long to send data, and in the end the PC will just not send it. So what you need to do (in fact probably what you meant to do) is to make sure that your main code (initialising the network/socket connection?) happens in the When you do that you'll need to manually type Otherwise you might be able to go into the Web IDE's settings and click 'Throttle Send' - it'll slow down transmission and might avoid your problems without you having to change your code. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by user7249 File Upload seems to not work, I have included the short code into my question above. I'll try to use an onInit function in a moment. Thanks so far I will post my results. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by @gfwilliams Ahh, yes - that's definitely your issue. With WIZnet, when you create the Websocket, I think it connects immediately - which might cause a delay which would stop everything getting uploaded correctly. Even using a
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by user7249 Thank you Gordon for your helpful reply, it (your example) still is not working and "hangs" the espruino. I even put the whole thing into an onInit function without success:
The output is as follows: Halo So as long as ws = new WebSocket("192.168.0.6",... is not executed times work well and the event loop runs, but after 10 Seconds ws is created the ws.on("open", ... is executed correctly but afterwards the espruino hangs and does not execute any more, as well as I have to plug usb out and in again to renew the script. To me it seems on ws.on("open",... the event loop gets blocked. Another thing I encountered is on the general Info page (http://www.espruino.com/Notes) it says:
In my case I needed to run the function manually otherwise onInit did not get fired (see last line). Thanks for your help! Best regards Manuel |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by @gfwilliams Is the problem just that you don't have a WebSocket server running on 192.168.0.6 port 8080, or that Espruino can't connect to it for whatever reason? I think the connection takes quite a while before it times out, which will unfortunately block Espruino.
It's executed when loading from flash (if you save with The reason is that since |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-06 by user7249 I checked my dataserver running on 192.168.0.6 port 8080 and this nodejs part works as expected (it even gets the initial data sent on ws.on('open') perfectly, and also saves data from other sources too). Maybe I have time to check your code in the WebSocket library, like I mentioned above I think it dies there (or maybe it needs an acknowledge message from dataserver to finish). Tanks for your clarification about onInit. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-01-07 by @gfwilliams Hmm, that's strange then - sorry, I didn't pick up that it had Does Ctrl-C help? It might let you break and see where execution has stopped? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-07 by urbiman I had problems getting WS to work with the latest nodejs ws version (1.1.0) as a server. The trick for me was to add a "return" statement in the first if block of the "parseData" method (roughly line 117 of wsx.js) to prevent the other statements to execute and produce unexpected results in some cases. After I did this change the connection was not directly terminated any longer. My ultimate goal is to get it running together with cloudflare (as they now support websocket connections in free plans). Added the hostname to the handshake (otherwise reverseproxies can not handle the request) Further I also try handle binary data, however this is experimental and has nothing to do with cloudflare. Now cloudflare finally accepts the connection and even forwards my first message, however the connection is terminated right after that. When not using cloudflare (and connecting directly) everything works as it should. Does anyone have a clue what could be the problem? Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-08 by urbiman Ok, I fixed it myself. The reason why the code was disconnecting all the time when using cloudflare was the following: The handshake was transmitted in two chunks (each chunk has a wrapped around it here):
So what happend here was, that during the handshake one data packet was received with an "H" at the beginning (first chunk) that did not contain the "Accept" Header (that is in the second chunk) and so was interpreted as WS message. "H" in hexadecimal is "48" which is the worst that could happen as it means opcode "8" - terminate connection. I now changed the code accordingly so that chunks do not get interpreted as message as long as the handshake is not finished. Attachments: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by @gfwilliams Thanks! Is this based on the module at http://www.espruino.com/ws, or is it completely different now? It's be great to pull these changes back in... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by Wilberforce I'm happy to look at the integration, if you can spell out which parts have changed. When I have time I'll try to do a diff... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-09 by urbiman My JavaScript is based on the wsx.js from the first post here which should be widely identical to the current ws.js available from the espruino site so it is compatible to what is in place now. Just give me a few days as I would like to further improve the http handshake as it could still fail under certain circumstances and I would like to add support for larger messages (more than 127 chars). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2016-05-18 by Wilberforce I'm looking through your updates, and trying to follow: old line 128:
new:
Aren't these equivalent? and also here line 182sh:
and new:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-12-19 by Snerkle
Hi,
I've really wanted to use webSockets with the Espruino & esp8266 as an http/webSockets server
but I was having some issues, namely:
So I learnt more than I thought I would about the webSocket protocol and made the attached, simple changes that address these requirements (renaming the module to wsX.js just to avoid conflicts). A simple diff between the attached file and the hosted module will show you the few changes and I've added comments to explain, but feel free to remove these.
I'm now finding webSockets very reliable and doing what I need. I hope the changes I'm offering make it into the hosted module.
Attachments:
Beta Was this translation helpful? Give feedback.
All reactions