JustJurt

temp

Feb 25th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. local myURL = "ws://localhost:8080"
  2. local ws = http.websocket(myURL)
  3.  
  4. if ws == nil then
  5. print("Failed to connect to " .. myURL)
  6. return
  7. else
  8. print("Connected to " .. myURL)
  9. end
  10.  
  11. function Write_File(path, data)
  12. fs.delete(path)
  13.  
  14. local file = fs.open(path, "w")
  15.  
  16. if file then
  17. file.write(data) -- Write content to the file
  18. file.close() -- Close the file
  19. return true
  20. end
  21. return false
  22. end
  23.  
  24. function Chat()
  25. local event, url, message
  26. local input
  27.  
  28. while true do
  29.  
  30. term.setTextColor(colors.blue)
  31.  
  32. io.write("> ")
  33. input = read()
  34.  
  35. if input == "exit" then
  36. break
  37. end
  38.  
  39. ws.send(input)
  40.  
  41. local data
  42.  
  43. repeat
  44. event, url, Response = os.pullEvent("websocket_message")
  45. until url == myURL and textutils.unserialiseJSON(Response).type == "chat"
  46.  
  47. data = textutils.unserialize(Response)
  48.  
  49. term.setTextColor(colors.lightBlue)
  50.  
  51. textutils.slowPrint(data.text)
  52.  
  53. end
  54. end
  55.  
  56. function AI_Functions()
  57. local data
  58.  
  59. repeat
  60. event, url, Response = os.pullEvent("websocket_message")
  61. until url == myURL and textutils.unserialiseJSON(Response).type == "file"
  62.  
  63. data = textutils.unserialize(Response)
  64.  
  65. if Write_File(data.name, data.code) then
  66. term.setTextColor(colors.green)
  67. print("File \"".. data.name .."\" Created")
  68. else
  69. term.setTextColor(colors.red)
  70. print("Failed to create file \"".. data.name .."\"")
  71. end
  72.  
  73. end
  74.  
  75. parallel.waitForAny(Chat, AI_Functions)
  76.  
  77. ws.close()
  78.  
  79. term.setTextColor(colors.white)
  80. print("Disconnected from server.")
Advertisement
Add Comment
Please, Sign In to add comment