Skip to content

Commit

Permalink
added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hackfrag committed Jul 23, 2010
1 parent ce568a0 commit 16d9e30
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 43 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Another, once cloned
git submodule update --init --recursive

How to run the demo
----------
-------------------

cd tests
sudo node server.js
Expand All @@ -46,7 +46,6 @@ ToDos
- add a access log (http and websocket calls)
- add command line options to weasel (like node server.js start/restart)
- add 'run as daemon'
- add mongodb as database layer
- add configuration files


Expand Down
65 changes: 34 additions & 31 deletions lib/weasel.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,6 @@ var http = require('http'),



var Commands = {

connect: function(server, client, params) {

this.response(server.clients, {
'client' : client
})
},
disconnect: function(server, client, params) {

this.response(server.clients, {
'client' : client
})
},
getAllClients: function(server, client, params) {
var clients = [];

for (var i in server.clients) {
if(server.clients[i] && i != client.sessionId) {
clients.push(server.clients[i])
}

}

this.response([client], {
'clients' : clients
})
}
}



Expand All @@ -52,7 +23,7 @@ var Server = exports.Server = Class({

this.options = options;
this.controllers = {
defaults: Commands
defaults: CoreCommands
}
this.clients = [];
this.db = Database.connect('mongodb://localhost/db');
Expand Down Expand Up @@ -169,7 +140,7 @@ var Server = exports.Server = Class({



command = Commands['disconnect'];
command = self.controllers.defaults['disconnect'];

command.response = function(to, params) {

Expand Down Expand Up @@ -221,6 +192,7 @@ var Server = exports.Server = Class({
context['require'] = require;
context['process'] = process;


scriptObj = Script.runInNewContext(scriptContent, context);

});
Expand Down Expand Up @@ -248,6 +220,7 @@ var Server = exports.Server = Class({
};
context['require'] = require;
context['process'] = process;
context['Server'] = self;

scriptObj = Script.runInNewContext(scriptContent, context);

Expand All @@ -261,6 +234,36 @@ var Server = exports.Server = Class({
});


var CoreCommands = {

connect: function(server, client, params) {

this.response(server.clients, {
'client' : client
})
},
disconnect: function(server, client, params) {

this.response(server.clients, {
'client' : client
})
},
getAllClients: function(server, client, params) {
var clients = [];

for (var i in server.clients) {
if(server.clients[i] && i != client.sessionId) {
clients.push(server.clients[i])
}

}

this.response([client], {
'clients' : clients
})
}
}




Expand Down
20 changes: 13 additions & 7 deletions tests/app/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
var sys = require('sys');

Controller('User', {

register: function(server, client, params) {
var User = server.db.model('User'),self = this;

var User = server.db.model('User'),
self = this;

user = new User();

user.username = params.username;
user.password = params.password;
user.position.top = (50 + parseInt( Math.random() * ( 250-50+1 ) ));
user.position.left = (50 + parseInt( Math.random() * ( 550-50+1 ) ));
user.sessionId = client.sessionId;
user.username = params.username;
user.password = params.password;
user.position.top = (50 + parseInt( Math.random() * ( 250-50+1 ) ));
user.position.left = (50 + parseInt( Math.random() * ( 550-50+1 ) ));
user.sessionId = client.sessionId;

user.save(function() {

Expand All @@ -20,10 +23,13 @@ Controller('User', {
'client': client
})
})

},

login: function(server, client, params) {

var User = server.db.model('User'),self = this;
var User = server.db.model('User'),
self = this;


User.find({username: params.username, password: params.password}).one(function(user) {
Expand Down
1 change: 1 addition & 0 deletions tests/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@

Weasel.subscribe('user/move', MoveDemo.move);




Weasel.ready(function() {
Expand Down
35 changes: 32 additions & 3 deletions tests/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
var weasel = require('../lib/weasel.server');

/**
* Create a new weasel server instance and listen on
* port 8080
*
* You can also extend the Server Class
*
* var Example = weasel.Server.extend({
* init: function(options) {
* this.__super__(options)
*
* // your code
* }
* })
*
* var myServer = new Example();
*
* myServer.listen(5050);
*
*/
var Example = new weasel.Server().listen(8080);

var Example = new weasel.Server({
debug : true
}).listen(8080);


Example.addListener("onClientConnect", function(client) {

})

Example.addListener("onClientDisconnect", function(client) {

})

Example.addListener("onClientMessage", function(message, client) {

})

0 comments on commit 16d9e30

Please sign in to comment.