Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
jirihnidek edited this page Sep 4, 2014 · 1 revision

The Verse Javascript library should be able to receive messages from Verse server and call corresponding callback functions.

There are two proposal of API. Both APIs are demonstrated on example of simple application that is able to subscribes to all nodes and tag groups and create one node with one tag group and one tag.

Low Level Proposal

var session = new verse.Session({
    url: 'ws://127.0.0.1:9000/',
    username: 'joe',
    password: 'secret_password'
});

session.on_connect = function (session) {

    // Callback function called, when connection is accepted by Verse server
    function on_connect_accept(session_id, avatar_id) {
        session.session_id = session_id; // ?
        session.avatar_id = avatar_id;  // ?
        // Important: subscribe to Root node
        session.send_node_subscribe(0, 0, 0);
        // Create test node
        var my_node_custom_type = 3333;
        session.send_node_create(my_node_custom_type);
        // Create hash table for nodes
        session.nodes = new Object;
        console.log('Connection accepted', session_id, avatar_id);
    }

    // Callback function called, when connection is terminated by Verse server
    function on_connect_terminate(error) {
        console.log('Session terminated: ', error);
    }

    // Callback function called, when new node was created at Verse server
    function on_node_create(node_id, parent_id, user_id, custom_type) {
        // Create test tag group
        if(session.avatar_id == parent_id && custom_type == 3333) {
            var tag_group_custom_type = 4444;
            var tag_group = new session.TagGroup(node_id,
                tag_group_custom_type);
            session.my_node_id = node_id;
        }
        // Save reference on node
        session.nodes[node_id] = node = new session.Node(node_id, parent_id,
            user_id, custom_type);
        // Subscribe to node
        session.send_node_subscribe(node_id, 0, 0);
        console.log('Node created: ', node_id, parent_id, user_id,
            custom_type);
    }

    // Callback function called, when new tag group was created at
    // Verse server
    function on_tag_group_create(node_id, tag_group_id, custom_type) {
        // Create test tag
        if(node_id == session.my_node_id) {
            var value_type = 'UINT32';
            var value_count = 1;
            var tag_custom_type = 5555;
            session.send_tag_create(node_id, tag_group_id, value_type,
                value_count, tag_custom_type);
            session.my_tag_group_id = tag_group_id;
            session.my_tag = tag;
        }
        // Save reference at tag group
        node = session.nodes[node_id];
        if(!'tag_groups' in node) {
            node.tag_groups = new Object();
        }
        node.tag_groups[tag_group_id] = new session.TagGroup(node_id,
            tag_group_id, custom_type);
        // Subscribe to tag group
        session.send_tag_group_subscribe(node_id, tag_group_id, 0, 0);
        console.log('Tag group created: ', node_id, tag_group_id,
            custom_type);
    }

    function on_tag_create(node_id, tag_group_id, tag_id, data_type, count,
        custom_type)
    {
        // Send value to test tag
        if(node_id == session.my_node_id &&
            tag_group_id == session.my_tag_group_id)
        {
            // Send value
            session.send_tag_set_value(node_id, tag_group_id, tag_id, 10);
        }
        // Save reference at tag
        node = session.nodes[node_id];
        tag_group = node.tag_groups[tag_group_id];
        if(!'tags' in tag_group) {
            tag_groups.tags = new Object();
        }
        tag_groups.tags[tag_id] = new session.Tag(node_id,
            tag_group_id, tag_id, data_type, count, custom_type);
        console.log('Tag created: ', node_id, tag_group_id, tag_id, data_type,
            count, custom_type);
    }
};

session.connect();

/*
var my_node_custom_type = 3333;
var node = new session.send_node_create(my_node_custom_type);
*/

High Level Proposal

var session = new verse.Session({
    url: 'ws://127.0.0.1:9000/',
    username: 'joe',
    password: 'secret_password',
    subscribe: {node: '*', tg: '*', layer: ''}
});

session.connect();

var node = new session.Node(3333);
var tag_group = new session.TagGroup(node, 4444);
var tag = new.Tag(tag_group, 'UINT32', 1, 5555);
tag.value(10);

##References

  1. http://dev.w3.org/html5/websockets/
  2. http://www.w3schools.com/html/html5_webworkers.asp
  3. http://autobahn.ws/js/
  4. http://www.smashingmagazine.com/2012/10/09/designing-javascript-apis-usability/
  5. http://webstandardssherpa.com/reviews/secrets-of-awesome-javascript-api-design
  6. http://blog.filepicker.io/post/34767202473/designing-javascript-apis

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.