kd8lvt

chat test

Jan 24th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. charlist = {}
  2. function init()
  3.   print("Name?")
  4.   name = io.read()
  5.   serverId = rednet.lookup("chatServ","chatServer")
  6.   if serverId ~= nil then
  7.     rednet.send(serverId,"PING","chatServ")
  8.     print("Getting PONG")
  9.     id, msg = rednet.receive("chatServ")
  10.     if msg == "PONG" then
  11.       print("Got PONG from  id "..id.." server id is "..serverId..".")
  12.       if id == serverId then
  13.         rednet.send(id,name,"chatServ")
  14.         return serverId, true
  15.       else
  16.         error("An unknown computer sent a message during handshake... aborting!")
  17.       end
  18.     else
  19.       error("Handshake interrupted for some reason... Aborting!")
  20.     end
  21.   else
  22.     error("You need a server to connect to, silly!")
  23.   end
  24. end
  25. function send(charlist,id)
  26.   msg = " "
  27.   for k,v in pairs(charlist) do
  28.     msg = msg..tostring(charlist[k])
  29.   end
  30.   rednet.send(id,msg,"chatServ")
  31. end
  32. function main(serverid)
  33.   event, idKey, msg = os.pullEvent()
  34.   if event == "rednet_message" then
  35.    
  36.   elseif event == "char" then
  37.     term.write(idKey)
  38.     table.insert(charlist,m,idKey)
  39.     m=m+1
  40.   elseif event == "key_up" then
  41.     if idKey == 28 then
  42.       x, y = term.getCursorPos()
  43.       newY = y+1
  44.       term.setCursorPos(1,newY)
  45.       send(charlist,serverid)
  46.       charlist = {}
  47.       m=0
  48.     end
  49.   end
  50. end
  51. --------
  52. m=0
  53. if textutils.serialize(peripheral.getNames()) ~= "{}" then
  54.   rednet.open("back")
  55. else
  56.   error("you need to put a wireless modem above this, dingbat")
  57. end
  58. serverId, status = init()
  59. if status then
  60.   while true do
  61.     main(serverId)  
  62.   end
  63. else
  64.   error("Unknown error occured! Aborting...")
  65. end
Add Comment
Please, Sign In to add comment