-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (73 loc) · 2.13 KB
/
Copy pathserver.js
File metadata and controls
79 lines (73 loc) · 2.13 KB
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
'use strict';
var channel=require('./channel.js');
var config=require('./config.js');
var db=require('./db.js');
var http=require('http');
var https=require('https');
var aChat=require('./package.json');
var user=require('./user.js');
var webSocket=require('websocket');
process.title='aChat - 聊天室伺服器';
console.log('aChat v%s by a0000778',aChat.version);
console.log(aChat.licence);
var serverLock=false;
if(config.ssl)
var httpServer=https.createServer(config.ssl);
else
var httpServer=http.createServer();
var wsServer=new webSocket.server();
httpServer
.on('request',require('./http.js').request)
.on('close',() => user.exit(1001))
;
wsServer.mount({
'httpServer': httpServer
});
wsServer.on('request',function(req){
if(req.requestedProtocols.indexOf('adminv1')!==-1)
user.createLink(req.accept('adminv1',req.origin));
else if(serverLock)
req.reject(4001,'Server locked.');
else if(user.sessionCount>=config.sessionMax)
req.reject(4002,'Server overload.');
else if(req.requestedProtocols.indexOf('chatv1')>=0)
user.createLink(req.accept('chatv1',req.origin));
else
req.reject(404,'Not supported protocol.');
});
console.log('載入頻道列表...');
channel.loadAll(function(){
if(!channel.findById(config.channelDefault)){
console.log('預設頻道不存在!');
process.exit();
}
console.log('啟動伺服器...');
httpServer.listen(config.port,function(error){
if(error){
console.log('伺服器啟動失敗');
console.log(error);
process.exit();
}else{
process.on('uncaughtException',function(e){
console.error(e.stack);
shutdown();
});
process.once('SIGINT',shutdown);
console.log('啟動完畢');
}
});
});
function shutdown(){
process.on('SIGINT',() => console.log('伺服器關閉中 ...'));
serverLock=true;
user.exit(1001);
httpServer.close();
db.writeChatLogNow(true);
setInterval(function(){
let chatLogCacheCount=db.chatLogCacheCount();
let queryQueueCount=db.queryQueueCount;
if(!(chatLogCacheCount || queryQueueCount))
process.exit();
console.log('等待資料庫操作完畢 (操作: %d, 記錄快取: %d) ...',queryQueueCount,chatLogCacheCount);
},1000);
}