Wondering if anyone can help me get node.js and socket.io working on my R-Pi. I have them installed but they don't work as I expect. Should the example below (from
Scott Blaine's Blog ) work from the R-Pi's own browser (Midori) or does it require Firefox or something else? Here is the simple example I can't get to work, it is two files
connectionCounter.js :
Code: Select all
var express = require('express');
var app = express();
var socket = require('socket.io');
var server = app.listen(4000);
var io = socket.listen(server);
app.get('/', function(request, response){
response.sendfile(__dirname + "/index.html");
});
var activeClients = 0;
io.sockets.on('connection', function(socket){clientConnect(socket)});
function clientConnect(socket){
activeClients +=1;
io.sockets.emit('message', {clients:activeClients});
socket.on('disconnect', function(){clientDisconnect()});
}
function clientDisconnect(){
activeClients -=1;
io.sockets.emit('message', {clients:activeClients});
and here is the second file
index.html :
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Connection Counter</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
function msgReceived(msg){
$clientCounter.html(msg.clients);
}
$(document).ready(function () {
$clientCounter = $("#client_count")
var socket = io.connect('http://localhost:4000');
socket.on('message', function(msg){msgReceived(msg)});
});
</script>
</head>
<body>
<p><span id="client_count">0</span> connected clients</p>
</body>
</html>
I can connect to
http://127.0.0.1:4000 from the R-Pi using Midori, from the R-Pi using links2 (text mode browser) and from my Android phone (Opera Mobile) via wifi. In every case I get the static page "0 connected clients" but I never see it increment, no matter how many clients connect. I do see each connection add a line of text on the console output:
Code: Select all
pi@raspberrypi:~/nodeJS$ sudo node connectionCounter.js
info - socket.io started
debug - served static content /socket.io.js
debug - served static content /socket.io.js
debug - served static content /socket.io.js
debug - served static content /socket.io.js
...etc.
it also works the same way, that is to say not correctly, if I don't use 'sudo'.