Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions BroadcastWebSocketsServer.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
68 changes: 68 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var sock = null;
var ellog = null;

window.onload = function() {

var wsuri;
ellog = document.getElementById('log');

if (window.location.protocol === "file:") {
wsuri = "ws://localhost:9126";
} else {
wsuri = "ws://" + window.location.hostname + ":9126";
}

if ("WebSocket" in window) {
sock = new WebSocket(wsuri);
} else if ("MozWebSocket" in window) {
sock = new MozWebSocket(wsuri);
} else {
log("Browser does not support WebSocket!");
}

if (sock) {
sock.onopen = function() {
log("Connected to " + wsuri);
}

sock.onclose = function(e) {
log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
sock = null;
}

sock.onmessage = function(e) {
log("Got echo: " + e.data);
}
}
};

function broadcast() {
var msg = document.getElementById('message').value;
if (sock) {
sock.send(msg);
log("Sent: " + msg);
} else {
log("Not connected.");
}
};

function log(m) {
ellog.innerHTML += m + '\n';
ellog.scrollTop = ellog.scrollHeight;
};
</script>
</head>
<body>
<h1>Autobahn WebSocket Broadcast Demo</h1>
<noscript>You must enable JavaScript</noscript>
<form>
<p>Broadcast Message: <input id="message" type="text" size="50" maxlength="50" value="Hello from Browser!"></p>
</form>
<button onclick='broadcast();'>Broadcast Message</button>
<pre id="log" style="height: 20em; overflow-y: scroll; background-color: #faa;"></pre>
</body>
</html>
11 changes: 11 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
48 changes: 0 additions & 48 deletions uiServer.py

This file was deleted.