Check out the Red programming language from the same author

OS News is announcing Cheyenne new Web Socket support!

Nenad Rakocevic - Softinnov
4-Jan-2010 13:04:55 GMT

Article #20
Main page || Index || 2 Comments


You may have heard about Web Sockets. This extension of the HTTP protocol provides a mean for having long-lasting connections between client browsers and web servers in a clean and efficient way. 

Cheyenne jumped a few days ago in the new real-time web bandwagon by, not only supporting the new protocol, but also providing a new simple framework for those new kind of web applications. This new framework integrates nicely with the existing RSP engine benefiting from its multitasking abilities.

OS News published a nice article yesterday, written by Kaj de Vos from the Syllable OS team, announcing that Cheyenne supports now web sockets. Thanks to Kaj for pushing it up.

You can checkout this new version from the SVN repository (see Download page for info on how to checkout).

Chrome 4 is currently required to use Web Sockets. Hopefully, more browser will support it soon as part of the HTML5 feature set.

A fully commented simple "echo" application example is provided, full documentation will come in a few days. Also, a simple demo real-time chat application is available both online and in SVN for testing.

Here's the full server-side source code of the Chat demo application :

install-socket-app [
    name: 'chat

    ;-- stores [port! string!] pairs
    users:   make block! 10
    history: make block! 50

    ;-- send same data to all connected users
    broadcast: func [msg][
        foreach port clients [send port msg]
    ]

    on-connect: func [client][
        ;-- send messages' history to new user
        foreach entry history [send client entry]

        ;-- send connected users list
        foreach [port user] users [send client user]
    ]

    on-message: func [client data][
        ;-- escape all html tags for security concerns
        data: copy data
        replace/all data "<" "&lt;"
        replace/all data ">" "&gt;"

        switch data/1 [
            #"m" [
                ;-- insert [hh:mm:ss] time prefix
                insert next data join remold [now/time] " "
                append history data

                ;-- keep only 50 messages in history
                if 50 <= length? history [remove history]
            ]
            #"u" [
                if not find users data [
                    ;-- keep users list updated
                    repend users [client data]
                ]
            ]
        ]
        ;-- broadcast messages to all users
        broadcast data
    ]

    on-disconnect: func [client /local pos user][
        pos: find users client
        user: pos/2
        remove/part pos 2

        ;-- send user quit message to everyone
        broadcast head change user #"r"
    ]
]
Hope you'll enjoy this new toy. ;-)



© Nenad Rakocevic - Softinnov