Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support to send data over websockets

  • Loading branch information...
commit 5fd4ee29cfb3f4b8919e0b9fa22c424a1a906b70 1 parent 9d1332c
@scribeGriff authored
Showing with 71 additions and 10 deletions.
  1. +14 −6 README.md
  2. +1 −1  lib/ConvoLab.dart
  3. +56 −3 lib/utilities/ConvoLabResults.dart
View
20 README.md
@@ -37,9 +37,13 @@ The variable y is actually an object with several return values. In addition, t
-2.00
-2.00 - 2.00j
-You can save this data to an external file using the export(String filename) method of the base results class as follows:
+You can use the export methods of the base results class to either save this data to an external file:
- y.export('pathName/filename.txt');
+ y.exportToFile('pathName/filename.txt');
+
+or send to a client using a websocket:
+
+ y.exportToWeb('local', 8080);
To perform an inverse fft to recover the original sequence, one can pass the data field of y to ifft():
@@ -87,9 +91,11 @@ To display the transformed data (usually an array of complex numbers) as String,
as in the example above. You can also save the calculated data (ie, y.data) in tab delimited format to an external file as follows:
- y.export('myPath/myExternalFile.txt');
+ y.exportToFile('myPath/myExternalFile.txt');
-This format allows you to import your data to Matlab, Scilab or other similar tools for plotting, post processing, etc.
+This format allows you to import your data to Matlab, Scilab or other similar tools for plotting, post processing, etc. You can also arrange to send the data to a client over a websocket:
+
+ y.exportToWeb('local', 8080);
**IFFT:** Performs an inverse FFT and is similar in performance and requirements as the FFT.
@@ -115,9 +121,11 @@ To just display the output data as String you can also simply write:
You can also save the calculated data (ie, x.data) in tab delimited format to an external file as follows:
- x.export('myPath/myExternalFile.txt');
+ x.exportToFile('myPath/myExternalFile.txt');
+
+This format allows you to import your data to Matlab, Scilab or other similar tools for plotting, post processing, etc. You can also arrange to send the data to a client over a websocket:
-This format allows you to import your data to Matlab, Scilab or other similar tools for plotting, post processing, etc.
+ y.exportToWeb('local', 8080);
**RandomizedSelection:** Performs a linear time selection for a certain order statistic from a list of unsorted data. A thorough description of the algorithm can be found at [the blog entry at scribegriff.com](http://http://www.scribegriff.com/studios/index.php?post/2012/05/31/A-Linear-Time-Randomized-Selection-Algorithm-in-Dart "Linear Time Randomized Selection Algorithm"). A simple example is as follows:
View
2  lib/ConvoLab.dart
@@ -7,7 +7,7 @@
#library('ConvoLab');
#import('dart:io');
#import('dart:math');
-
+#import('dart:json');
#source('exceptions/ConvoLabException.dart');
#source('exceptions/DataFormatException.dart');
View
59 lib/utilities/ConvoLabResults.dart
@@ -11,9 +11,8 @@ class ConvoLabResults {
// Method: export writes data list to a file. If the data is complex,
// the external file is tab delimited. Data can then be read into
- // Matlab, Scilab, etc. This is temporary until we develop the html5
- // data visualizer (ie, ConvoWeb).
- void export(String filename) {
+ // Matlab, Scilab, etc.
+ void exportToFile(String filename) {
File fileHandle = new File(filename);
RandomAccessFile dataFile = fileHandle.openSync(FileMode.WRITE);
if (this.data.every(f(element) => element is Complex)) {
@@ -28,4 +27,58 @@ class ConvoLabResults {
}
dataFile.closeSync();
}
+
+ //Method: export data to web using a web socket. If data is complex,
+ //send data as a real array and an imag array.
+ void exportToWeb(String host, int port) {
+ //connect with ws://localhost:8080/ws
+ //for echo - http://www.websocket.org/echo.html
+ if (host == 'local') host = '127.0.0.1';
+ List _real = new List(this.data.length);
+ List _imag = new List(this.data.length);
+ bool _isComplex;
+ if (this.data.every(f(element) => element is Complex)) {
+ for (var i = 0; i < this.data.length; i++) {
+ this.data[i] = this.data[i].cround2;
+ _real[i] = this.data[i].real;
+ _imag[i] = this.data[i].imag;
+ }
+ _isComplex = true;
+ } else {
+ _isComplex = false;
+ }
+
+ HttpServer _server = new HttpServer();
+ WebSocketHandler _wsHandler = new WebSocketHandler();
+ _server.addRequestHandler((req) => req.path == "/ws", _wsHandler.onRequest);
+
+ // Open the connection.
+ _wsHandler.onOpen = (WebSocketConnection wsConn) {
+ print('Opening connection at $host:$port');
+
+ // Receive message and send reply.
+ wsConn.onMessage = (message) {
+ var msg = JSON.parse(message);
+ print("Received the following message: \n"
+ "${msg["request"]}\n${msg["date"]}");
+ if(_isComplex) {
+ wsConn.send(JSON.stringify({"real": _real, "imag": _imag}));
+ } else {
+ wsConn.send(JSON.stringify({"real": this.data, "imag": null}));
+ }
+ };
+
+ // Close the connection.
+ wsConn.onClosed = (int status, String reason) {
+ print('Connection closed: Status - $status : Reason - $reason');
+ };
+
+ // Handle a connection error.
+ wsConn.onError = (err) {
+ print('There was an error with the connection: $err');
+ };
+ };
+
+ _server.listen(host, port);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.