added rooms, clients can talk to each other
This commit is contained in:
parent
51fa60036c
commit
20e78c095b
15
anonkun.py
15
anonkun.py
|
@ -7,7 +7,7 @@ import time
|
|||
|
||||
from flask import Flask, session, request, abort, redirect, url_for, g, \
|
||||
render_template
|
||||
from flask_socketio import SocketIO, emit
|
||||
from flask_socketio import SocketIO, emit, join_room
|
||||
|
||||
class ReverseProxied(object):
|
||||
"""
|
||||
|
@ -50,24 +50,29 @@ app.config['SECRET_KEY'] = 'secret!'
|
|||
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
||||
socketio = SocketIO(app)
|
||||
|
||||
@socketio.on('joined', namespace='/chat')
|
||||
def joined(message):
|
||||
@socketio.on('joined', namespace="/chat")
|
||||
def joined(data):
|
||||
"""
|
||||
Sent by clients when they enter a room.
|
||||
"""
|
||||
room = data["room"]
|
||||
join_room(room)
|
||||
print("Client connected.")
|
||||
|
||||
|
||||
@socketio.on('text', namespace='/chat')
|
||||
@socketio.on('message', namespace="/chat")
|
||||
def text(data):
|
||||
"""
|
||||
Sent by a client when the user entered a new message.
|
||||
"""
|
||||
room = data["room"]
|
||||
|
||||
message = data["message"]
|
||||
name = data["name"]
|
||||
date = int(time.time())
|
||||
data["date"] = date
|
||||
emit('message', data)
|
||||
|
||||
emit('message', data, room=room)
|
||||
|
||||
|
||||
@app.template_filter("strftime")
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
socket = io.connect('https://' + document.domain + ':' + location.port + '/chat');
|
||||
socket.on('connect', function() {
|
||||
socket.emit('joined', {});
|
||||
socket.emit('joined', {room: 'index'});
|
||||
});
|
||||
socket.on('message', function(data) {
|
||||
date = new Date(data.date * 1000);
|
||||
|
@ -30,7 +30,7 @@
|
|||
if (event.key == 'Enter') {
|
||||
text = document.getElementById('text').value;
|
||||
document.getElementById('text').value = '';
|
||||
socket.emit('text', {message: text, name: 'Anonymous'});
|
||||
socket.emit('message', {message: text, name: 'Anonymous', room: 'index'});
|
||||
}
|
||||
});
|
||||
}, 100 );
|
||||
|
|
Loading…
Reference in New Issue
Block a user