有没有人有gevent-socketio的工作实例?

时间:2012-04-18 06:46:20

标签: python websocket socket.io gevent

bitbucket和github中gevent-socketio的所有分支都有示例/ chat.py无法正常工作。 有谁能找到我gevent-socketio的工作实例?

3 个答案:

答案 0 :(得分:3)

使用新的官方存储库:

并查看那里的示例应用程序,大多数应该是最新的(我认为最近有一些修复了chat.py示例)

还要查看文档:

答案 1 :(得分:1)

我在websockets上制作。 这是草案代码,但它可以工作。

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()

和客户端的HTML一样:

<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>

答案 2 :(得分:1)

你使用什么浏览器。我在IE中看到了这种行为。 Mozilla和chrome都很好。我修复了flashscket协议的问题,即应该工作但是jquery UI不能正常工作。不知道足够的JS来解决它