Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Last active February 26, 2016 14:58
Show Gist options
  • Save littlefuntik/b91eaff582a60f62a2fc to your computer and use it in GitHub Desktop.
Save littlefuntik/b91eaff582a60f62a2fc to your computer and use it in GitHub Desktop.
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