2012-04-18 89 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

相關問題