Skip to content

Instantly share code, notes, and snippets.

@ppcano
Created December 1, 2011 11:12
Show Gist options
  • Save ppcano/1415918 to your computer and use it in GitHub Desktop.
Save ppcano/1415918 to your computer and use it in GitHub Desktop.
configuring socket server
this.manager.configure( function (){
this.set('authorization', function (handshakeData, callback) {
console.log( 'inside authorization......');
var room_id = handshakeData.query.room;
var authorized = this.room_trigger[room_id]!=undefined;
var error = authorized ? undefined : 'room is not available';
callback(error, authorized);
});
});
this.manager.sockets.on('connection', function( socket ) {
var room_id = socket.handshake.query.room;
var room_name = socketio_room_name(room_id);
if ( !socket.manager.rooms[room_name] || !socket.manager.roomClients[socket.id][room_name] ) {
socket.room_id = room_id;
socket.join(socket.room_id);
console.log( 'Socket joined to room: '+ socket.room_id );
} else {
console.log( 'Socket already in room, try to join again: '+ room_name );
}
socket.on('disconnect', function() {
// FILTER BY disconnecting the client
// missing same logic on the server side
// check if was already on the room
if ( this.room_id && this.manager.room_trigger[this.room_id] ) {
var room_name = socketio_room_name(room_id);
if ( this.manager.rooms[room_name] || this.manager.roomClients[this.id][room_name] ) {
this.leave( this.room_id );
//console.log( 'Socket leaves the room: '+ this.room_id );
} else {
console.log( 'Socket try to leave a room in which is not present: '+ room_name );
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment