bitbucket と github の gevent-socketio のすべてのフォークには、動作しない example/chat.py があります。gevent-socketio の実例を見つけてくれる人はいますか?
7645 次
3 に答える
3
次の新しい公式リポジトリを使用します。
そこにあるサンプルアプリを見てみましょう。ほとんどは最新の状態になっているはずです (最近、chat.py のサンプルにいくつかの修正を加えたコミットがあったと思います)。
ドキュメントも見てください:
于 2012-04-20T03:38:41.767 に答える
1
私はウェブソケットで作ります。これはドラフトコードですが、機能します。
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
どのブラウザを使用していますか。この動作は IE で見られました。Mozilla と chrome は問題ありませんでした。私が修正した flashscket プロトコルに問題があったため、動作するはずですが、jquery UI が動作しないことが問題です。それを修正するのに十分なJSを知らない
于 2012-06-30T04:55:37.503 に答える