Hoi,
Omdat hier een bezoekersteller te koop staat, wou ik ook een maken en gratis releasen dus;
Codeblok (app.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var express = require(express)
, http = require(http)
, app = express()
, io = require(socket.io).listen(1337);
app.set(port, 80);
app.configure(function() {
app.use(express.static(__dirname + /views));
});
http.createServer(app).listen(app.get(port), function(){
console.log(Server runs at port: + app.get(port));
});
var online = new Array();
io.sockets.on(connection, function(socket) {
if(online.indexOf(socket.handshake.address.address) == -1) {
online.push(socket.handshake.address.address);
}
io.sockets.emit(onlineUpdate, {val: online.length });
socket.on(disconnect, function() {
online.splice(online.indexOf(socket.handshake.address.address), 1);
io.sockets.emit(onlineUpdate, {val: online.length });
});
});
Codeblok (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
Online bezoekers teller
Codeblok (script.js):
1
2
3
4
5
6
7
$(document).ready(function() {
var socket = io.connect(http://localhost:1337);
socket.on(onlineUpdate, function(s) {
$("div").html("Er zijn nu "+ s.val +" gebruikers online!");
});
});
Het is wat simpel maar ik wou even weer met NodeJS bezig zijn. Veel succes ermee!
Omdat hier een bezoekersteller te koop staat, wou ik ook een maken en gratis releasen dus;
Codeblok (app.js):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var express = require(express)
, http = require(http)
, app = express()
, io = require(socket.io).listen(1337);
app.set(port, 80);
app.configure(function() {
app.use(express.static(__dirname + /views));
});
http.createServer(app).listen(app.get(port), function(){
console.log(Server runs at port: + app.get(port));
});
var online = new Array();
io.sockets.on(connection, function(socket) {
if(online.indexOf(socket.handshake.address.address) == -1) {
online.push(socket.handshake.address.address);
}
io.sockets.emit(onlineUpdate, {val: online.length });
socket.on(disconnect, function() {
online.splice(online.indexOf(socket.handshake.address.address), 1);
io.sockets.emit(onlineUpdate, {val: online.length });
});
});
Codeblok (index.html):
1
2
3
4
5
6
7
8
9
10
11
12
13
Online bezoekers teller
Ophalen...
Codeblok (script.js):
1
2
3
4
5
6
7
$(document).ready(function() {
var socket = io.connect(http://localhost:1337);
socket.on(onlineUpdate, function(s) {
$("div").html("Er zijn nu "+ s.val +" gebruikers online!");
});
});
Het is wat simpel maar ik wou even weer met NodeJS bezig zijn. Veel succes ermee!

