import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import pygame
from time import sleep
pygame.mixer.init()


son = "/var/www/html/Son/Startup.wav"
sounda=pygame.mixer.Sound(son)
channela = sounda.play()
son = "/var/www/html/Son/sent2.wav"
sounda=pygame.mixer.Sound(son)
channela = sounda.play()


class WSHandler(tornado.websocket.WebSocketHandler):

  def check_origin(self, origin):
      return True

  def open(self):
    print 'user is connected.\n'

  def on_message(self, message):
    print 'received message: %s\n' %message
    son =message.strip()
    sounda=pygame.mixer.Sound(son)
    channela = sounda.play()
    while channela.get_busy():
	pygame.time.delay(50)

  def on_close(self):
        print 'connection closed\n'

application = tornado.web.Application([(r'/ws', WSHandler),])

if __name__ == "__main__":
  http_server = tornado.httpserver.HTTPServer(application)
  http_server.listen(8890
  tornado.ioloop.IOLoop.instance().start()
