-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (22 loc) · 676 Bytes
/
server.js
File metadata and controls
29 lines (22 loc) · 676 Bytes
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
var WebSocketServer = require("ws").Server,
fs = require("fs"),
wss = new WebSocketServer({port: 8080});
console.log("Websocket server started at ws://localhost:8080");
wss.on('connection', function(ws) {
// When message is received
ws.on('message', function(message) {
var file = message;
if(fs.existsSync(file)) {
var data = fs.readFileSync(file);
// Is the file a javascript file?
var contentType = /\.js$/.test(file) ? "text/javascript" : "text";
ws.send(JSON.stringify({
url: file,
contentType: contentType,
data: data.toString()
}));
console.log("Sending file: " + file);
}
console.log("Received: %s", message);
});
});