Commit df2314fc authored by Jed Simson's avatar Jed Simson

Testing out WebSockets

parent 6f9b1e10
from flask import Blueprint, request, jsonify, render_template
from flask_socketio import SocketIO, emit
from lib.slider import Slider
import time
......@@ -7,20 +9,46 @@ import time
api = Blueprint('api', __name__, url_prefix='/api')
slider = Slider()
socketio = SocketIO()
@api.route('/')
def index():
return jsonify({'name:': 'Reddit Slider',
'description': 'A simple program to allow greater filtering options for Reddit submissions.',
'version': 1.0})
@api.route('/test/')
def test():
return render_template('test.html')
@socketio.on('connect', namespace='/api')
def test_connect():
print('Received connect')
emit('response', {'data': 'Connected'})
@socketio.on('message event', namespace='/api')
def test_message(message):
print('Received message')
emit('response', {'data': 'Received message: ' + str(message['data'])})
@socketio.on('disconnect', namespace='/api')
def test_disconnect():
print('Received disconnect')
print('Client disconnected')
@socketio.on('search event', namespace='/api')
def search_event(subreddit):
pass
@api.route('/search/<subreddit>/')
def subreddit(subreddit):
def search(subreddit):
dimension = request.args.get('range', None)
delta = request.args.get('delta', None)
sort = request.args.get('sort', 'hot')
limit = int(request.args.get('limit', 100))
sliding_window = request.args.get('use_sliding_window', False)
sliding_window = True if sliding_window == 'true' else False
t0 = time.time()
......@@ -29,8 +57,7 @@ def subreddit(subreddit):
dimension,
delta,
sort,
limit,
sliding_window=sliding_window)
limit)
except ValueError as e:
t1 = time.time()
t = round(t1 - t0, 4)
......
from flask import Flask
from api.views import api
from api.views import api, socketio
from slider.views import slider
import os
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
socketio.init_app(app)
app.register_blueprint(api)
app.register_blueprint(slider)
......@@ -12,4 +18,5 @@ def create_app():
if __name__ == "__main__":
app = create_app()
app.run(host='0.0.0.0', port=5050, debug=True)
#app.run(host='0.0.0.0', port=5050, debug=True)
socketio.run(app)
......@@ -95,7 +95,16 @@ class Search:
r = Reddit('Reddit Slider by /u/oracular_demon - v{}'.format(self.version))
generator = submissions_between(r, subreddit, low_timestamp, high_timestamp)
submissions = []
submissions = [submission for submission in generator]
sorted_submissions = sorted(submissions, key=self.sort_fns[sort])
current = 0
while current < min(limit, len(sorted_submissions)):
current += 1
yield sorted_submissions[current]
'''
count = 0
while count <= limit:
......@@ -113,6 +122,7 @@ class Search:
for submission in sorted_submissions:
yield submission
'''
......
<!DOCTYPE HTML>
<html>
<head>
<title>Flask-SocketIO Test</title>
<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
namespace = '/api'; // change to an empty string to use the global namespace
// the socket.io documentation recommends sending an explicit package upon connection
// this is specially important when using the global namespace
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
// event handler for server sent data
// the data is displayed in the "Received" section of the page
socket.on('response', function(msg) {
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.count + ': ' + msg.data).html());
});
// event handler for new connections
socket.on('connect', function() {
socket.emit('message event', {data: 'I\'m connected!'});
});
// handlers for the different forms in the page
// these send data to the server in a variety of ways
$('form#emit').submit(function(event) {
console.log('Emit');
socket.emit('message event', {data: $('#emit_data').val()});
return false;
});
$('form#disconnect').submit(function(event) {
socket.emit('disconnect');
return false;
});
});
</script>
</head>
<body>
<h2>Send:</h2>
<form id="emit" method="POST" action='#'>
<input type="text" name="emit_data" id="emit_data" placeholder="Message">
<input type="submit" value="Echo">
</form>
<form id="disconnect" method="POST" action="#">
<input type="submit" value="Disconnect">
</form>
<h2>Receive:</h2>
<div id="log"></div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment