2012-04-18 38 views
4

在bitbucket和github中的gevent-socketio的所有分支都有示例/ chat.py不起作用。 任何人都可以找到我一个gevent-socketio的工作示例吗?有没有人有gevent-socketio的工作示例?

+0

什么失败? 您是否安装了所需的库? 'easy_install gevent gevent-socketio' – 2012-04-26 05:33:41

+1

没有失败。所有库都安装成功。 'python chat.py'也在运行。但在浏览器中,输入昵称并点击“输入”后,聊天窗口不会出现。相反,昵称输入“行为”,就好像它没有提交表单一样。而在gevent-socketio的一些分支中,味精只出现在自己的聊天窗口中,而不出现在其他用户的聊天窗口中。 – suzanshakya 2012-04-26 06:25:54

回答

3

使用在新的官方资料库:

,并看一看的例子应用程序在那里,大多数应该是现在最新的(我认为有一些提交修正了c最近hat.py例子)

看看该文档也:

+1

https://github.com/abourget/gevent-socketio中的chat.py仍然不起作用。 – suzanshakya 2012-04-26 04:35:14

+0

它在https://github.com/abourget/gevent-socketio/pull/25应用此修补程序后工作,如https://github.com/abourget/gevent-socketio/issues/12#issue-ref-13709053 。 – suzanshakya 2012-04-26 05:26:03

+0

我还发现聊天示例没有工作,但那是因为我安装了旧版本的gevent/greenlet/libevent。将来,请包含您收到的错误消息;我在Environ ['socketio']上得到了KeyError。 – 2012-06-29 18:04:53

1

我在websocket上制作。 这是草稿的代码,但它的工作。

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

谢谢。但所有浏览器都不支持websocket。所以我想使用支持不同传输机制的socket.io。 – suzanshakya 2012-04-18 08:28:53

+0

是的,我知道,但我不会用socket.io尝试,但websockets可以在所有现代浏览器中使用。 – Denis 2012-04-18 10:32:17

+0

suzanshakya the IE。 ui是底层套接字socket.io的代码是由我修复的,你必须从github上获取最新的副本 – vivekv 2012-06-30 04:59:11

1

你用什么浏览器。我在IE中看到了这种行为。 Mozilla和Chrome都很好。有FlashSocket协议,我已经修复的问题,即应该工作,但jQuery UI不工作,这是问题。不知道足够的JS来修复它

+0

我使用Opera,Mozilla,Chrome和Safari。 – suzanshakya 2012-07-01 11:20:28

相关问题