有没有人有一个gevent-socketio的工作示例



bitbucket 和 github 中 gevent-socketio 的所有分支都有不起作用的示例/聊天.py。谁能给我找到一个gevent-socketio的工作示例?

在以下位置使用新的官方存储库:

  • https://github.com/abourget/gevent-socketio

并查看其中的示例应用程序,大多数现在应该是最新的(我认为最近有一个提交,对 chat.py 示例进行了一些修复)

  • https://github.com/abourget/gevent-socketio/tree/master/examples

另请查看文档:

  • http://readthedocs.org/docs/gevent-socketio/en/latest/

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

您使用的浏览器。我在IE上看到了这种行为。Mozilla和chrome都很好。我已经修复的 flashscket 协议存在问题,因此 IE 应该可以工作,但 jquery UI 不起作用,这就是问题所在。不知道足够的JS来修复它

相关内容

  • 没有找到相关文章

最新更新