Skip to content

Commit

Permalink
got a node2node chatter working with mike
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgibson14 committed Apr 1, 2012
1 parent 2c80e0f commit dfbf52b
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app.js
Expand Up @@ -43,6 +43,12 @@ app.get('/blogs', function(req, res){
}); });
}) })
}); });
app.post('/login',function(req, res){
//var username = req.param('username');
//var password = req.param('password');
//var user = personProvider.findone({username:username});
//if(user.password == password){you got your password right, redirect to home page}else {this is not a valid password}
});
app.get('/blog/new', function(req, res) { app.get('/blog/new', function(req, res) {
res.render('blog_new.jade', { locals: { res.render('blog_new.jade', { locals: {
title: 'New Post' title: 'New Post'
Expand Down
10 changes: 10 additions & 0 deletions chatserver.js
@@ -0,0 +1,10 @@
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('myevent', function (data) {
console.log(data);
socket.broadcast.emit('user connected',data);
});

});
23 changes: 23 additions & 0 deletions client.js
@@ -0,0 +1,23 @@
var io = require('socket.io-client');
var socket = io.connect('http://192.168.1.4');
socket.on('connect', function () {
// socket connected
console.log('connected');
setInterval(chatter,1000);
});
socket.on('user connected', function (data) {
// server emitted a custom event
console.log('event' + data.msg);
});
socket.on('disconnect', function () {
// socket disconnected
console.log('disconnected');
});
var n = 0;
function chatter(){
++n;
//console.log('chat #' + n);
socket.emit('myevent', {msg:'hi there mike from James chat #' + n});
};


Empty file added models/events.js
Empty file.
Empty file added models/locations.js
Empty file.
Empty file added models/people.js
Empty file.
Empty file added models/pictures.js
Empty file.
27 changes: 27 additions & 0 deletions npm-debug.log
@@ -0,0 +1,27 @@
info it worked if it ends with ok
verbose cli [ 'node', '/usr/local/bin/npm', 'install', '-d' ]
info using npm@1.1.9
info using node@v0.6.13
verbose config file /home/james/.npmrc
verbose config file /usr/local/etc/npmrc
verbose config file /usr/local/lib/node_modules/npm/npmrc
ERR! Couldn't read dependencies.
ERR! Failed to parse json
ERR! Unexpected token >
ERR! File: /home/james/dev/FunFamilyLegacy/package.json
ERR! JSON.parse Failed to parse package.json data.
ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
ERR! JSON.parse This is not a bug in npm.
ERR! JSON.parse Tell the package author to fix their package.json file.
ERR!
ERR! System Linux 2.6.38-8-generic
ERR! command "node" "/usr/local/bin/npm" "install" "-d"
ERR! cwd /home/james/dev/FunFamilyLegacy
ERR! node -v v0.6.13
ERR! npm -v 1.1.9
ERR! file /home/james/dev/FunFamilyLegacy/package.json
ERR! code EJSONPARSE
ERR! message Failed to parse json
ERR! message Unexpected token >
ERR! errno {}
verbose exit [ 1, true ]
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -8,5 +8,7 @@
, "jade": ">= 0.0.1" , "jade": ">= 0.0.1"
, "mongodb": ">= 0.9.6-7" , "mongodb": ">= 0.9.6-7"
, "reloader": ">0" , "reloader": ">0"
, "socket.io: ">0"
, "socket.io-client": ">0"
} }
} }
80 changes: 80 additions & 0 deletions postProvider.js
@@ -0,0 +1,80 @@
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_blog');

var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var Comments = new Schema({
person : String
, comment : String
, created_at : Date
});

var Post = new Schema({
author : ObjectId
, title : String
, body : String
, created_at : Date
, comments : [Comments]
});

mongoose.model('Post', Post);
var Post = mongoose.model('Post');

PostProvider = function(){};

//Find all posts
PostProvider.prototype.findAll = function(callback) {
Post.find({}, function (err, posts) {
callback( null, posts )
});
};

//Find post by ID
PostProvider.prototype.findById = function(id, callback) {
Post.findById(id, function (err, post) {
if (!err) {
callback(null, post);
}
});
};

//Update post by ID
PostProvider.prototype.updateById = function(id, body, callback) {
Post.findById(id, function (err, post) {
if (!err) {
post.title = body.title;
post.body = body.body;
post.save(function (err) {
callback();
});
}
});
};

//Create a new post
PostProvider.prototype.save = function(params, callback) {
var post = new Post({title: params['title'], body: params['body'], created_at: new Date()});
post.save(function (err) {
callback();
});
};

//Add comment to post
PostProvider.prototype.addCommentToPost = function(postId, comment, callback) {
this.findById(postId, function(error, post) {
if(error){
callback(error)
}
else {
post.comments.push(comment);
post.save(function (err) {
if(!err){
callback();
}
});
}
});
};

exports.PostProvider = PostProvider;

0 comments on commit dfbf52b

Please sign in to comment.