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

Chat Program

Started by hunterguy4, 13 November 2013 - 11:24 AM
hunterguy4 #1
Posted 13 November 2013 - 12:24 PM
hi all!

iv been workin on a simple chat program that sends a message from the client to a server then to the receiving id. the ids are hard coded for now so there no issue there yet but my question in this. when i call a read it stops the recieve function so it can write the message to a send. while in the read function i have rednet closed because i thought it would tell the server that the port is closed and to wait, if the com has been sent any messages while the user was typing. once the read is finished i reopen the port and the message sends fine but any incoming messages are not received. the server got the message but once the port was reopen it did not send it and i cant figure out for the life of me why.


tldr: a chat program cant have 2 users typing messages at once.
MKlegoman357 #2
Posted 13 November 2013 - 12:28 PM
Give us your code. It's hard to tell you the problem if we don't see it.
hunterguy4 #3
Posted 13 November 2013 - 12:34 PM
while true do
rednet.open("right")
a, b, c = os.pullEvent()

if a == "rednet_message" then
print( c )
elseif a == "char" then
rednet.close("right")
s = read()
end

if s ~= nil then
rednet.open("right"0
rednet.send(2253, s)
s = nil
end
end
Edited on 13 November 2013 - 11:36 AM
Lyqyd #4
Posted 13 November 2013 - 12:37 PM
That's not how rednet works. The other computer will send regardless, and if you're not listening, it won't receive. You'll either need to create a custom version of the read function that handles rednet_message events as well, or use the parallel API.
hunterguy4 #5
Posted 13 November 2013 - 12:39 PM
parallel didnt seem to want to run both functions at once, it was just bouncing between the 2 which wasnt helpful in this case =/

is there no way of checking or confirming that a computers port is open from another com?
Edited on 13 November 2013 - 11:44 AM
Zudo #6
Posted 13 November 2013 - 12:46 PM
parallel didnt seem to want to run both functions at once, it was just bouncing between the 2 which wasnt helpful in this case =/

Don't use the parentheses for the functions inside the call to parallel.waitForAny or waitForAll()

For example:


-- Do this
parallel.waitForAny(function, anotherFunction)
-- not
parallel.waitForAny(function(), anotherFunction())
hunterguy4 #7
Posted 13 November 2013 - 12:49 PM
well il do some recoding and see if i cant get that to work, i dont think i used () inside parallel but it dosnt hurt to be sure
hunterguy4 #8
Posted 13 November 2013 - 12:58 PM
rednet.open("right")
while true do
r = read()
functions s()
a, b, c, = os.pullEvent(rednet.send)
b = r
rednet.send(1, b )
end

function r()
rednet.receive()
end

parallel.waitForAny(s, r)
end

so i tried this and it does the read then jumps to the right in that order so from what i can tell its not doing that at the same time
Edited on 13 November 2013 - 12:00 PM
TheOddByte #9
Posted 13 November 2013 - 01:24 PM
Well first of all.. Check out how to use os.pullEvent(), Second of all, use code tags [CODE-] print("Code here") [/CODE-] without the '-'
How are you planning on using the received message? Just write it to the screen? or storing it in a table and then writing it to the screen?
Anyways.. Here's an example

--# The table that are going to contain the messages
local messages = {}


--# The sending/reading function
local function send()
	local input = read()
	rednet.send(1, input)
end


--# The receiveing function
local function receive()
	local id, msg = rednet.receive()
	table.insert(messages, msg) -- Inserting the message in the messages table above
end


while true do
	term.clear()
	term.setCursorPos(1,1)
	for i = 1,#messages do
		print(table[i])
	end
	parallel.waitForAny(send, receive)
end
Edited on 14 November 2013 - 11:57 AM
hunterguy4 #10
Posted 13 November 2013 - 01:31 PM
ha sorry was in a rush i didnt mean to put rednet in the os.pullevent
XD
Edited on 13 November 2013 - 12:31 PM
Bomb Bloke #11
Posted 14 November 2013 - 12:49 AM
Hellkid, that "while" loop there refers to your table as "table" instead of "messages".

Hunterguy, your last example has a few issues, but one in particular that I want to point out is where you define "r" twice - once when you use it to store what "read()" returns, and once where you try to define it as the function "r()". This sort of coding will cause a clash, so take care not to give your "regular" variables the same titles as your functions.
Cozzimoto #12
Posted 14 November 2013 - 10:31 AM
if i were to make a chat program, id use os.pullEvent to read everything. have an empty string and when a key is pressed, insert that key into the the string to create my input so i dont miss and rednet messages. and with the return key pressed would send my string to the server



local input = ""
while true do
  local ev = { os.pullEvent() }
  if ev[1] == "char" then
    input = input .. ev[2]
  end

end

then you would have to capture the return key event and also the rednet messages to be displayed on screen to view messages sent from other players
TheOddByte #13
Posted 14 November 2013 - 12:57 PM
Hellkid, that "while" loop there refers to your table as "table" instead of "messages".

Hunterguy, your last example has a few issues, but one in particular that I want to point out is where you define "r" twice - once when you use it to store what "read()" returns, and once where you try to define it as the function "r()". This sort of coding will cause a clash, so take care not to give your "regular" variables the same titles as your functions.
*Facepalm* Thanks for noticing.. I like to make some silly mistakes sometimes :P/>