Skip to content

Commit

Permalink
make shortid stores in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
denghongcai committed Jan 4, 2016
1 parent e85fd2e commit f36682a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
9 changes: 6 additions & 3 deletions modules/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ module.exports = function(io) {
});

io.on('connection', socket => {
socket.shortid = shortid.generate().toLowerCase(); // generate shortid for an incomming connection
onlines.set(socket.shortid, socket); // add incomming connection to online table
socket.emit('shortid', socket.shortid);
socket.on('request shortid', function() {
onlines.delete(socket.shortid);
socket.shortid = shortid.generate().toLowerCase(); // generate shortid for a request
onlines.set(socket.shortid, socket); // add incomming connection to online table
socket.emit('shortid', socket.shortid);
});
});

io.on('disconnect', socket => {
Expand Down
8 changes: 7 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
<img class="logo" src="http://semantic-ui.com/examples/assets/images/logo.png" alt="logo">
Forsaken Mail
</div>
<div class="ui label item right floated mailaddress"><i class="mail icon copyable"></i><span id="shortid">请等待分配临时邮箱</span></div>
<div class="ui label item right floated mailaddress">
<i class="mail icon copyable"></i>
<div class="ui icon input">
<input id="shortid" type="text" placeholder="请等待分配临时邮箱" disabled>
<i id="refreshShortid" class="circular refresh link icon"></i>
</div>
</div>
</div>
</div>
<div class="ui main container">
Expand Down
24 changes: 22 additions & 2 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,29 @@ $(function(){
});

var socket = io();
socket.on('shortid', function(id) {

var setMailAddress = function(id) {
localStorage.setItem('shortid', id);
var mailaddress = id + '@' + location.hostname;
$('#shortid').text(mailaddress).siblings('i').attr('data-clipboard-text', mailaddress);
$('#shortid').val(mailaddress).parent().siblings('i').attr('data-clipboard-text', mailaddress);
};

if(('localStorage' in window)) {
var shortid = localStorage.getItem('shortid');
if(!shortid) {
socket.emit('request shortid', true);
}
else {
setMailAddress(shortid);
}
}

$('#refreshShortid').click(function() {
socket.emit('request shortid', true);
});

socket.on('shortid', function(id) {
setMailAddress(id);
});

socket.on('mail', function(mail) {
Expand Down

0 comments on commit f36682a

Please sign in to comment.