Browse code
fixing active users to avoid streaming problems
fiddlerwoaroof authored on 19/10/2015 22:59:26
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -2,7 +2,7 @@ from __future__ import division |
2 | 2 |
import json |
3 | 3 |
|
4 | 4 |
import flask |
5 |
-from flask import Blueprint, session, redirect, url_for, escape, request, abort, g, current_app, stream_with_context |
|
5 |
+from flask import Blueprint, session, redirect, url_for, escape, request, abort, g |
|
6 | 6 |
from flask.ext.cors import cross_origin |
7 | 7 |
from flask.ext.login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user |
8 | 8 |
import psycopg2 |
... | ... |
@@ -91,26 +91,6 @@ def follows(to): |
91 | 91 |
# def list_users(): |
92 | 92 |
# return json.dumps([_ for _ in users.keys()]) |
93 | 93 |
|
94 |
-class ServerSentEvent(object): |
|
95 |
- def __init__(self, id, event, data): |
|
96 |
- self.data = data |
|
97 |
- self.event = event |
|
98 |
- self.id = id |
|
99 |
- self.desc_map = { |
|
100 |
- self.data : "data", |
|
101 |
- self.event : "event", |
|
102 |
- self.id : "id" |
|
103 |
- } |
|
104 |
- |
|
105 |
- def encode(self): |
|
106 |
- if not self.data: |
|
107 |
- return "" |
|
108 |
- lines = ["%s: %s" % (v, k) |
|
109 |
- for k, v in self.desc_map.iteritems() if k] |
|
110 |
- |
|
111 |
- return "%s\n\n" % "\n".join(lines) |
|
112 |
- |
|
113 |
- |
|
114 | 94 |
@user_blueprint.route('/add', methods=['POST']) |
115 | 95 |
def adduser(): |
116 | 96 |
db = database.get_db() |
... | ... |
@@ -142,46 +122,22 @@ def adduser(): |
142 | 122 |
else: db.commit() |
143 | 123 |
return json.dumps(result) |
144 | 124 |
|
145 |
-import time |
|
146 | 125 |
@user_blueprint.route('/active') |
147 | 126 |
def active(): |
148 |
- def get_event(): |
|
149 |
- result = dict(status=False, data=[]) |
|
150 |
- with database.get_db() as db: |
|
151 |
- with db.cursor() as cur: |
|
152 |
- cur.execute("SELECT * FROM recently_active_users ORDER BY posted DESC LIMIT 10") |
|
153 |
- result['status'] = True |
|
154 |
- store = result['data'] |
|
155 |
- for id,name,last_posted in cur.fetchall(): |
|
156 |
- store.append( |
|
157 |
- dict( |
|
158 |
- id=id, |
|
159 |
- name=name, |
|
160 |
- last_posted=last_posted.isoformat() |
|
161 |
- ) |
|
127 |
+ result = dict(status=False, data=[]) |
|
128 |
+ with database.get_db() as db: |
|
129 |
+ with db.cursor() as cur: |
|
130 |
+ cur.execute("SELECT * FROM recently_active_users") |
|
131 |
+ store = result['data'] |
|
132 |
+ for id,name,last_posted in cur.fetchall(): |
|
133 |
+ store.append( |
|
134 |
+ dict( |
|
135 |
+ id=id, |
|
136 |
+ name=name, |
|
137 |
+ last_posted=last_posted.isoformat() |
|
162 | 138 |
) |
163 |
- return json.dumps(result) |
|
164 |
- def poll(): |
|
165 |
- try: |
|
166 |
- id = 1 |
|
167 |
- t0 = time.time() |
|
168 |
- data = get_event() |
|
169 |
- ev = ServerSentEvent(id, "active", data).encode() |
|
170 |
- yield ev |
|
171 |
- while True: |
|
172 |
- t1 = time.time() |
|
173 |
- if t1 - t0 > 5: |
|
174 |
- id += 1 |
|
175 |
- data = get_event() |
|
176 |
- ev = ServerSentEvent(id, "active", data).encode() |
|
177 |
- yield ev |
|
178 |
- t0 = t1 |
|
179 |
- time.sleep(0.1) |
|
180 |
- except GeneratorExit: |
|
181 |
- print 'GeneratorExit!' |
|
182 |
- response = flask.Response(stream_with_context(poll()), mimetype="text/event-stream") |
|
183 |
- response.headers['X-Accel-Buffering'] = 'no' |
|
184 |
- return response |
|
139 |
+ ) |
|
140 |
+ return (json.dumps(result), 200, {'Content-Type': 'application/json'}) |
|
185 | 141 |
|
186 | 142 |
@user_blueprint.route('/following') |
187 | 143 |
@login_required |
... | ... |
@@ -265,13 +265,14 @@ marrowApp.controller('UserSettingCtrl', function ($scope,$http,$location) { |
265 | 265 |
}); |
266 | 266 |
|
267 | 267 |
marrowApp.controller('SidebarCtrl', function ($scope,$http,$location,$route, $window, UserService) { |
268 |
- eventSource = new EventSource("/api/user/active"); |
|
269 |
- $scope.activeUsers = Object.create(null); |
|
270 |
- $scope.activeUsers.users = [] |
|
271 |
- eventSource.addEventListener("active", function(event) { |
|
272 |
- var users = $scope.activeUsers.users; |
|
273 |
- Array.prototype.splice.apply(users, [0, users.length].concat(JSON.parse(event.data).data)); |
|
274 |
- }); |
|
268 |
+ //eventSource = new EventSource("/api/user/active"); |
|
269 |
+ $scope.activeUsers = UserService.active(); |
|
270 |
+ //$scope.activeUsers = Object.create(null); |
|
271 |
+ //$scope.activeUsers.users = [] |
|
272 |
+ //eventSource.addEventListener("active", function(event) { |
|
273 |
+ // var users = $scope.activeUsers.users; |
|
274 |
+ // Array.prototype.splice.apply(users, [0, users.length].concat(JSON.parse(event.data).data)); |
|
275 |
+ //}); |
|
275 | 276 |
|
276 | 277 |
$scope.subscriptions = function() { |
277 | 278 |
if ($location.url() !== '/subscriptions') { $location.url('/subscriptions'); } |