找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12398|回复: 0

[求助] 关于socket链接服务器会异常中断的问题

[复制链接]
发表于 2020-11-5 21:35:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
renpy中的socket访问服务器时,在电脑端正常,一打包到安卓就会出现访问异常中断。
初步判断是http握手后直接断开了链接,因为只链接上了一瞬间就断开链接了。
求大佬解答
以下是网络访问模块的代码……
[RenPy] 纯文本查看 复制代码
init python:
    import time
    from threading import Timer, Event, Thread
    import threading
    import socket
    import json
    import websocket
    class VaClass(object):
        def __init__(self):
            self.values = []
            self.hostname = ''
            self.ipaddr = ''
            self.ws = ''
            self.wst = ''
            self.times = 100
            self.readys = False
            self.room = ''
            self.uplist = ''
            self.downlist = []
            self.pointer = 0
            self.resave = False
            self.randlist = []
            self.randompointer = 0
            self.first = 0
            self.minecardlist = []

    va = VaClass()
    def Torand(rand, which):
        which.randlist = rand[1][:]
        which.randlist = which.randlist.split(' ')
        va.first = which.randlist.pop()
        va.room = which.randlist.pop()
    def on_message(ws, message):
        """接收信息"""
        if message[0]=="4" and message[1]=="2":
            mes = message[2:]
            mes = json.loads(mes)
            if mes[0] == "usecard":
                va.values = mes
                va.downlist = mes[1].split(' ')
                va.downlist.pop()
                renpy.show_screen('pinging')
            elif mes[0] == "reda":
                va.values = mes
                va.readys = True
                Torand(mes, va)
                renpy.show_screen('pinging')
            elif mes[0] == "cbs":
                va.values = mes
                renpy.show_screen('pysds')
                asd()
                renpy.show_screen('pinging')

    def on_error(ws, error):
        renpy.show_screen('error_S', '未知错误!!!')
    def on_close(ws):
        renpy.show_screen('error_S', '网络已断开!!!')
    def on_open(ws):
        ws.send("2probe")
    def Connect():
        va.hostname = socket.gethostname()
        va.ipaddr = socket.gethostbyname(va.hostname)
        websocket.enableTrace(True)
        va.ws = websocket.WebSocketApp( "ws://(这是我的端口号:))/socket.io/?EIO=3&transport=websocket", on_message=on_message, on_error=on_error, on_close=on_close)
        va.ws.on_open = on_open
        va.wst = threading.Thread(target=va.ws.run_forever)
        va.wst.daemon = True
        va.wst.start()
        renpy.show_screen('pinging')
    def Unconnect():
        wst._Thread__stop()
        renpy.hide_screen('pinging')
        ws.close()
    def getCardonline(which):
        va.minecardlist.append(which)
    def Upcards():
        va.ws.send('2')
        if va.resave:
            if len(va.downlist):
                va.ws.send('42["usecard","{0}"]'.format(va.uplist))
            else:
                va.ws.send('42["cardbase","{0}"]'.format(mines))
    def aichange(which):
        ai.do.append(which)
    def asd():
        if not len(persistent.enemy):
            cardlist = va.values[1].split(' ')
            cardlist.pop()
            for i in cardlist:
                persistent.enemy.append(persistent.card[int(i)].copy())

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号|苏公网安备 32092302000068号 )

GMT+8, 2024-12-22 14:30 , Processed in 0.135812 second(s), 28 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表