重写了下博客的播放器

国庆无聊,对博客的播放器做了比较大幅度的修改,总结一下现有以下改进:

  1. 添加了”自动播放”复选框,可以由用户自主决定是否需要播放背景音乐(其实歌都不错的。。)
  1. 使用 node.js 重写了多标签的背景音乐控制器,主要是为了避免产生当用户打开多个标签后背景音乐同时播放的问题

抱着开源共享的想法,现在这里共享一下控制器源码。播放器的就不特地写了,都是前端的东西,右键”查看源代码”即可。如果有什么 bug,希望各位提出,谢谢各位。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.send('<h1>Welcome Realtime Server</h1>');
});

function count(o){
var t = typeof o;
if (t == 'string') {
return o.length;
} else if (t == 'object') {
var n = 0;
for (var i in o) {
n++;
}
return n;
}
return false;
};

Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

var dump_log=function(msg) {
//console.log("\033[40;33m"+new Date().Format("yyyy-MM-dd hh:mm:ss")+"\033[0m\n"+msg+"\nNow "+count(clients)+" user(s) online.");
console.log(new Date().Format("yyyy-MM-dd hh:mm:ss")+"\n"+msg+"\nNow "+count(clients)+" user(s) online.");
}


var onlineCount = 0;
var index=0;
global['clients']={};

io.on('connection', function(socket){

socket.on('bindkey', function(str){
if (!str.match(/^[a-z0-9]{64}$/)) {
dump_log("data: "+str+"\nip: "+socket.handshake.address);
return false;
}
socket.bindkey = str;
socket.index = index;
if (typeof global['clients'][str]=="undefined") {
global['clients'][str]={};
onlineCount++;
}
global['clients'][str][index]={'playing':(count(global['clients'][str])==0?1:0),'socket':socket,'ip':socket.handshake.address,'index':index};
socket.emit("message",[0,index,global['clients'][str][index]['playing']]);
dump_log("bindkey: "+str+", index: "+index+", ip: "+socket.handshake.address);
index++;
});

socket.on('disconnect', function(){
delete global['clients'][socket.bindkey][socket.index];
if (count(global['clients'][socket.bindkey])!==0) {
for (var ckey in global['clients'][socket.bindkey]) {
var ready_play=global['clients'][socket.bindkey][ckey];
break;
}
ready_play['playing']=1;
ready_play.socket.emit("message",[1,ready_play.socket.index,1]);
} else {
delete global['clients'][socket.bindkey];
onlineCount--;
}
dump_log("bindkey: "+socket.bindkey+", index: "+socket.index+", ip: "+socket.handshake.address+" offline.");
});

});

http.listen(3000, function(){
console.log('listening on *:3000');
});

Docker 项目地址: https://github.com/jshensh/audioController