Guest User

chat

a guest
Jan 24th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.35 KB | None | 0 0
  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.     id, msg = rednet.receive("chatServ")
  9.     if msg == "PONG" then  
  10.       if id == serverId then
  11.         rednet.send(id,name)
  12.         return serverId, true
  13.       else
  14.         error("An unknown computer sent a message during handshake... aborting!")
  15.       end
  16.     else
  17.       error("Handshake interrupted for some reason... Aborting!")
  18.     end
  19.   else
  20.     error("You need a server to connect to, silly!")
  21.   end
  22. end
  23. function send(charlist,id)
  24.   msg = " "
  25.   for k,v in pairs(charlist) do
  26.     msg = msg..tostring(charlist[k])
  27.   end
  28.   rednet.send(id,msg)
  29. end
  30. function main(serverid)
  31.   event, idKey, msg = os.pullEvent()
  32.   if event == "rednet_message" then
  33.    
  34.   elseif event == "char" then
  35.     table.insert(charlist,m,idKey)
  36.     m=m+1
  37.   elseif event == "key_up" then
  38.     if idKey == 28 then
  39.       send(charlist,serverid)
  40.     end
  41.   end
  42. end
  43. --------
  44. m=0
  45. if textutils.serialize(peripheral.getNames()) ~= "{}" then
  46.   rednet.open("back")
  47. else
  48.   error("you need to put a wireless modem above this, dingbat")
  49. end
  50. serverId, status = init()
  51. if status then
  52.   while true do
  53.     main(serverId)  
  54.   end
  55. else
  56.   error("Unknown error occured! Aborting...")
  57. end
Advertisement
Add Comment
Please, Sign In to add comment