multibrowser与flas-socketio聊天

时间:2014-06-23 00:52:39

标签: python flask socket.io gevent-socketio

我正在尝试创建一个简单的多浏览器聊天Web应用程序。我以为我完成了它。但是当我尝试运行它时,我获得500状态和

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

不确定为什么会这样。我在Flask做这个项目,我目前正在学习。因为我目前正在学习Flask和Flask-socketio以及websockets。

chatty.py:

from gevent import monkey
# Do all of the default monkey patching (calls every other function in this module.
monkey.patch_all()

from threading import Thread
from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit
from time import strftime

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app)
thread = None


@app.route('/')
def index():
    global thread
    if thread is None:
        #thread = Thread(target=background_thread)
        thread.start()
    return render_template('index.html')

# handle messages
@socketio.on('send message')
def handle_message(message):
    #msg = strftime("%H:%M:%S") + ': ' + str(message)
    emit('response', 
            {'data': strftime("%H:%M:%S") + ': ' + message['data']}, 
            broadcast = True)

@socketio.on('connect')
def conn():
    emit('response', {'data': 'Connected'})

if __name__ == '__main__':
    socketio.run(app)

的index.html:

    <html>
    <!-- jQuery and socket.io -->
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            var socket = io.connect('https://' + document.domain + ':' + location.port);

            socket.on('connect', function() {
                socket.emit('', {data: 'I\'m connected!'});
            });

            socket.on('response', function(msg) {
                $('#log').append(msg.data);
            });

            $('form#broadcast').submit(function() {
                socket.emit('send message', { data: $('#msg').val()});
            })

        })
    </script>

    <form id="broadcast" method="POST">
        <input type="text" name="message" id="msg" placeholder="Enter message here...">
        <input type="submit" value="Send">
    </form>

    <h2>Chat</h2>
    <div id="log"></div>

</html>

0 个答案:

没有答案