Advertisement
Guest User

Untitled

a guest
Jul 19th, 2010
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from websocket import Server
  3. import traceback
  4.  
  5. server = Server('localhost', 9999)
  6. log = open('simplechat.log').read().splitlines()
  7. logfile = open('simplechat.log', 'a')
  8.  
  9. readers = set()
  10.  
  11. def on_open(handler, headers):
  12. ip, port = handler.address
  13. print "%r:%r connected to resource: %r" % (ip, port, handler.path)
  14. readers.add(handler)
  15. for message in log[-50:]:
  16. handler.send(message)
  17. server.on_open = on_open
  18.  
  19. def on_message(handler, message):
  20. print "message received! %r" % message
  21. message = message.replace('\n', ' ')
  22. for reader in readers:
  23. reader.send(message)
  24. log.append(message)
  25. logfile.write(message + '\n')
  26. server.on_message = on_message
  27.  
  28. def on_close(handler):
  29. print "%r:%r connection closed" % handler.address
  30. readers.discard(handler)
  31. server.on_close = on_close
  32.  
  33. def on_exception(handler, e):
  34. print "error at connection %r:%r" % handler.address
  35. print traceback.format_exc()
  36. server.on_exception = on_exception
  37.  
  38. while True:
  39. server.process()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement