summaryrefslogtreecommitdiffstats
path: root/webapp
diff options
context:
space:
mode:
authorgroup1 <f16_group1@pbridge.adm.cs.cmu.edu>2016-10-09 14:45:03 -0400
committergroup1 <f16_group1@pbridge.adm.cs.cmu.edu>2016-10-09 14:45:03 -0400
commitdfe1127addf7e31acd63e87db0a5d5e35628ea67 (patch)
tree101526e338590ba0980ff1809a8338cd12b50b4e /webapp
parentdd7dd93d9e1ee2e0095f0fa403a499b69aa289a8 (diff)
downloadbridge-dfe1127addf7e31acd63e87db0a5d5e35628ea67.tar.zst
Add twinkle
Diffstat (limited to 'webapp')
-rw-r--r--webapp/app/__init__.py11
-rw-r--r--webapp/app/templates/index.html55
-rw-r--r--webapp/app/views.py37
-rwxr-xr-xwebapp/run.py7
4 files changed, 110 insertions, 0 deletions
diff --git a/webapp/app/__init__.py b/webapp/app/__init__.py
new file mode 100644
index 0000000..aa2e264
--- /dev/null
+++ b/webapp/app/__init__.py
@@ -0,0 +1,11 @@
+from flask import Flask
+import socketio
+import eventlet
+import eventlet.wsgi
+
+sio = socketio.Server(async_mode='threading')
+app = Flask(__name__)
+app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
+from app import views
+
+
diff --git a/webapp/app/templates/index.html b/webapp/app/templates/index.html
new file mode 100644
index 0000000..b0d9bb3
--- /dev/null
+++ b/webapp/app/templates/index.html
@@ -0,0 +1,55 @@
+<html>
+ <head>
+ <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js"></script>
+ <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
+
+ <script>
+ var SPAWN_URL = "/spawn";
+
+ function creature(name, socket) {
+ var self = this;
+ self.name = name;
+ self.socket = socket;
+
+ self.render = function($parent) {
+ var $a = $('<a>');
+ $a.text(self.name);
+ $a.click(self.click.bind(self));
+
+ var $li = $('<li>');
+ $li.append($a);
+ $parent.append($li);
+ };
+
+ self.click = function() {
+ self.socket.emit('spawn', {name:self.name});
+ }
+ }
+
+ $(document).ready(function() {
+ var namespace = '/spawn';
+ var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
+
+ socket.on('connect', function() {
+ console.log('connected!');
+ });
+
+ socket.on('new creature', function(msg) {
+ console.log('new!');
+ var c = new creature(msg.name, socket);
+ c.render($('#creatures'));
+ });
+
+ socket.on('disconnect', function() {
+ console.log('disconnected');
+ });
+ });
+
+ </script>
+ <title>{{ title }}</title>
+ </head>
+ <body>
+ <h1>Ocean Fun Times</h1>
+ <ul id="creatures"></ul>
+ </body>
+</html>
diff --git a/webapp/app/views.py b/webapp/app/views.py
new file mode 100644
index 0000000..4aa2c05
--- /dev/null
+++ b/webapp/app/views.py
@@ -0,0 +1,37 @@
+from flask import render_template, request
+from app import app, sio
+import time
+
+import threading
+
+thread = None
+
+@app.route('/')
+@app.route('/index')
+def index():
+ return render_template('index.html',
+ title='F16 Group1')
+
+connections = set()
+
+@sio.on('connect', namespace='/spawn')
+def connect(sid, environ):
+ print("connect ", sid)
+ connections.add(sid)
+
+@sio.on('spawn', namespace='/spawn')
+def message(sid, data):
+ print("message ", data)
+
+@sio.on('disconnect', namespace='/spawn')
+def disconnect(sid):
+ print('disconnect', sid)
+ connections.remove(sid)
+
+
+
+def add_creature():
+ time.sleep(5)
+ sio.emit('new creature', {'name': 'fish1'}, namespace='/spawn')
+
+thread = sio.start_background_task(add_creature)
diff --git a/webapp/run.py b/webapp/run.py
new file mode 100755
index 0000000..b5c8c6f
--- /dev/null
+++ b/webapp/run.py
@@ -0,0 +1,7 @@
+#!/usr/bin/env python
+
+import sys
+sys.path.append('packages')
+
+from app import app
+app.run(threaded=True, host='', port=8000)