bitbucket 和 github 中 gevent-socketio 的所有分支都有不起作用的示例/chat.py。谁能给我找到一个 gevent-socketio 的工作示例?
			
			7645 次
		
3 回答
            3        
        
		
在以下位置使用新的官方存储库:
并查看其中的示例应用程序,大多数现在应该是最新的(我认为最近有一个对 chat.py 示例进行了一些修复的提交)
还请查看文档:
于 2012-04-20T03:38:41.767   回答
    
    
            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>
    于 2012-04-18T07:16:34.713   回答
    
    
            1        
        
		
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
于 2012-06-30T04:55:37.503   回答