diff --git a/BroadcastWebSocketsServer.py b/BroadcastWebSocketsServer.py new file mode 100644 index 0000000..8b1be4b --- /dev/null +++ b/BroadcastWebSocketsServer.py @@ -0,0 +1,78 @@ +import sys + +from twisted.internet import reactor +from twisted.python import log +from twisted.web.server import Site +from twisted.web.static import File + +from autobahn.twisted.websocket import WebSocketServerFactory, \ + WebSocketServerProtocol, \ + listenWS + + +class BroadcastServerProtocol(WebSocketServerProtocol): + def onOpen(self): + self.factory.register(self) + + def onMessage(self, payload, isBinary): + if not isBinary: + msg = "{} from {}".format(payload.decode('utf8'), self.peer) + self.factory.broadcast(msg) + + def connectionLost(self, reason): + WebSocketServerProtocol.connectionLost(self, reason) + self.factory.unregister(self) + + +class BroadcastServerFactory(WebSocketServerFactory): + """ + Simple broadcast server broadcasting any message it receives to all + currently connected clients. + """ + + def __init__(self, url): + WebSocketServerFactory.__init__(self, url) + self.clients = [] + self.tickcount = 0 + self.tick() + + def tick(self): + self.tickcount += 1 + self.broadcast("tick %d from server" % self.tickcount) + reactor.callLater(1, self.tick) + + def register(self, client): + if client not in self.clients: + print("registered client {}".format(client.peer)) + self.clients.append(client) + + def unregister(self, client): + if client in self.clients: + print("unregistered client {}".format(client.peer)) + self.clients.remove(client) + + def broadcast(self, msg): + print("broadcasting message '{}' ..".format(msg)) + for c in self.clients: + c.sendMessage(msg.encode('utf8')) + print("message sent to {}".format(c.peer)) + + def getClientsCount(self): + return list.count(self.clients) + + +if __name__ == '__main__': + log.startLogging(sys.stdout) + + ServerFactory = BroadcastServerFactory + # ServerFactory = BroadcastPreparedServerFactory + + factory = ServerFactory(u"ws://127.0.0.1:9126") + factory.protocol = BroadcastServerProtocol + listenWS(factory) + + webdir = File(".") + web = Site(webdir) + reactor.listenTCP(8181, web) + + reactor.run() diff --git a/README.md b/README.md index 52363cc..666654a 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,10 @@ To enable encryption you need to run keyGen.py to generate priv/public keys Also make sure that switches are on in config.ini ##How to run: -* sudo python3 client.py (on the client side (where the camera is)) -* sudo python3 server.py (on the server side (where the web ui should be)) - -## Things you need to run it -* linux -* python3 -* openCv -* python Crypto (sudo apt-get install python3-crypto) +* pip install -r requirements.txt +* adjust config.ini +* on the server python server.py +* on the client python client.py ## Trello board with info of what is going on: https://trello.com/b/ZxRjPN2B/pythonvideostream \ No newline at end of file diff --git a/config/config.ini b/config/config.ini index 560b72a..3e2de45 100644 --- a/config/config.ini +++ b/config/config.ini @@ -2,7 +2,7 @@ tmp_directory = /tmp/ buffer_file = /tmp/buff.jpg [TRANSPORT] -server_address = web2u.org +server_address = 0.0.0.0 port = 4080 ui_port = 8181 [COMPRESSION] diff --git a/index.html b/index.html new file mode 100644 index 0000000..c310958 --- /dev/null +++ b/index.html @@ -0,0 +1,68 @@ + + +
+ + + +