我正在尝试做一个音频聊天室。我已经阅读了很多教程,但似乎无法完成这项工作。我找到了这个存储库,在那里我找到了录制音频的示例(我需要这个功能)和一个基本的文本聊天室。
聊天的服务器端代码,它还包括录音所需的代码:
from flask import Blueprint, render_template, request, session, url_for, \
current_app
from flask_socketio import emit
from socketio_examples import socketio
import uuid
import wave
bp = Blueprint('chat', __name__, static_folder='static',
template_folder='templates')
@bp.route('/')
def index():
"""Return the client application."""
chat_url = current_app.config.get('CHAT_URL') or \
url_for('chat.index', _external=True)
return render_template('chat/main.html', chat_url=chat_url)
@socketio.on('connect', namespace='/chat')
def on_connect():
"""A new user connects to the chat."""
if request.args.get('username') is None:
return False
session['username'] = request.args['username']
emit('message', {'message': session['username'] + ' has joined.'},
broadcast=True)
@socketio.on('disconnect', namespace='/chat')
def on_disconnect():
"""A user disconnected from the chat."""
emit('message', {'message': session['username'] + ' has left.'},
broadcast=True)
@socketio.on('post-message', namespace='/chat')
def on_post_message(message):
"""A user posted a message to the chat."""
emit('message', {'user': session['username'],
'message': message['message']},
broadcast=True)
@socketio.on('start-recording', namespace='/audio')
def start_recording(options):
"""Start recording audio from the client."""
id = uuid.uuid4().hex # server-side filename
session['wavename'] = id + '.wav'
wf = wave.open(current_app.config['FILEDIR'] + session['wavename'], 'wb')
wf.setnchannels(options.get('numChannels', 1))
wf.setsampwidth(options.get('bps', 16) // 8)
wf.setframerate(options.get('fps', 44100))
session['wavefile'] = wf
@socketio.on('write-audio', namespace='/audio')
def write_audio(data):
"""Write a chunk of audio from the client."""
session['wavefile'].writeframes(data)
@socketio.on('end-recording', namespace='/audio')
def end_recording():
"""Stop recording audio from the client."""
emit('add-wavefile', url_for('static',
filename='_files/' + session['wavename']))
session['wavefile'].close()
del session['wavefile']
del session['wavename']
客户端的 JS 文件:
var input_field = document.getElementById('message');
var chat_field = document.getElementById('chat');
// create a random username
var username = 'user' + parseInt(Math.random() * 10000);
var socketio = io.connect(location.origin + '/chat',
{query: 'username=' + username, 'transports': ['websocket']});
// event handler when ENTER is pressed on the chat input field
input_field.onchange = function() {
socketio.emit('post-message', { message: this.value });
this.value = '';
}
// the server is sending a message to display in the chat window
socketio.on('message', function(message) {
msg = document.createElement('p');
if (message.user) {
// this is a message written by a user
msg.innerHTML = '<span class="user">' + message.user + '</span>: ' +
'<span class="message">' + message.message + "</span>";
}
else {
// this is a control message that comes from the server itself
msg.innerHTML = '<span class="control-message">' + message.message + '</span>';
}
chat_field.appendChild(msg);
chat_field.scrollTop = chat.scrollHeight; // scroll to bottom
});
input_field.focus();
有什么方法可以修改聊天室以用于实时音频流而不是文本?