-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add socket.io to server-side, websocket work as a plugin
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
Showing
4 changed files
with
72 additions
and
1 deletion.
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
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 | 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' | ||
}; | ||
|
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 | 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; | ||
|