suzanshakya
suzanshakya

Reputation: 3650

Do anyone have a working example of gevent-socketio?

All the forks of gevent-socketio in bitbucket and github have examples/chat.py that do not work. Can anyone find me a working example of gevent-socketio?

Upvotes: 4

Views: 7753

Answers (3)

vivekv
vivekv

Reputation: 2298

what browser do you use. I saw this behavior with IE. both Mozilla and chrome were fine. there were issues with the flashscket protocol which I have fixed so ie should work but the jquery UI does not work that is the issue. don't know enough JS to fix it

Upvotes: 1

abourget
abourget

Reputation: 2409

Use the new official repository at:

and take a look at the example apps in there, most should be up to date now (I think there was a commit with some fixes to the chat.py example recently)

Take a look at the docs also:

Upvotes: 3

Denis
Denis

Reputation: 7343

I make on websockets. This is draft code but it work.

import os
from gevent.pywsgi import WSGIServer
import geventwebsocket

class eServer(object):

    def __init__(self):
        path = os.path.dirname(geventwebsocket.__file__)
        agent = "gevent-websocket/%s" % (geventwebsocket.__version__)
        print "Running %s from %s" % (agent, path)
        self.all_socks = []
        self.s = WSGIServer(("", 8000), self.echo, handler_class=geventwebsocket.WebSocketHandler)
        self.broken_socks = []
        self.s.serve_forever()

    def echo(self, environ, start_response):
        websocket = environ.get("wsgi.websocket")
        if websocket is None:
            return http_handler(environ, start_response)
        try:
            while True:
                message = websocket.receive()
                if message is None:
                    break
                self.sock_track(websocket)
                for s in self.all_socks:
                    try:
                        s.send(message)
                    except Exception:
                        print "broken sock"
                        self.broken_socks.append(s)
                        continue
                if self.broken_socks:
                     for s in self.broken_socks:
                         print 'try close socket'
                         s.close()
                        if s in self.all_socks:
                            print 'try remove socket'
                            self.all_socks.remove(s)
                    self.broken_sock = []
                    print self.broken_sock
            websocket.close()
        except geventwebsocket.WebSocketError, ex:
            print "%s: %s" % (ex.__class__.__name__, ex)


    def http_handler(self, environ, start_response):
        if environ["PATH_INFO"].strip("/") == "version":
            start_response("200 OK", [])
            return [agent]
        else:
            start_response("400 Bad Request", [])
            return ["WebSocket connection is expected here."]

    def sock_track(self, s):
        if s not in self.all_socks:
            self.all_socks.append(s)
            print self.all_socks



s = eServer()

and client's html like:

<html>
<head>
    <script type="text/javascript" src="http://yandex.st/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
    var socket = new WebSocket("ws://localhost:8000");
    socket.onopen = function(){
        console.log('socket open');
}
    socket.onmessage = function(msg){
        console.log(msg);
        $('#recive').after('<p>'+msg.data+'</p>');
}
    $('#send-btn').click(function(){
        var txt = $('#txt').val();
        console.log(txt);
        socket.send(txt);
    })

});

    </script>
</head>
<body>
    <textarea id="txt"></textarea>
    <input type="button" id="send-btn" value="Send"></input>
    <div id="recive"></div>
</body>
</html>

Upvotes: 1

Related Questions