-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryServer.js
executable file
·76 lines (61 loc) · 2.13 KB
/
binaryServer.js
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
/*
The code is based on:
- File Upload example from Binary JS library (see https://github.com/binaryjs/binaryjs/tree/master/examples/fileupload)
- Socket.io example from Socket.io library (see https://github.com/socketio/socket.io)
*/
// Get all the Node JS bits
var fs = require('fs');
var http = require('http');
var jsonfile = require('jsonfile');
var socket = require('socket.io');
// Serve client side statically
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
// Create http server
var server = http.createServer(app);
// Listen on port 9000
server.listen(9000);
// Create socket server
var io = socket(server);
// On connection, callback function 'newConnection'
io.sockets.on('connection', newConnection);
// Function called when new connection is made
function newConnection(socket) {
// Log message to console to confirm connection
console.log("New connection");
// When a new json message received, callback function 'jsonMsg'
socket.on('newJsonFile', jsonMsg);
// Function for dealing with new json
function jsonMsg(data) {
// Create data path file name
var fileName = 'public/data.json';
// Save the file, log any errors
jsonfile.writeFile(fileName, data, function(err) {
console.error(err);
})
}
}
// Start Binary.js server
var BinaryServer = require('binaryjs').BinaryServer;
var bs = BinaryServer({server: server});
// Wait for new user connections
bs.on('connection', function(client){
// Log message to console to confirm connection
console.log('Connected!');
// When client starts streaming data
client.on('stream', function(stream, meta){
// Log a confirmation message
console.log('Server recieving client');
// Create a file stream to public/sounds/ folder
var file = fs.createWriteStream(__dirname + '/public/sounds/' + meta.name);
// Save file
stream.pipe(file);
// Send progress back
stream.on('data', function(data){
stream.write({rx: data.length / meta.size});
});
});
});
// Log confirmation message to console
console.log('HTTP and BinaryJS server started on port 9000');