2 _bonescript.modules = {};
3 _bonescript._callbacks = {};
4 _bonescript._seqnum = 0;
6 _bonescript.on.connect = function(){};
7 _bonescript.on.connecting = function(){};
8 _bonescript.on.disconnect = function(){};
9 _bonescript.on.connect_failed = function(){};
10 _bonescript.on.error = function(){};
11 _bonescript.on.reconnect = function(){};
12 _bonescript.on.reconnect_failed = function(){};
13 _bonescript.on.reconnecting = function(){};
14 _bonescript.on.initialized = function(){};
16 var head = document.getElementsByTagName('head')[0];
17 var script = document.createElement('script');
18 script.type = 'text/javascript';
19 script.src = '___INSERT_HOST___/socket.io/socket.io.js';
20 script.charset = 'UTF-8';
21 var scriptObj = head.appendChild(script);
22 scriptObj.onload = _onSocketIOLoaded;
24 function _onSocketIOLoaded() {
25 //console.log("socket.io loaded");
26 var socket = io.connect('___INSERT_HOST___:80');
27 socket.on('require', getRequireData);
28 socket.on('bonescript', _seqcall);
29 socket.on('connect', _bonescript.on.connect);
30 socket.on('connecting', _bonescript.on.connecting);
31 socket.on('disconnect', _bonescript.on.disconnect);
32 socket.on('connect_failed', _bonescript.on.connect_failed);
33 socket.on('error', _bonescript.on.error);
34 socket.on('reconnect', _bonescript.on.reconnect);
35 socket.on('reconnect_failed', _bonescript.on.reconnect_failed);
36 socket.on('reconnecting', _bonescript.on.reconnecting);
37 socket.on('initialized', _bonescript.on.initialized);
39 function getRequireData(m) {
40 if(!m.module || !m.data)
41 throw('Invalid "require" message sent for "' + m.module + '"');
42 //console.log('Initialized module: ' + m.module);
43 _bonescript.modules[m.module] = {};
44 for(var x in m.data) {
45 if(!m.data[x].type || !m.data[x].name || (typeof m.data[x].value == 'undefined'))
46 throw('Invalid data in "require" message sent for "' + m.module + '.' + m.data[x] + '"');
47 if(m.data[x].type == 'function') {
48 // define the function
50 throw('Missing args in "require" message sent for "' + m.module + '.' + m.data[x] + '"');
51 var myargs = m.data[x].value;
53 // eval of objString builds the call data out of arguments passed in
55 for(var y in myargs) {
56 if(isNaN(y)) continue; // Need to find the source of this bug
57 if(myargs[y] == 'callback') continue;
58 objString += ' if(typeof ' + myargs[y] + ' == "function") {\n';
59 objString += ' ' + myargs[y] + ' = ' + myargs[y] + '.toString();\n';
61 objString += ' calldata.' + myargs[y] + ' = ' + myargs[y] + ';\n';
63 var argsString = myargs.join(', ');
64 var handyfunc = '_bonescript.modules["' + m.module + '"].' + m.data[x].name +
66 'function (' + argsString + ') {\n' +
67 ' var calldata = {};\n' +
70 ' _bonescript._callbacks[_bonescript._seqnum] = callback;\n' +
71 ' calldata.seq = _bonescript._seqnum;\n' +
72 ' _bonescript._seqnum++;\n' +
74 ' socket.emit("' + m.module + '$' + m.data[x].name + '", calldata);\n' +
78 _bonescript.modules[m.module][m.data[x].name] = m.data[x].value;
81 _bonescript.on.initialized();
85 function _seqcall(data) {
86 if((typeof data.seq != 'number') || (typeof _bonescript._callbacks[data.seq] != 'function'))
87 throw "Invalid callback message received: " + JSON.stringify(data);
88 _bonescript._callbacks[data.seq](data);
89 if(data.oneshot) delete _bonescript._callbacks[data.seq];
92 // Require must be synchronous to be able to return data structures and
93 // functions and therefore cannot call socket.io. All exported modules must
94 // be exported ahead of time.
95 function require(module) {
96 if(typeof _bonescript.modules[module] == 'undefined')
97 throw 'Module "' + module + '" is not currently available';
98 return(_bonescript.modules[module]);