-
Notifications
You must be signed in to change notification settings - Fork 20
Javascript api
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.
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);
*/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
- http://dev.w3.org/html5/websockets/
- http://www.w3schools.com/html/html5_webworkers.asp
- http://autobahn.ws/js/
- http://www.smashingmagazine.com/2012/10/09/designing-javascript-apis-usability/
- http://webstandardssherpa.com/reviews/secrets-of-awesome-javascript-api-design
- http://blog.filepicker.io/post/34767202473/designing-javascript-apis
- Home
- C/C++ API
- Basic Functions
- Node Functions
- Tag Functions
- Layer Functions
- Python API
- Session Methods
- Node Methods
- Tag Group Methods
- Tag Methods
- Layer Mehods
- Specification
- Data Types
- Transport Layer
- Security
- Structure of Packet and Message
- User Authentication
- Negotiation
- Handshake of Datagrame Connection
- Teardown of Datagrame Connection
- Resend Mechanism
- Congestion Control
- Flow Control
- TCP Variant
- Websocket Variant
- Node Commands
- Tag Group Commands
- Tag Commands
- Layer Commands