forked from luofei614/SocketLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (43 loc) · 1.22 KB
/
Copy pathindex.js
File metadata and controls
45 lines (43 loc) · 1.22 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
var ws = require("nodejs-websocket");
var http = require('http');
var server = ws.createServer(function (conn) {
console.log("New connection");
conn.on("text", function (str) {
//老版本的发送日志的方法。 新版可以去掉
console.log(str);
broadcast(str,conn.path);
});
conn.on("close", function (code, reason) {
console.log("Connection closed");
});
conn.on("error", function (err) {
console.log(err);
});
}).listen(1229);
//广播消息
function broadcast(msg,path) {
server.connections.forEach(function (conn) {
//通过path判断,将日志发给指定的client_id
if(conn.path==path)
{
console.log('##send message##');
conn.sendText(msg);
}
});
}
var httpServer = http.createServer(function(request, response){
if('POST'==request.method)
{
var requestBody = '';
request.on('data', function(data) {
requestBody+=data;
});
request.on('end',function(){
console.log(requestBody);
//发送日志
broadcast(requestBody,request.url);
});
}
response.end('sucess');
});
httpServer.listen(1116);