CubeyTheCube/scratchclient

WebSocketConnectionClosedException

goodmaster1233 opened this issue · 7 comments

Whenever I leave my python code running for a long time I get this error.

websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

give the the code

I can check

here is my code

`from ast import While
from scratchclient import ScratchSession
import sqlite3
import time

#connect to database
conn = sqlite3.connect('scratch-data.db')

#create cursor
c = conn.cursor()

#temp add
#c.execute("INSERT INTO scratch VALUES ('28171032233637383939', '987')")

#c.execute("DELETE from scratch")
#c.execute("DELETE from scratch WHERE rowid = 2")

conn.commit()

#scratch stuff
session = ScratchSession("USR", "PASS")
connection = session.create_cloud_connection(694789946)

def deleteRecord(id):
new_con = sqlite3.connect("scratch-data.db")
new_cur = new_con.cursor()

new_cur.execute("DELETE from scratch WHERE rowid = (?)", id)

new_con.commit()
new_cur.close()
new_con.close()

while True:

generalVar = connection.get_cloud_variable("general")

if generalVar:

    if generalVar == '0': #read data
        new_con = sqlite3.connect("scratch-data.db")
        new_cur = new_con.cursor()

        connection.set_cloud_variable("general", 200)
        dataIdentifier = connection.get_cloud_variable("dataTransmission")
        if dataIdentifier:

            new_cur.execute("SELECT rowid, * FROM scratch")

            #fetch all data
            fetchedData = new_cur.fetchall()
            print(fetchedData)

            for item in fetchedData:
                #print("----------")
                #print(str(item[0]))
                #print(int(dataIdentifier))
                #print("----------")

                if str(item[0]) == str(dataIdentifier):
                    print("Found Data!")
                    connection.set_cloud_variable("dataTransmission", int(item[1]))

            new_con.commit()
            new_con.close()
    if generalVar == '1': #write data
        connection.set_cloud_variable("general", 200)

        dataIdentifier = connection.get_cloud_variable("dataTransmission")
        if dataIdentifier:

            connection.set_cloud_variable("dataTransmission", 0)

            time.sleep(1)
            dataToSave = connection.get_cloud_variable("dataTransmission")

            c.execute("INSERT INTO scratch VALUES (?,?)", (dataIdentifier, dataToSave))
            conn.commit()

            print("Successfully wrote data to database!")
            connection.set_cloud_variable("dataTransmission", 200)

    if generalVar == '2':
        connection.set_cloud_variable("general", 200)

        dataIdentifier = connection.get_cloud_variable("dataTransmission")
        if dataIdentifier:
            
            deleteRecord(str(dataIdentifier))

            connection.set_cloud_variable("dataTransmission", 200)
            print("Deleted Data!")

#commit our command
conn.commit()

#close our connection
conn.close()

#ending code
print("Code Execution Finished.")

`

the code is fine . the connection timeing out

what do you mean? im new to python websockets

Since much of the cloud-related code is different now, I'm not sure this issue is relevant anymore. Create a new issue if it is still causing problems.

Whenever I leave my python code running for a long time I get this error.

websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

This is not an error with the library or your code. Websockets are not typically meant to be kept alive for a while unless data is being sent from client to server and vice-versa