Skip to content

Commit

Permalink
Websockets!
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas O Fredericks committed Aug 15, 2017
1 parent 96abb12 commit 6a37497
Show file tree
Hide file tree
Showing 13 changed files with 44,875 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AsciiMassagePacker {

// Returns a reference to the buffer.
byte[] buffer() {
if ( !ended ) println("Warning: betting the buffer of an unended packet!");
if ( !ended ) println("Warning: getting the buffer of an unended packet!");
return internalString.getBytes();
}

Expand Down
20 changes: 20 additions & 0 deletions applications/Websocket Client(p5.js)/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AsciiMassage Websockets</title>
<script src="libraries/p5.js" type="text/javascript"></script>

<script src="libraries/p5.dom.js" type="text/javascript"></script>
<script src="libraries/p5.sound.js" type="text/javascript"></script>

<script src="libraries/AsciiMassagePacker.js" type="text/javascript"></script>
<script src="libraries/AsciiMassageParser.js" type="text/javascript"></script>

<script src="sketch.js" type="text/javascript"></script>

<style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
</head>
<body>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
function AsciiMassagePacker() {

this._messageSize;
this._internalString = "";
this._ended = false;

/// Returns size of buffer.
this.size = function() {
return this._internalString.length();
}

// Returns a reference to the buffer.
this.buffer = function() {
if ( this.ended == false ) console.log("Warning: getting the buffer of an unended packet!");
return this._internalString;
}

/// Begins the sending of a message.
this.beginPacket = function(address) {
this._internalString = address;
this._ended = false;
}

/// Adds a byte.
this.addByte = function(value) {
this._internalString = this._internalString +" " + value;
}

/// Adds an int.
this.addInt = function(value) {
this._internalString = this._internalString +" " + value;
}

/// Adds a long.
this.addLong = function(value) {
this._internalString = this._internalString +" " + value;
}

/// Adds a float.
this.addFloat = function(value) {
this._internalString = this._internalString +" " + value;
}

/// Ends the sending of a message.
this.endPacket = function() {
this._internalString += '\n';
this._ended = true;
}

/// Create a packet with no arguments.
this.packEmpty = function(address)
{
this.beginPacket(address);
this.endPacket();
}

/// Create a packet with a single byte value.
this.packOneByte = function( address, value)
{
this.beginPacket(address);
this.addByte(value);
this.endPacket();
}

/// Create a packet with a single int value.
this.packOneInt = function( address, value)
{
this.beginPacket(address);
this.addInt(value);
this.endPacket();
}

/// Create a packet with a single long value.
this.packOneLong = function( address, value)
{
this.beginPacket(address);
this.addLong(value);
this.endPacket();
}

/// Create a packet with a single float value.
this.packOneFloat = function( address, value)
{
this.beginPacket(address);
this.addFloat(value);
this.endPacket();
}



}
101 changes: 101 additions & 0 deletions applications/Websocket Client(p5.js)/libraries/AsciiMassageParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@


function AsciiMassageParser() {


this._flush = function() {
this._needToFlush = false;
this._receivedDataIndex = 0;
this._internalString = new Array();
this._currentWord = 0;
this._ready = false;
this._words = null;
}

this._flush();

this.parse = function ( data , callback = null ) {



if ( this._needToFlush || this._receivedDataIndex > 1024 ) {
this._flush();
}

if ( data == '\n' ) {

this._needToFlush = true;
if ( this._receivedDataIndex > 0 ) {
this._words = this._internalString.split(" ");
this._ready = true;
this._currentWord = 1;

if ( callback != null ) callback();
return true;
}

} else {
this._internalString += data;
this._receivedDataIndex++;
}
return false;
}


this._isReady = function() {

if ( this._ready && this._currentWord < this._words.length ) {
return true;
} else {
return false;
}
}

this.fullMatch = function( address ) {
if ( this._isReady() && this._words[0] == address ) {
return true;
}
return false;
}

this.nextByte = function() {

if ( this._isReady() ) {
var data = parseInt(this._words[this._currentWord]);
this._currentWord++;
return data;
}
return 0;
}

this.nextInt = function() {

if ( this._isReady() ) {
var data = parseInt(this._words[this._currentWord]);
this._currentWord++;
return data;
}
return 0;
}

this.nextFloat = function() {

if ( this._isReady() ) {
var data = parseFloat(this._words[this._currentWord]);
this._currentWord++;
return data;
}
return 0;
}

this.nextLong = function() {

if ( this._isReady() ) {
var data = parseInt(this._words[this._currentWord]);
this._currentWord++;
return data;
}
return 0;
}

}
Loading

0 comments on commit 6a37497

Please sign in to comment.