1 /** Example node.js app for serving random markers via socket.io **/
3 var app = require('http').createServer(handler);
4 var fs = require('fs');
5 var util = require('util');
6 var ns = require('node-static');
7 var io = require('socket.io').listen(app);
8 var net = require('net');
9 var file = new(ns.Server)("../static/", { cache: 600 });
11 // Listen on port 1337
14 // Serve static content
15 function handler (req, res) {
16 req.addListener('end', function() {
17 file.serve(req, res, function(err, result) {
19 console.error('Error serving %s - %s', req.url, err.message);
20 if (err.status === 404 || err.status === 500) {
21 file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
23 res.writeHead(err.status, err.headers);
31 // Push random markers via socket.io
32 io.sockets.on('connection', function (socket) {
33 //function random_point() {
35 // lat = Math.random() * 180 - 90;
36 // lng = Math.random() * 360 - 180;
37 // socket.emit('marker', { lat: lat, lng: lng });
40 //function insert_point(lat,lng) {
41 // socket.emit('marker', { lat: lat, lng: lng });
44 var sock = net.createConnection(10000,"localhost");
45 console.log('Attempting a connection');
46 sock.on('connect', function (connect) {
47 console.log('Connected');
48 sock.on('data', function (data) {
49 // var data = c + ''; // make sure it's a string
50 //var tmp = data.toString("utf-8").split("\n");
51 //console.log('data`%s`', tmp[0]);
52 //var parts = tmp[0].split(":");
53 var parts = data.toString("utf-8").split(":");
55 lat = Number(parts[0]);
56 lng = Number(parts[1]);
57 if (lat != null && lng != null) {
58 //console.log('split lat: %s long: %s', parts[0], parts[1]);
59 //lat.replace(/(\n|\r|\r\n)$/, '');
60 //lat.replace(/(\n|\r|\r\n)$/, '');
61 //lng.replace(/(\n|\r|\r\n)$/, '');
63 //console.log('passed lat: %s long: %s', lat, lng);
64 //console.log('Received data: `%s`', data);
65 //insert_point(lat,lng);
68 socket.emit('marker', { lat: lat, lng: lng });
72 //socket.on("end", function () {
78 //setInterval(function() { setTimeout(random_point, Math.random() * 1000) }, 500);