-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathskynet.js
59 lines (48 loc) · 1.8 KB
/
skynet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function skynet (config, cb) {
if (!cb && typeof config === 'function') {
cb = config
config = {}
}
function loadScript(url, callback)
{
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
}
var authenticate = function() {
var socket = io.connect('http://skynet.im', {
port: 80
});
// var socket = io.connect('localhost', {
// port: 3000
// });
socket.on('connect', function(){
console.log('Requesting websocket connection to Skynet');
socket.on('identify', function(data){
console.log('Websocket connecting to Skynet with socket id: ' + data.socketid);
//console.log('Sending device uuid: ' + config.uuid);
if (config.uuid && config.token) socket.emit('identity', {uuid: config.uuid, socketid: data.socketid, token: config.token});
else socket.emit('register', config, function (ident) {
config = ident
socket.emit('identity', {uuid: config.uuid, socketid: data.socketid, token: config.token});
console.log(config)
})
});
socket.on('notReady', function(data){
cb(new Error('Authentication Error'));
});
socket.on('ready', function(data){
cb(null, socket);
});
});
};
loadScript("http://skynet.im/socket.io/socket.io.js", authenticate);
};