DroidScript / Docs

DroidScript Documentation
Apache License 2.0
81 stars 29 forks source link

CreateNetClient Python example TCPAutoReceive #215

Open Al4He6 opened 2 months ago

Al4He6 commented 2 months ago

https://github.com/DroidScript/Docs/blob/63145db940b0ace9ef88855f7e3a4ec9c6c4576c/files/markup/en/app/CreateNetClient.js#L449

cannot access local variable sent

Al4He6 commented 1 month ago

Adding (3) global statements seems to fix the error messages

from native import app

def OnStart(): global net, msg, web, txt lay = app.CreateLayout("linear")

web = app.CreateWebView(1, 0.5, "ignoreerrors")
lay.AddChild(web)

txt = app.CreateTextEdit("", 1, 0.5, "ReadOnly,NoKeyboard")
txt.SetTextSize(12)
lay.AddChild(txt)

app.AddLayout(lay)

net = app.CreateNetClient("TCP,Raw")
net.SetOnConnect(net_OnConnect)
net.SetOnReceive(OnReceive)
net.AutoReceive("www.randomfunfacts.com", 80, "UTF-8")

sent = False msg = "" def net_OnConnect(connected):

if not connected:
    return app.ShowPopup("Failed to connect!")

global sent
global msg

if sent:
    return sent and msg != ""
else:
    sent = True

net.SendText("GET / HTTP/1.1\r\nHost:www.randomfunfacts.com\r\n\r\n", "UTF-8")

def OnReceive(s):

global msg

msg += s

if s.endswith("\r\n\r\n"):
    txt.SetText(msg)
    web.LoadHtml(msg)
    msg = ""