forked from theRealWardo/Python_OpenBCI
-
Notifications
You must be signed in to change notification settings - Fork 8
/
socket_server.js
94 lines (76 loc) · 2.34 KB
/
socket_server.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var dgram = require('dgram');
var events = require('events');
var fs = require('fs');
var http = require('http');
var io = require('socket.io');
var UDP_HOST = '127.0.0.1',
UDP_PORT = 8888,
SERVER_HOST = '127.0.0.1',
SERVER_PORT = 8880,
HTDOCS_PATH = '/htdocs';
/**
* A client of the UDP server that will be broadcasting packets of
* channel data from the OpenBCI board.
* @param {number} port The port to connect to.
* @param {string} host The host to connect to.
* @constructor
*/
function UDPClient(port, host) {
this.port = port;
this.host = host;
this.data = [];
this.events = new events.EventEmitter();
this.connection = dgram.createSocket('udp4');
this.connection.on('listening', this.onListening.bind(this));
this.connection.on('message', this.onMessage.bind(this));
this.connection.bind(this.port, this.host);
};
/**
* Handler for when the connection is opened.
*/
UDPClient.prototype.onListening = function() {
console.log('Listening for data...');
};
/**
* Handler for when a message is received.
* @param {string} message The message that was received.
*/
UDPClient.prototype.onMessage = function(msg) {
this.events.emit('sample', JSON.parse(msg.toString()));
};
function OpenBCIServer(host, port, htdocs) {
this.host = host;
this.port = port;
this.server = http.createServer(this.onRequest.bind(this));
this.socket = io.listen(this.server, { log: false });
this.htdocs = htdocs;
this.socket.on('connection', this.onSocketConnect.bind(this));
this.server.listen(this.port, this.host)
};
OpenBCIServer.prototype.onRequest = function(req, res) {
this.serveStatic(req, res);
};
OpenBCIServer.prototype.serveStatic = function(req, res) {
var file = req.url;
if (file == '/') {
file = '/index.html';
}
fs.readFile(__dirname + this.htdocs + file,
function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
};
OpenBCIServer.prototype.onSocketConnect = function(socket) {
console.log('Client connected!');
};
var client = new UDPClient(UDP_PORT, UDP_HOST);
var server = new OpenBCIServer(SERVER_HOST, SERVER_PORT, HTDOCS_PATH);
client.events.on('sample', function(data) {
server.socket.sockets.emit('openbci', data);
});