Skip to content

Commit 8cc6ede

Browse files
committed
Report the number of connected clients
1 parent aa9c137 commit 8cc6ede

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

client/data-tool/src/components/Search.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useSpinner from './useSpinner';
99

1010
export default function Search(props) {
1111
const socketSessionId = useSelector(state => state.socket ? state.socket.id : 0)
12+
const numberPeople = useSelector(state => state.numberPeople);
1213
const { register, errors, handleSubmit } = useForm({ mode: "onBlur" });
1314
const [datasetSession, setDatasetSession] = useState(true);
1415
const [loading, showLoading, hideLoading] = useSpinner();
@@ -75,7 +76,7 @@ export default function Search(props) {
7576
<button className="search" name="search" type="submit" ref={register}>Search</button>
7677
</form>
7778
</div>
78-
<p className='logo-people'>There are now <span className='logo-people number'>X</span> people together with you.</p>
79+
<p className='logo-people'>There are now <span className='logo-people number'>{numberPeople - 1}</span> people together with you.</p>
7980
<ToastContainer position='top-left' delay={8000} />
8081
</div>
8182
)

client/data-tool/src/state.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ const reducer = (state = {
1414
}, action) => {
1515
switch (action.type) {
1616

17-
case 'SET_SOCKET': {
18-
console.log("Setting socket", action.socket);
19-
return {...state, socket: action.socket}
20-
}
17+
case 'SET_SOCKET': {
18+
console.log("Setting socket", action.socket);
19+
action.socket.off('connected-clients');
20+
action.socket.on('connected-clients', (data) => {
21+
console.log('Conneted clients:', data);
22+
store.dispatch({
23+
type: 'NUMBER_PEOPLE',
24+
value: data.value
25+
})
26+
});
27+
return {...state, socket: action.socket}
28+
}
2129
case 'SET_SESSION': {
2230
console.log("Setting session", action.session);
2331
return {...state, session: action.session}
@@ -49,6 +57,12 @@ const reducer = (state = {
4957
cropImages: [...state.cropImages, action.cropImages]
5058
}
5159
}
60+
case 'NUMBER_PEOPLE': {
61+
return {
62+
...state,
63+
numberPeople: action.value
64+
}
65+
}
5266
case 'SAVE_VALUE_SLIDER': {
5367
return {
5468
...state,

server/server.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ def run(self):
209209
print("Failed")
210210
pass
211211

212+
connected_clients = 0
213+
212214
app = Flask(__name__)
213215
app.config['SECRET_KEY'] = 'mysecret'
214216
socketio = SocketIO(app, cors_allowed_origins="*")
@@ -233,6 +235,7 @@ def run(self):
233235
)
234236
poser.start()
235237

238+
236239
@app.route('/sessions/<path:filepath>')
237240
def data(filepath):
238241
return send_from_directory('sessions', filepath)
@@ -327,14 +330,24 @@ def catch_all(path):
327330

328331
@socketio.on('connect')
329332
def on_connect():
330-
print("Client connected {}".format(request.sid))
331-
join_room(request.sid)
333+
global connected_clients
334+
connected_clients = connected_clients + 1
335+
print("Client connected {}. Total: {}".format(request.sid, connected_clients))
336+
emit('connected-clients',{'value': connected_clients },broadcast=True, namespace='/')
337+
#join_room(request.sid)
332338
# if(request.method=='POST'):
333339
# some_json=request.get_json()
334340
# return jsonify({"key":some_json})
335341
# else:
336342
# return jsonify({"GET":"GET"})
337343

344+
@socketio.on('disconnect')
345+
def on_disconnect():
346+
global connected_clients
347+
connected_clients = connected_clients - 1
348+
print("Client disconnected {}. Total: {}".format(request.sid, connected_clients))
349+
emit('connected-clients',{'value': connected_clients },broadcast=True, namespace='/')
350+
338351
if __name__ == '__main__':
339352

340353
#print("Generating samples")

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -3386,9 +3386,9 @@ caniuse-api@^3.0.0:
33863386
lodash.uniq "^4.5.0"
33873387

33883388
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001035, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111:
3389-
version "1.0.30001114"
3390-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001114.tgz#2e88119afb332ead5eaa330e332e951b1c4bfea9"
3391-
integrity sha512-ml/zTsfNBM+T1+mjglWRPgVsu2L76GAaADKX5f4t0pbhttEp0WMawJsHDYlFkVZkoA+89uvBRrVrEE4oqenzXQ==
3389+
version "1.0.30001252"
3390+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001252.tgz"
3391+
integrity sha512-I56jhWDGMtdILQORdusxBOH+Nl/KgQSdDmpJezYddnAkVOmnoU8zwjTV9xAjMIYxr0iPreEAVylCGcmHCjfaOw==
33923392

33933393
capture-exit@^2.0.0:
33943394
version "2.0.0"

0 commit comments

Comments
 (0)