Menu

CometStream

Anonymous

NEW Aeon Alpha is an example of Comet Stream on rupy.

You can now stream messages over HTTP with rupy.

As RFC I supply this reference implementation. - Termination is now \n for this text based implementation.

The message energy performance per second of the test included above:

CPU
usage
in
out
total
watt
mess./joule
arch.

3770S
0%
4.000
9.000
13.000
/
20
=
650
x86

4010U
7.5%
3.000
6.000
9.000
/
10
=
900
x86

D510MO
50%
3.000
6.000
9.000
/
15
=
600
x86

RPi 2
12.5%
2.000
4.500
6.500
/
1
=
6.500
ARM

RPi
99%
300
700
1.000
/
2
=
500
ARM

You can try it quickly by going to this flash demo:

Flash: stream (see code below) (server is in Helsingborg, Sweden)

Or right-clicking these two links and opening them
in different tabs/windows/devices (so you can see the stream in real-time):

HTTP | one | < "hello" | two | - Chrome doesn't render without a html tag now. Now it does again.

event-stream | one | < "hello" | two |

You will then send "hello" from two to one.
Reloading two will resend the message.
You can play around with more windows
and name=three&message=hi values.

If it doesn't work, try later, somebody else might
be trying it at the same time.

IE doesen't render until a certain amount is
received, so hold down F5 in two for a while.

This code allows you to receive dynamic chunks with flash:

package {
    import flash.display.*;
    import flash.system.*;
    import flash.errors.*;
    import flash.events.*;
    import flash.utils.*;
    import flash.text.*;
    import flash.net.*;

    public class stream extends flash.display.MovieClip {
        var host = "talk.rupy.se";
        var port = 80;
        var pull, push; // sockets
        var id = 1;
        var output;
        var first = true;
        var length = 120;
        var sleep = 20;
        var start;

        public function stream() {
            output = new TextField();
            output.width = 400;
            output.height = 400;
            output.multiline = true;

            addChild(output);

            output.appendText("security...");

            pull = socket(pull_data, pull_connect);
        }

        function socket(data, connect):Socket {
            try {
                var socket = new Socket();
                socket.addEventListener(IOErrorEvent.IO_ERROR, error);
                socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, error);
                socket.addEventListener(ProgressEvent.SOCKET_DATA, data); 
                socket.addEventListener(Event.CONNECT, connect);
                socket.connect(host, port);
                return socket;
            } catch(error) {
                output.appendText(error + "\n");
            }

            return null;
        }

        function pull_connect(event) {
            output.appendText(" done \n");

            start = new Date().getTime();

            pull.writeUTFBytes("GET /pull?name=one HTTP/1.1\r\n");
            pull.writeUTFBytes("Host: " + host + "\r\n");
            pull.writeUTFBytes("Head: less\r\n"); // enables TCP no delay
            pull.writeUTFBytes("\r\n");
            pull.flush();

            push = socket(push_data, push_connect);
        }

        function push_connect(event) {
            //var timer = new Timer(sleep, length);
            //timer.addEventListener(TimerEvent.TIMER, tick);
            //timer.start();

            tick(null);
        }

        function tick(event) {
            push.writeUTFBytes("POST /push HTTP/1.1\r\n");
            push.writeUTFBytes("Host: " + host + "\r\n");

            if(first) {
                push.writeUTFBytes("Head: less\r\n"); // tells rupy to not send any headers
                first = false;
            }

            push.writeUTFBytes("\r\n");
            push.writeUTFBytes("name=two&message=" + id++);
            push.flush();
        }

        function push_data(event) {
            var message = push.readUTFBytes(event.bytesLoaded);
            //output.appendText(message + "\n"); // HTTP/1.1 200 OK
            if(id <= length) {
                tick(null);
            }
            else {
                var latency = int((new Date().getTime() - start) / length);
                output.appendText("avg. packet latency " + latency + " ms.\n");
                output.scrollV+=10000;
            }
        }

        /* here we need to split on \n

         * or binary fixed length
         */
        function pull_data(event) {
            var message = pull.readUTFBytes(event.bytesLoaded);
            var array = message.split("\r\n");
            for(var i = 1; i < array.length; i += 2) {
                output.appendText(array[i]);
                output.scrollV+=10000;
            }
        }

        function error(event) {
            output.appendText(event + "\n");
        }
    }
}

Tutorial:

  • Create 2 new files:
    1. ActionScript - Paste the code above into it and save it as stream.as.
    2. Flash (AS 3.0) - Edit the Class to 'stream' in the PROPERTIES PUBLISH pane on the right, save in the same folder.
  • Then run Control -> Test Movie (Ctrl+Enter).

MongoDB Logo MongoDB