Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
got a node2node chatter working with mike
- Loading branch information
1 parent
2c80e0f
commit dfbf52b
Showing
10 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
}); | |||
|
|||
}); |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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.
Empty file.
Empty file.
Empty file.
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 ] |
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; |