from random import uniform
from geojson import Point as point
from geojson import Feature as feature

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
 
 
class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message(point_plus_accuract())
      
    def on_message(self, message):
        print 'message received %s' % message
 
    def on_close(self):
      print 'connection closed'
 
 
application = tornado.web.Application([
    (r'/ws', WSHandler),
])
 
 

def point_plus_accuracy(lat=None, lon=None, accuracy=None):
  if lat is None: lat = uniform(-180,180)
  if lon is None: lon = uniform(-90,90)
  if accuracy is None: accuracy = uniform(0,20000)
  return feature(geometry=point((lat,lon)), property={'accuracy': accuracy})

print [point_plus_accuracy() for x in xrange(10)]

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
