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 @@ + + + + + + +

Autobahn WebSocket Broadcast Demo

+ +
+

Broadcast Message:

+
+ +

+   
+
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..50dbe94
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,11 @@
+certifi==2018.8.24
+chardet==3.0.4
+crypto==1.4.1
+idna==2.7
+Naked==0.1.31
+numpy==1.15.2
+opencv-python==3.4.3.18
+PyYAML==3.13
+requests==2.19.1
+shellescape==3.4.1
+urllib3==1.23
diff --git a/uiServer.py b/uiServer.py
deleted file mode 100644
index 5f107b5..0000000
--- a/uiServer.py
+++ /dev/null
@@ -1,48 +0,0 @@
-#!/usr/bin/env python
-import os
-import subprocess
-import sys
-
-from compression.compressor import Compressor
-from transport.server.UdpSocket import UdpSocket
-from encryption.encryptor import Encryptor
-from config.config import Config
-
-
-sys.path.insert(0, os.getcwd())
-
-
-# Get the args
-def main():
-    # Clear the screen
-    subprocess.call('clear', shell=True)
-    config_object = Config(os.getcwd() + '/config/config.ini').raw_config_object
-    transport = UdpSocket(config_object)
-
-    if config_object['COMPRESSION']['switch'] == 'On':
-        compressor = Compressor(config_object)
-        transport.add_compression(compressor)
-
-    if config_object['ENCRYPTION']['switch'] == 'On':
-        #encryptor = Encryptor(config_object)
-        #transport.add_encryption(encryptor)
-        print("test")
-    try:
-        while True:
-            '''
-            do stuff
-            '''
-            transport.handle_client()
-            print('\nPrint ... Server running \n')
-
-    except LookupError as e:
-        print(e)
-        sys.exit(2)
-
-    except KeyboardInterrupt:
-        print('keyboard interruption')
-        sys.exit(1)
-
-
-if __name__ == "__main__":
-    main()