Websocket:Server Php Ratchet,客户端Python Twisted + Autobahn - >连接被不正当地关闭了

时间:2014-04-15 09:30:45

标签: php python twisted autobahn ratchet

我有一个带棘轮/ ZMQ的php服务器。 我有一个使用Twisted和autobahn的python客户端。

当我连接客户端时,服务器检测到连接打开但客户端使用此错误关闭连接:

(Slevin@localhost 6)python test.py 
2014-04-15 10:39:44+0200 [-] Log opened.
2014-04-15 10:39:44+0200 [-] Connecting to ws://localhost:9000
2014-04-15 10:39:44+0200 [-] Starting factory <autobahn.wamp1.protocol.WampClientFactory instance at 0x3abfd88>
2014-04-15 10:39:44+0200 [MyPubSubClientProtocol,client] Connected!
**2014-04-15 10:39:44+0200 [-] Connection closed :  connection was closed uncleanly (I failed the WebSocket connection by dropping the TCP connection)**
2014-04-15 10:39:44+0200 [-] Stopping factory <autobahn.wamp1.protocol.WampClientFactory instance at 0x3abfd88>
2014-04-15 10:39:44+0200 [-] Main loop terminated.

我不明白为什么会出现这个问题...

这是我使用Twisted和Autobahn Python的客户端:

import sys

from twisted.python import log
from twisted.internet import reactor

from autobahn.twisted.websocket import connectWS

from autobahn.wamp1.protocol import WampClientFactory, \
                                    WampClientProtocol

class MyPubSubClientProtocol(WampClientProtocol):

   def onSessionOpen(self):

      def onMyEvent(self) :
         print "Suscribed"

      print "Connected!"
      #self.subscribe("http://example.com/myEvent1", onMyEvent)                                                                                                                                                    

   def onClose(self, wasClean, code, reason):
      print "Connection closed : ", reason
      reactor.stop()

if __name__ == '__main__':

   log.startLogging(sys.stdout)

   if len(sys.argv) > 1:
      wsuri = sys.argv[1]
   else:
      wsuri = "ws://localhost:9000"

   print "Connecting to", wsuri

   ## our WAMP/WebSocket client                                                                                                                                                                                    
   ##                                                                                                                                                                                                              
   factory = WampClientFactory(wsuri, debugWamp = False)
   factory.protocol = MyPubSubClientProtocol
   connectWS(factory)

   ## run the Twisted network reactor                                                                                                                                                                              
   ##                                                                                                                                                                                                              
   reactor.run()

我的服务器类和我的服务器(Php + Ratchet + ZMQ):

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

$loop   = React\EventLoop\Factory::create();
$server = new MyApp\Server;

$context = new React\ZMQ\Context($loop);
$dealer = $context->getSocket(ZMQ::SOCKET_DEALER);
$dealer->bind('tcp://127.0.0.1:5555');
$dealer->on('message', array($server, 'onBlogEntry'));

$webSock = new React\Socket\Server($loop);
$webSock->listen(9000, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(new Ratchet\Http\HttpServer
                                         (new Ratchet\WebSocket\WsServer(
                                                new Ratchet\Wamp\WampServer($server))), $webSock);

$loop->run();
?>

<?php
namespace MyApp;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;

class Server implements WampServerInterface {

  public function onSubscribe(ConnectionInterface $conn, $topic) {
    print("onSubscribe\n");
  }

  public function onBlogEntry($entry) {
    print("onBlogEntry\n");
  }

  public function onUnSubscribe(ConnectionInterface $conn, $topic) {
    print("onUnSubscribe\n");
  }
  public function onOpen(ConnectionInterface $conn) {
    print("onOpen\n");
    $conn->send("{ Welcome ! }");
  }
  public function onClose(ConnectionInterface $conn) {
    print("onClose\n");
  }
  public function onCall(ConnectionInterface $conn, $id, $topic, array $params) {
    $conn->callError($id, $topic, 'You are not allowed to make calls')->close();
    print("onCall\n");
  }
  public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
    $conn->close();
    print("onPublish\n");
  }
  public function onError(ConnectionInterface $conn, \Exception $e) {
    print("onError\n");
  }
}
?>

我真的不知道问题出在哪里...你能帮助我吗?谢谢

编辑:当我将调试设置为True时,我可以看到:

(Slevin@localhost 12)python client.py
2014-04-16 10:30:19+0200 [-] Log opened.
2014-04-16 10:30:19+0200 [-] Starting factory <autobahn.twisted.websocket.WebSocketClientFactory instance at 0x1efa830>
2014-04-16 10:30:19+0200 [Uninitialized] 
    [('debug', True, 'WebSocketClientFactory'),
     ('debugCodePaths', False, 'WebSocketClientFactory'),
     ('logOctets', True, 'WebSocketClientFactory'),
     ('logFrames', True, 'WebSocketClientFactory'),
     ('trackTimings', False, 'WebSocketClientFactory'),
     ('allowHixie76', False, 'WebSocketClientFactory'),
     ('utf8validateIncoming', True, 'WebSocketClientFactory'),
     ('applyMask', True, 'WebSocketClientFactory'),
     ('maxFramePayloadSize', 0, 'WebSocketClientFactory'),
     ('maxMessagePayloadSize', 0, 'WebSocketClientFactory'),
     ('autoFragmentSize', 0, 'WebSocketClientFactory'),
     ('failByDrop', True, 'WebSocketClientFactory'),
     ('echoCloseCodeReason', False, 'WebSocketClientFactory'),
     ('openHandshakeTimeout', 5, 'WebSocketClientFactory'),
     ('closeHandshakeTimeout', 1, 'WebSocketClientFactory'),
     ('tcpNoDelay', True, 'WebSocketClientFactory'),
     ('version', 18, 'WebSocketClientFactory'),
     ('acceptMaskedServerFrames', False, 'WebSocketClientFactory'),
     ('maskClientFrames', True, 'WebSocketClientFactory'),
     ('serverConnectionDropTimeout', 1, 'WebSocketClientFactory'),
     ('perMessageCompressionOffers', [], 'WebSocketClientFactory'),
     ('perMessageCompressionAccept',
      <function <lambda> at 0x7fb43f346a28>,
      'WebSocketClientFactory')]
2014-04-16 10:30:19+0200 [Uninitialized] connection to 127.0.0.1:8080 established
2014-04-16 10:30:19+0200 [Uninitialized] TX Octets to 127.0.0.1:8080 : sync = False, octets = 474554202f20485454502f312e310d0a557365722d4167656e743a204175746f6261686e507974686f6e2f302e382e380d0a486f73743a206c6f63616c686f73743a383038300d0a557067726164653a20576562536f636b65740d0a436f6e6e656374696f6e3a20557067726164650d0a507261676d613a206e6f2d63616368650d0a43616368652d436f6e74726f6c3a206e6f2d63616368650d0a5365632d576562536f636b65742d4b65793a2037547a617a76437166583662463442646a584b2f45773d3d0d0a5365632d576562536f636b65742d56657273696f6e3a2031330d0a0d0a
2014-04-16 10:30:19+0200 [Uninitialized] GET / HTTP/1.1
    User-Agent: AutobahnPython/0.8.8
    Host: localhost:8080
    Upgrade: WebSocket
    Connection: Upgrade
    Pragma: no-cache
    Cache-Control: no-cache
    Sec-WebSocket-Key: 7TzazvCqfX6bF4BdjXK/Ew==
    Sec-WebSocket-Version: 13


2014-04-16 10:30:19+0200 [MyClientProtocol,client] RX Octets from 127.0.0.1:8080 : octets = 485454502f312e312031303120537769746368696e672050726f746f636f6c730d0a557067726164653a20776562736f636b65740d0a436f6e6e656374696f6e3a20557067726164650d0a5365632d576562536f636b65742d4163636570743a20374f4b534f68505330766e54656a6d704855452b684a5838464c453d0d0a582d506f77657265642d42793a20526174636865742f302e330d0a0d0a81245b302c2235333465336639623532613632222c312c22526174636865745c2f302e33225d810d7b2057656c636f6d652021207d
2014-04-16 10:30:19+0200 [MyClientProtocol,client] received HTTP response:

    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: 7OKSOhPS0vnTejmpHUE+hJX8FLE=
    X-Powered-By: Ratchet/0.3




2014-04-16 10:30:19+0200 [MyClientProtocol,client] received HTTP status line in opening handshake : HTTP/1.1 101 Switching Protocols
2014-04-16 10:30:19+0200 [MyClientProtocol,client] received HTTP headers in opening handshake : {u'connection': u'Upgrade', u'upgrade': u'websocket', u'sec-websocket-accept': u'7OKSOhPS0vnTejmpHUE+hJX8FLE=', u'x-powered-by': u'Ratchet/0.3'}
2014-04-16 10:30:19+0200 [MyClientProtocol,client] Server connected: 127.0.0.1:8080
2014-04-16 10:30:19+0200 [MyClientProtocol,client] WebSocket connection open.
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Frame to 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 1, mask = f1426ebe, length = 13, repeat_length = None, chopsize = None, sync = False, payload = Hello, world!
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Octets to 127.0.0.1:8080 : sync = False, octets = 818df1426ebeb92702d29e6e4ec99e3002dad0
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Frame to 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 2, mask = 0199b739, length = 4, repeat_length = None, chopsize = None, sync = False, payload = 00010304
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Octets to 127.0.0.1:8080 : sync = False, octets = 82840199b7390198b43d
2014-04-16 10:30:19+0200 [MyClientProtocol,client] RX Frame from 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 1, mask = -, length = 36, payload = [0,"534e3f9b52a62",1,"Ratchet\/0.3"]
2014-04-16 10:30:19+0200 [MyClientProtocol,client] Text message received: [0,"534e3f9b52a62",1,"Ratchet\/0.3"]
2014-04-16 10:30:19+0200 [MyClientProtocol,client] RX Frame from 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 1, mask = -, length = 13, payload = { Welcome ! }
2014-04-16 10:30:19+0200 [MyClientProtocol,client] Text message received: { Welcome ! }
2014-04-16 10:30:19+0200 [MyClientProtocol,client] RX Octets from 127.0.0.1:8080 : octets = 880203ef
2014-04-16 10:30:19+0200 [MyClientProtocol,client] RX Frame from 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 8, mask = -, length = 2, payload = 03ef
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Frame to 127.0.0.1:8080 : fin = True, rsv = 0, opcode = 8, mask = 48205ee1, length = 2, repeat_length = None, chopsize = None, sync = False, payload = 03e8
2014-04-16 10:30:19+0200 [MyClientProtocol,client] TX Octets to 127.0.0.1:8080 : sync = False, octets = 888248205ee14bc8
2014-04-16 10:30:19+0200 [MyClientProtocol,client] WebSocket connection closed: None
2014-04-16 10:30:19+0200 [MyClientProtocol,client] connection to 127.0.0.1:8080 lost
2014-04-16 10:30:19+0200 [MyClientProtocol,client] Stopping factory <autobahn.twisted.websocket.WebSocketClientFactory instance at 0x1efa830>

我们可以看到这一行:2014-04-16 10:30:19 + 0200 [MyClientProtocol,client] WebSocket连接已关闭:无 这并不让人放心......

0 个答案:

没有答案