This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KRHN's profile picture

ParallelAPI In Chat [Answered]

Started by KRHN, 03 February 2015 - 07:41 PM
KRHN #1
Posted 03 February 2015 - 08:41 PM
Hello guys, I am DunkFest. I have one problem in ComputerCraft, I developed Chat System works with HTTP API. I want send and receive function works together but I got different bugs in print. I am waiting your comments.


  --Made by Schaffer79
io.write("Your Name: ")
ad = io.read()
sleep(1)
term.clear()
function receive()
  x, y = term.getCursorPos()
  clearLines()
  term.setCursorPos(1, 1)
  io.write(http.get("http://www.dragcavetooltr.esy.es/receive.txt").readAll())
  term.setCursorPos(x, y)
  send()
end
function send()
  term.setCursorPos(1, 17)
  term.clearLine()
  term.setCursorPos(1, 17)
  io.write("> ")
  term.setCursorPos(3, 17)
  msg = io.read()
  msg = msg:gsub(" ", "+")
  http.get("http://dragcavetooltr.esy.es/send.php?msg="..ad..">+"..msg)
end
function clearLines()
  for i = 1, 15 do
	term.setCursorPos(1, i)
	term.clearLine()
  end
end

while true do
  receive()
  --parallel.waitForAny(send, receive)
end
Edited on 04 February 2015 - 12:53 PM
Bomb Bloke #2
Posted 03 February 2015 - 10:41 PM
Instead of:

io.write(http.get("http://www.dragcavetooltr.esy.es/receive.txt").readAll())

Use:

local webHandle = http.get("http://www.dragcavetooltr.esy.es/receive.txt")
io.write(webHandle.readAll())
webHandle.close()

And rather than:

  msg = io.read()
  msg = msg:gsub(" ", "+")

Use:

  msg = textutils.urlEncode( read() )

You should also use textutils.urlEncode() on your "ad" variable.
_removed #3
Posted 03 February 2015 - 10:44 PM

  --Made by Schaffer79
term.clear()
term.setCursorPos(1, 1)
term.write("Your Name:")
ad = read()
sleep(1)
term.clear()
function receive()
  x, y = term.getCursorPos()
  clearLines()
  term.setCursorPos(1, 1)
  receive = http.get("http://www.dragcavetooltr.esy.es/receive.txt")
  print(receive.readAll())
  term.setCursorPos(x, y)
  send()
end

function send()
  term.setCursorPos(1, 17)
  term.clearLine()
  term.setCursorPos(1, 17)
  term.write(">")
  msg = read()
  msg = msg:gsub(" ", "+")
  send = http.post("http://dragcavetooltr.esy.es/send.php?msg="..textutils.urlEncode(ad)..">+"..textutils.urlEncode(msg))
end

function clearLines()
  for i = 1, 15 do
	term.setCursorPos(1, i)
	term.clearLine()
  end
end

while true do
  parallel.waitForAny(send, receive)
end
Dog #4
Posted 03 February 2015 - 10:48 PM
smigger22, in your example you'll need to change either the name of your send function or the name of your send variable - you are using the same variable name for both and that will cause problems. The same goes for your receive function and variable.
Edited on 03 February 2015 - 09:53 PM
KRHN #5
Posted 03 February 2015 - 10:53 PM

  --Made by Schaffer79
term.clear()
term.setCursorPos(1, 1)
term.write("Your Name:")
ad = read()
sleep(1)
term.clear()
function receive()
  x, y = term.getCursorPos()
  clearLines()
  term.setCursorPos(1, 1)
  receive = http.get("http://www.dragcavetooltr.esy.es/receive.txt")
  print(receive.readAll())
  term.setCursorPos(x, y)
  send()
end

function send()
  term.setCursorPos(1, 17)
  term.clearLine()
  term.setCursorPos(1, 17)
  term.write(">")
  msg = read()
  msg = msg:gsub(" ", "+")
  send = http.post("http://dragcavetooltr.esy.es/send.php?msg="..textutils.urlEncode(ad)..">+"..textutils.urlEncode(msg))
end

function clearLines()
  for i = 1, 15 do
	term.setCursorPos(1, i)
	term.clearLine()
  end
end

while true do
  parallel.waitForAny(send, receive)
end

My problem isnt http. I cant work functions together, with my way works successfull I will use http.post, thanks but not helps :(/>.
Bomb Bloke #6
Posted 03 February 2015 - 10:56 PM
Ditto for receive, and don't forget to close those web handles!
KRHN #7
Posted 03 February 2015 - 11:12 PM
Ditto for receive, and don't forget to close those web handles!
Thanks for your advice but I need work functions together. If you tried than its so buggy, not works together.

Hey guys! I havent get error, thanks for advices but my problem is different. When I try use ParallelAPI receive function and send function goes to bug. I need fix parallelapi bug.
KRHN #8
Posted 04 February 2015 - 10:33 AM
Bump.
Bomb Bloke #9
Posted 04 February 2015 - 11:45 AM
In regards to applying parallel to what you've got, the two functions must be rigged so that they don't end - they must repeat themselves instead.

Eg, something like:

Spoiler
term.write("Your Name: ")
local ad = textutils.urlEncode(read())
sleep(1)
term.clear()

local function receive()
	local lastText = {}  -- New, empty table.
	
	while true do
		local newText, webHandle = {}, http.get("http://www.dragcavetooltr.esy.es/receive.txt")
		for line in webHandle.readLine do table.insert(newText, line) end
		webHandle.close()

		if #newText > #lastText then  -- If number of entries in newText is more than number of entries in lastText, then...
			local x, y = term.getCursorPos()

			local linesWritten = 1
			for i = math.min(1, #newText - 14), math.min(15, #newText) do  -- math.min returns the lowest value, eg math.min(23, 12) is 12
				term.setCursorPos(1, linesWritten)
				term.clearLine()
				term.write(newText[i])
				linesWritten = linesWritten + 1
			end

			term.setCursorPos(x, y)
			lastText = newText
		end
		
		sleep(5)
	end
end

local function send()
	while true do
		term.setCursorPos(1, 17)
		term.clearLine()
		term.write("> ")
		local msg = textutils.urlEncode(read())
		local webHandle = http.get("http://dragcavetooltr.esy.es/send.php?msg="..ad..">+"..msg)
		webHandle.close()
	end
end

parallel.waitForAll(send, receive)
KRHN #10
Posted 04 February 2015 - 01:52 PM
In regards to applying parallel to what you've got, the two functions must be rigged so that they don't end - they must repeat themselves instead.

Eg, something like:

Spoiler
term.write("Your Name: ")
local ad = textutils.urlEncode(read())
sleep(1)
term.clear()

local function receive()
	local lastText = {}  -- New, empty table.
	
	while true do
		local newText, webHandle = {}, http.get("http://www.dragcavetooltr.esy.es/receive.txt")
		for line in webHandle.readLine do table.insert(newText, line) end
		webHandle.close()

		if #newText > #lastText then  -- If number of entries in newText is more than number of entries in lastText, then...
			local x, y = term.getCursorPos()

			local linesWritten = 1
			for i = math.min(1, #newText - 14), math.min(15, #newText) do  -- math.min returns the lowest value, eg math.min(23, 12) is 12
				term.setCursorPos(1, linesWritten)
				term.clearLine()
				term.write(newText[i])
				linesWritten = linesWritten + 1
			end

			term.setCursorPos(x, y)
			lastText = newText
		end
		
		sleep(5)
	end
end

local function send()
	while true do
		term.setCursorPos(1, 17)
		term.clearLine()
		term.write("> ")
		local msg = textutils.urlEncode(read())
		local webHandle = http.get("http://dragcavetooltr.esy.es/send.php?msg="..ad..">+"..msg)
		webHandle.close()
	end
end

parallel.waitForAll(send, receive)
Thanks Bomb Bloke! I edited your codes and I did successfully HTTP Chat!