Skip to content
Snippets Groups Projects
Commit 623eaf3d authored by Sven Stumm's avatar Sven Stumm
Browse files

added docker files and start scripts

parent d65f5345
No related branches found
No related tags found
No related merge requests found
FROM python:3.6-slim
MAINTAINER Sven Stumm <stumm@ip.rwth-aachen.de>
ADD ./requirements.txt /
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 8888
EXPOSE 80
WORKDIR /app
ADD app /app
CMD ["python", "app.py"]
......@@ -15,14 +15,14 @@ Already a pro? Just edit this README.md and make it your own. Want to make it ea
```
cd existing_repo
git remote add origin https://git-ce.rwth-aachen.de/cloud56-public-testing/test-ws-service.git
git remote add origin https://git-ce.rwth-aachen.de/ip/cloud56/test-ws-service.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://git-ce.rwth-aachen.de/cloud56-public-testing/test-ws-service/-/settings/integrations)
- [ ] [Set up project integrations](https://git-ce.rwth-aachen.de/ip/cloud56/test-ws-service/-/settings/integrations)
## Collaborate with your team
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import tornado.web
import tornado.websocket
import tornado.ioloop
import tornado.options
from tornado.options import define, options
import uuid
#define ports
define("http_port", default=80, help="HTTP port", type=int)
define("ws_port", default=8888, help="WebSocket port", type=int)
verbose = True
cl = []
class HttpApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler)
]
settings = dict(debug=True)
tornado.web.Application.__init__(self, handlers, **settings)
class WsApplication(tornado.web.Application):
def __init__(self):
handlers = [
(r"/ws", MainHandler)
]
settings = dict(debug=True)
tornado.web.Application.__init__(self, handlers, **settings)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Example</title>
</head>
<body>
<h1>WebSocket Example</h1>
<p>This is a simple WebSocket example.</p>
<h2><div id="TEXT"></div></h2>
<script>
let ws = new WebSocket("ws://localhost:8888/ws");
ws.onopen = function(event) {
console.log("WebSocket is open now.");
setTimeout(getUpdate, 1000);
};
getUpdate = function () {
if (!ws || ws.readyState != 1) {
setTimeout(getUpdate, 500);
} else {
//get status
message = "HelloWorld "
ws.send(message);
setTimeout(getUpdate, 1000);
}
};
ws.onmessage = function(event) {
console.log(event.data);
var elem = document.getElementById('TEXT');
if(elem != undefined && elem != null) {
elem.innerHTML = "Message From Server:" + event.data;
}
};
ws.onclose = function(event) {
console.log("WebSocket is closed now.");
};
ws.onerror = function(event) {
console.error("WebSocket error observed:", event);
};
</script>
</body>
</html>
""")
class MainHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
# Ignore Origin
return True
def open(self):
self.uid = str(uuid.uuid4())
if verbose:
logging.info("A client connected.")
if self not in cl:
cl.append(self)
self.msgcounter = 0
def on_close(self):
if verbose:
logging.info("A client disconnected")
if self in cl:
cl.remove(self)
def send_message(self, message):
removable = set()
for ws in cl:
if not ws.ws_connection or not ws.ws_connection.stream.socket:
removable.add(ws)
elif ws.uid == self.uid:
# Only answer to the same client
ws.write_message(message)
for ws in removable:
cl.remove(ws)
def on_message(self, message):
# Unnecessary sample code "replycounter" echo send
reply = str(message) + str(self.msgcounter)
self.msgcounter = (self.msgcounter + 1) % 5000
self.send_message(reply)
def main():
tornado.options.parse_command_line()
http_app = HttpApplication()
ws_app = WsApplication()
http_app.listen(options.http_port)
ws_app.listen(options.ws_port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
sudo docker build --tag wsexample:0.1 .
tornado
requests
websockets
asyncio
\ No newline at end of file
sudo docker run\
--name wsexample\
-d\
-p 8888:8888\
wsexample:0.1
sudo docker stop wsexample
sudo docker rm wsexample
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment