I have implemented a small example shoving how to use the websocket support in Aleph, the asynchronous webframework for Clojure built on Netty.
The example is tested with Chrome and Firefox on Ubuntu. It should work in all modern browsers as it relies on web-socket-js for websocket emulation in browsers that do not have native support. Please note the updated Usage instructions. The socket-policy-server necessary for Flash websocket emulation has to listen on port 843 (at least that is the first place Flash asks for the policy file) so the server has to be run using sudo.
If you are interested the example is at hosted on github, along with usage instructions.
Nice work! that’s such a tiny amount of code
The flash client requests a policy file from port 843 first and then after that from the port you are connecting to. You can run a simple server on port 843 using the socat command:
DATA=”echo \’\'”
/usr/bin/socat -T 1 TCP-L:843,reuseaddr,fork,crlf SYSTEM:”$DATA”
It probably wouldn’t be that hard to create an Aleph policy server that served the “file” on port 843. Just remember to close the socket after sending the data.
Thanks for the tip. Zach is working on support for a spf server in Aleph at the moment. I jerry-rigged a solution that works for the moment:
(def *spf* "<cross-domain-policy><allow-access-from domain='*' to-ports='*'/></cross-domain-policy>\n\n\0")(defn spf-handler [channel connection-info]
(receive-all channel (fn [req]
(if (= ""
(byte-buffer->string req))
(let [f (string->byte-buffer *spf*)]
(enqueue-and-close channel f))
(println (byte-buffer->string req))))))
(defn start-policy-server []
(start-tcp-server spf-handler {:port 843}))
Unfortunately that only gets me a bit further as web_socket.js (from web-socket-js) fails with
[WebSocket] header Sec-WebSocket-Origin is missingWould you know what is causing the error? I found some posts on this being connected to an upgrade to the standard, I i thought web-socket-js had been changed to accommodate this.
Great. However I seem to be getting a “Can’t receive from a closed channel.”-error when loading the page. Any idea what might be the cause?
Thanks
I will update the Usage section on github to clarify that the index.html file is not served by a webserver and should be opened using the “Open File” method in the browser.
I’m getting “The websocket is not open!” in both FF 3.6 and 4 when opening as a file.
I see this when I start the server: #
When I output socket.readyState I see 0.
Any ideas on what the problem could be?
Sorry, I see
#
When starting, and when I go to localhost:10000 I get “Can’t receive from a closed channel.” in the repl which was expected so the server seems to be running properly anyway.
Third attempt: netty$start_server$fn__1985 aleph.netty$start_server$fn__1985@60227bd0
Sorry for the confusion. Sometimes it’s too quick. I didn’t think about the fact that I was running FlashBlock in FF 3.6, I also thought I could run a downloaded binary of FF 4 but the launch script would still run 3.6 and I didn’t check that I was actually running 4 and not 3.6.
I’ve now installed version 4 through a PPA:
sudo add-apt-repository ppa:ubuntu-mozilla-daily/ppa && sudo apt-get update
sudo apt-get install firefox-4.0 (it will show in the menu as the mine sweeper browser)
And for good measure I’ve also turned off flashblock in it even though it shouldn’t really matter. This setup works.