I built an extremely small lightbulb flasher and describe the design
process and challenges. This video is free for Patreon subscribers, and
if I win the contest prize, I will donate the winnings to a STEM
education charity.
Hello! I have re-launched the Applied Science T-shirt campaign on
Teepspring. It will end in just a few days, but will re-launch after
that, and be active for at least two weeks. These shirts are being sold
at-cost -- thanks so much for your support!
var http = require('http'),
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/index.html');
// Send index.html to all requests
var app = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
});
// Socket.io server listens to our app
var io = require('socket.io').listen(app);
// Send current time to all connected clients
function sendTime() {
// io.emit('time', { time: new Date().toJSON() });
io.emit('time', pipedata);
}
// Send current time every 10 secs
setInterval(sendTime, 100);
// Emit welcome message on connection
io.on('connection', function(socket) {
// Use socket to communicate with this particular client only, sending it it's own id
socket.emit('welcome', { message: 'Welcome!', id: socket.id });
socket.on('i am client', console.log);
});
app.listen(8080);
index.html:
<!doctype html>
<html>
<head>
<script src='/socket.io/socket.io.js'></script>
<script>
var socket = io();
socket.on('welcome', function(data) {
addMessage(data.message);
// Respond with a message including this clients' id sent from the server
socket.emit('i am client', {data: 'foo!', id: data.id});