Skip to content

Commit

Permalink
miscellaneous
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Weber committed Oct 5, 2014
1 parent 4171c6a commit 49f783e
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 92 deletions.
75 changes: 75 additions & 0 deletions array-methods/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env node

'use strict';

// demonstrate five array methods.

// indexOf - This method returns the index of the search term.
// Returns -1 if search term is not found.

var fruit = ['pear', 'apple', 'grape', 'tomato', 'orange'];

var index = fruit.indexOf('grape');

console.log('Index of grape in fruit array: ' + index);

index = fruit.indexOf('fuss');

console.log('Index of fuss in fruit array: ' + index);


// filter - Creates a new array as element selection
// criteria is applied to the input array.

var newArray = fruit.filter(function(element) {
if ((element[0] === 'g') || (element[0] == 't')) {
return true;
}
});


// forEach - Calls the supplied function for each
// array element.

newArray.forEach(function(element) {
console.log('newArray element:' + element);
});


// map - Create a new array by applying the provided
// function to each element of the input array.

var anotherNewArray = fruit.map(function(item, index) {
return (item + ' at index ' + index);
});

anotherNewArray.forEach(function(item) {
console.log(item);
});


// reduce - Produces a single value by applying a function to each element
// of the supplied array.

var accumulator = fruit.reduce(function(previousAccumulator, item, index, array) {
return (previousAccumulator + ' ' + item.substring(0, 2));
}, 'First two letters:');

console.log('accumulator: ' + accumulator);


// see underscore and lodash for additional collection manipulation capabilities

// demethodizing - This technique is for make a standalone function out of an
// object method. Needs more study.

// Demethodizing the Array method, forEach(), into a generic "each"
//var each = Function.prototype.call.bind([].forEach);
//
//var nodeList = document.querySelectorAll("p");
//
//each(nodeList,bold);
//
//function bold(node){
// node.style.fontWeight ="bold";
//}
53 changes: 0 additions & 53 deletions dayley-book/emitters/app.js

This file was deleted.

39 changes: 0 additions & 39 deletions dayley-book/emitters2/app.js

This file was deleted.

Binary file added dayley-book/errata.doc
Binary file not shown.
34 changes: 34 additions & 0 deletions websockets/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node
var WebSocketClient = require('websocket').client;

var client = new WebSocketClient();

client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
console.log('WebSocket client connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
}
});

function sendNumber() {
if (connection.connected) {
var number = Math.round(Math.random() * 0xFFFFFF);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
});

client.connect('ws://localhost:8080/', 'echo-protocol');
62 changes: 62 additions & 0 deletions websockets/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env node

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});

wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});

function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}

var connectionId = 0;
var clients = {};

wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}

var id = connectionId++;
var connection = request.accept('echo-protocol', request.origin);
clients.id = connection;

console.log((new Date()) + ' Connection accepted.');

connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.sendUTF(message.utf8Data);
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
// attn - for this to work, there has to be closure for the value of id
// new Date vs Date() - which is preferred
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
63 changes: 63 additions & 0 deletions websockets/websocket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
// var wsUri = "ws://echo.websocket.org/";
var wsUri = "ws://localhost:8080/";
var output;

function init() {
output = document.getElementById("output");
testWebSocket();
}

function testWebSocket() {
websocket = new WebSocket(wsUri, 'echo-protocol');
websocket.onopen = function(evt) {
onOpen(evt);
};
websocket.onclose = function(evt) {
onClose(evt);
};
websocket.onmessage = function(evt) {
onMessage(evt);
};
websocket.onerror = function(evt) {
onError(evt);
};
}

function onOpen(evt) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
}

function onClose(evt) {
writeToScreen("DISCONNECTED");
}

function onMessage(evt) {
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
websocket.close();
}

function onError(evt) {
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
}

function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}

function writeToScreen(message) {
var pre = document.createElement("p");
pre.style.wordWrap = "break-word";
pre.innerHTML = message;
output.appendChild(pre);
}

window.addEventListener("load", init, false);
</script>
<h2>WebSocket Test</h2>
<div id="output"></div>

0 comments on commit 49f783e

Please sign in to comment.