Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from websocket import Server
- import traceback
- server = Server('localhost', 9999)
- log = open('simplechat.log').read().splitlines()
- logfile = open('simplechat.log', 'a')
- readers = set()
- def on_open(handler, headers):
- ip, port = handler.address
- print "%r:%r connected to resource: %r" % (ip, port, handler.path)
- readers.add(handler)
- for message in log[-50:]:
- handler.send(message)
- server.on_open = on_open
- def on_message(handler, message):
- print "message received! %r" % message
- message = message.replace('\n', ' ')
- for reader in readers:
- reader.send(message)
- log.append(message)
- logfile.write(message + '\n')
- server.on_message = on_message
- def on_close(handler):
- print "%r:%r connection closed" % handler.address
- readers.discard(handler)
- server.on_close = on_close
- def on_exception(handler, e):
- print "error at connection %r:%r" % handler.address
- print traceback.format_exc()
- server.on_exception = on_exception
- while True:
- server.process()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement