Liondancer
Liondancer

Reputation: 16479

multibrowser chatting with flas-socketio

I am trying to create a simple multi browser chat web application. I thought I finished creating it. But when I try run it I get 500 status and

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.

Not really sure why this is occurring. I am doing this project in Flask and I am currently learning at the same time. Because I am currently learning Flask and Flask-socketio and 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>

Upvotes: 0

Views: 55

Answers (0)

Related Questions