• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            python在tornado下websocket客戶端

            from tornado import httpclient
            from tornado import ioloop
            from tornado import websocket
            import uuid
            import binascii
            import struct
            import weakref
            DEFAULT_CONNECT_TIMEOUT = 60
            DEFAULT_REQUEST_TIMEOUT = 60
            def my_unpack(message1, length,ag_client):
                """解包"""
                message = message1[length:len(message1)]
                # 獲取cmd
                cmd, = struct.unpack('!i', message[0:4])
                # 獲取長度
                size, = struct.unpack('!i', message[4:8])
                if size!=len(message):
                    print('接收字節長度=%d' % (len(message)))
                    print('包頭中包長度=%d' % size)
                    print('cmd=====%d'%cmd)
                #包頭中的長度比包長度大,丟棄,不完整包
                if size >len(message):
                    print('壞包丟棄')
                    return
                # 包內容
                string, = struct.unpack('!{0}s'.format(size - 4), message[4:size])
                from protocol.deserialize import ag_parse
                ag_parse(cmd, string, ag_client)
                length += size
                if len(message) > size:
                    my_unpack(message1, length,ag_client)
            class WebSocketClient(object):
                """Base for web socket clients.
                """
                def __init__(self, *, connect_timeout = DEFAULT_CONNECT_TIMEOUT,
                                      request_timeout = DEFAULT_REQUEST_TIMEOUT,
                                      name = login_name,
                                      passwd = login_password,
                                      player = None):
                    self.connect_timeout = connect_timeout
                    self.request_timeout = request_timeout
                    #玩家登陸名
                    self.player_name = name
                    #玩家登陸passwd
                    self.player_passwd = passwd
                    #玩家數據
                    if player:
                        self.player = weakref.proxy(player)
                    else:
                        self.player = None
                def connect(self, url):
                    """Connect to the server.
                    :param str url: server URL.
                    """
                    request = httpclient.HTTPRequest(url=url,
                                                     connect_timeout=self.connect_timeout,
                                                     request_timeout=self.request_timeout)
                    ws_conn = websocket.WebSocketClientConnection(request,ioloop.IOLoop.current())
                    ws_conn.connect_future.add_done_callback(self._connect_callback)
                    ws_conn.on_message=self._on_message
                    ws_conn.on_connection_close=self._on_connection_close
                def send(self, data):
                    """Send message to the server
                    :param str data: message.
                    """
                    if not self._ws_connection:
                        raise RuntimeError('Web socket connection is closed.')
                    self._ws_connection.write_message(data,True)
                def close(self):
                    """Close connection.
                    """
                    if not self._ws_connection:
                        raise RuntimeError('Web socket connection is already closed.')
                    self._ws_connection.close()
                def _connect_callback(self, future):
                    if future.exception() is None:
                        self._ws_connection = future.result()
                        self._on_connection_success()
                    else:
                        self._on_connection_error(future.exception())
                def _on_message(self, msg):
                    """This is called when new message is available from the server.
                    :param str msg: server message.
                    """
                    pass
                def _on_connection_success(self):
                    """This is called on successful connection ot the server.
                    """
                    pass
                def _on_connection_close(self):
                    """This is called when server closed the connection.
                    """
                    pass
                def _on_connection_error(self, exception):
                    """This is called in case if connection to the server could
                    not established.
                    """
                    pass
                def bind(self,player):
                    pass
            class AppWebSocketClient(WebSocketClient):
                def _on_message(self, msg):
                    my_unpack(msg, 0, self)
                def _on_connection_success(self):
                    print('Connected!')
                    self.bind(self.player)
                    def intToBytes(value, length):
                        resu = []
                        for i in range(0, length):
                            resu.append(value >> (i * 8) & 0xff)
                        resu.reverse()
                        return resu
                    login_pack = packet_pb2.PK_CLIENT_LOGIN()
                    #用戶名
                     login_pack.loginname = self.player_name
                 
                    #密碼
                    curr_password = self.player_passwd
                    login_pack.devicetype = 1
                    result = []
                    temp = b''
                    for i in range(0, len(curr_password), 2):
                        result.append(binascii.unhexlify(curr_password[i:i + 2]))
                        temp += (binascii.unhexlify(curr_password[i:i + 2]))
                    login_pack.token = temp
                    # 序列化
                    serializeToString = login_pack.SerializeToString()
                    print(serializeToString, type(serializeToString))
                    args = (704, len(serializeToString), 0, 1, 1, serializeToString)
                    fmt = ">iiihh{0}s".format(len(serializeToString))
                    data = struct.pack(fmt, *args)
                    self.send(data)
                def _on_connection_close(self):
                    print('Connection closed!')
                    self._ws_connection.tcp_client.close()
                    print('關閉socket,重新連接')
                    # self.connect(ag_url)
                def _on_connection_error(self, exception):
                    print('Connection error: %s', exception)
                def bind(self,player):
                    """綁定ag的wss連接"""
                    if player:
                        if player.ag_wss:
                            player.ag_wss.close()
                        self.player.ag_wss = self
                        print('!!!!!!綁定到ag完成!!!!!!')
            def player_client(name,passwd,table_id,player):
                """玩家登陸創建客戶端"""
                if name:
                    if passwd:
                        if table_id > 0xFFFE:
                            if player:
                                client = AppWebSocketClient(connect_timeout=DEFAULT_CONNECT_TIMEOUT,
                                                            request_timeout=DEFAULT_REQUEST_TIMEOUT,
                                                            name=name,
                                                            passwd=passwd,
                                                            player= player)
                                client.connect(ag_url)

            posted on 2020-01-03 19:41 Benjamin 閱讀(1225) 評論(0)  編輯 收藏 引用 所屬分類: python

            久久99久国产麻精品66| 久久久久久久综合综合狠狠| 一本久久a久久精品vr综合| 亚洲综合熟女久久久30p| 国产亚洲精品自在久久| 91精品日韩人妻无码久久不卡| 色综合久久久久综合99| 久久精品亚洲中文字幕无码麻豆| 伊人丁香狠狠色综合久久| 久久久久国产精品麻豆AR影院| 亚洲综合日韩久久成人AV| 国产成人综合久久久久久| 久久99久国产麻精品66| 久久国产香蕉一区精品| 精品国产乱码久久久久久郑州公司| 国产成人久久久精品二区三区| 亚洲欧美成人综合久久久| 99热成人精品免费久久| 久久久久久毛片免费播放| 很黄很污的网站久久mimi色 | 久久青青草原国产精品免费| 久久精品夜色噜噜亚洲A∨| 精品久久久久久无码专区| 亚洲欧美日韩精品久久亚洲区 | 久久久久亚洲AV成人片| 久久这里的只有是精品23| 久久精品草草草| 久久久无码一区二区三区| 思思久久99热只有频精品66| 国产精品成人精品久久久| 久久人人爽人人爽人人片AV不 | 亚洲AV日韩AV永久无码久久| 久久综合九色综合欧美就去吻| 91精品国产91久久久久久| 精品久久久久久无码中文字幕一区| 久久久精品国产免大香伊| 伊人久久大香线蕉精品不卡| 一本大道久久香蕉成人网| 狠狠色综合网站久久久久久久高清| 久久久这里有精品| 久久亚洲精品成人无码网站|