Last active
February 26, 2016 14:58
-
-
Save littlefuntik/b91eaff582a60f62a2fc to your computer and use it in GitHub Desktop.
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
document.title = '[WS] Connected..'; | |
(function(){ | |
// config | |
var HOST = 'example.com' | |
var PORT = 3311 | |
var USER_ID = 1000; | |
// connect | |
var ns = io.connect('http://' + HOST + ':' + PORT + '/drivers?userId=' + USER_ID, { | |
"forceNew": true | |
// ,"reconnect": false | |
// ,"reconnection": false | |
// ,"reconnectionAttempts": -1 | |
}) | |
window.ns = ns; | |
if (!ns._callbacks["error"]) { | |
// connect | |
ns.on('connect', function (){ | |
console.log('Connection complete'); | |
// availables | |
ns.on('availables', function(usersIds){ | |
console.log('Availables users: ' + usersIds.join(', ')); | |
}) | |
// available on | |
ns.on('available on', function(data){ | |
console.log('User is now available: ' + data.user_id); | |
}) | |
// available off | |
ns.on('available off', function(data){ | |
console.log('User became unavailable: ' + data.user_id); | |
}) | |
// update location | |
ns.on('update location', function(location){ | |
console.log('User#' + location.user_id + ', latitude ' + location.latitude + ', longitude: ' + location.longitude) | |
}) | |
// emit | |
ns.emit('update location', {latitude: 1.23, longitude: 2.123}) | |
// disconnect | |
// ns.disconnect() | |
}) | |
// error | |
ns.on('error', function (){ | |
console.log('Fail connection') | |
}) | |
// disconnect | |
ns.on('disconnect', function (){ | |
console.log('You are disconnected') | |
}) | |
} | |
})() | |
'Start script..' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment