Skip to content

Commit

Permalink
1. add socket.io to server-side, websocket work as a plugin
Browse files Browse the repository at this point in the history
2. for now, response to 3 events: user:join , change:name, new:message
3. message and user profile is stored into testdata set now
  • Loading branch information
sunzhxjs committed Apr 4, 2016
1 parent 6f3bfb5 commit 0a53f7e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"dependencies": {
"good": "^6.0.0",
"good-console": "^5.3.1",
"hapi": "^13.2.1"
"hapi": "^13.2.1",
"socket.io": "^1.4.5"
},
"devDependencies": {
"eslint-config-google": "^0.4.0",
Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');


global.rootRequire = function(name) {
var projectPath = path.join(__dirname, name);
return require(projectPath);
Expand All @@ -16,6 +17,7 @@ server.connection({
host: 'localhost'
});


// Export the server to be required elsewhere.
module.exports = server;

Expand Down Expand Up @@ -43,6 +45,9 @@ server.register([
},
{
register: require('./server/base/messages/index.js')
},
{
register: require('./server/socket/index.js')
}
], function() {
server.start(function() {
Expand Down
47 changes: 47 additions & 0 deletions server/socket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

var testdata = require('../../testdata')

exports.register = function(server, options, next) {
var io = require('socket.io')(server.listener);
io.on('connection', function (socket) {

//Triggered when recieve new message from client side
socket.on('newMessage',function(newMessage){
socket.broadcast.emit('new:message',newMessage);

//add new message to old message group
testdata.messages.old.push(newMessage);

});

//Triggered when the user change pseduo username
socket.on('newName',function(odata){
socket.broadcast.emit('change:name',odata);

//Replace user's username with new name on server side
testdata.profiles[odata.id].username = odata.newName;
});

//Triggered when new user join in the chatroom
socket.on('Iamin',function(){
//Generate userID (switch to using uuid generater lib later)
var id = 'u'+((new Date()).getTime().toString());

//Mock profile data
testdata.profiles[id]={gender:'male',name:'new user',location:'Houston',email:'[email protected]',username:id,img:'https://i0.wp.com/slack.global.ssl.fastly.net/7fa9/img/avatars/ava_0012-512.png?ssl=1'};
testdata.id=id;

//trigger 'init' event on the calling user, and send (userlist, messagelist and userid) for inialization
socket.emit('init',testdata);

//trigger 'user:join' event on other client, and send new userlist
socket.broadcast.emit('user:join',testdata);
});
});
next();
}

exports.register.attributes = {
name: 'socket'
};

18 changes: 18 additions & 0 deletions testdata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var _test={}

_test.profiles = {
u0:{gender:'male',name:'alex2',location:'Houston',email:'[email protected]',username:'blaze',img:'https://i0.wp.com/slack.global.ssl.fastly.net/7fa9/img/avatars/ava_0012-512.png?ssl=1'} ,
u1:{gender:'male',name:'@sunzhx',location:'Houston',email:'[email protected]',username:'sunzhx',img:'https://secure.gravatar.com/avatar/3f244274ede6c91a956b06feccf7a0ff.jpg?s=512&d=https%3A%2F%2Fslack.global.ssl.fastly.net%2F7fa9%2Fimg%2Favatars%2Fava_0000-512.png'}
};
_test.messages = {
old:[
{profile:'u0',content:'hello world'} ,
{profile:'u1',content:'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'} ,
{profile:'u0',content:'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam'} ,
] ,
_new :[]

};

module.exports = _test;

0 comments on commit 0a53f7e

Please sign in to comment.