Last active
August 29, 2015 14:03
-
-
Save chrismatthieu/7956141689f0f71c4c5c to your computer and use it in GitHub Desktop.
skynet.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 network; | |
if(config.host && config.port){ | |
network = config.host + ":" + config.port; | |
} | |
var socket = io(network || "http://skynet.im"); | |
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); | |
cb(null, socket, data); | |
}); | |
}); | |
}; | |
// // loadScript("http://skynet.im/socket.io/socket.io.js", authenticate); | |
// loadScript("https://skynet.im/socket.io/socket.io.js", authenticate); | |
loadScript("http://192.168.112.21:3000/socket.io/socket.io.js", authenticate); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment