Advertisement
meuced

Tchat serveur v0.5

Nov 24th, 2013
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local liste_client = {} -- ici, chaque élément sera un couple d'infos : ID client et pseudo
  2.  
  3. local idChannelLogin = 130
  4. local idChannelAdmin = 131
  5. local idChannelAdminRep = 13101
  6. local idChannelMsgRec = 132
  7. local idChannelMsgEnv = 133
  8.  
  9. local keygen = "azerty"
  10.  
  11. function attente_client()
  12.     -- attente de connexion/déconnexion des clients
  13.     print("lancement attente_client OK.")
  14.     while true do
  15.         modem.open(idChannelLogin)
  16.         local event, modemSide, senderChannel, replyChannel, text, senderDistance = os.pullEvent("modem_message")
  17.         if senderChannel == idChannelLogin then
  18.             local msg = textutils.unserialize(text)
  19.             if msg.key == keygen then
  20.                 if msg.action == "login" then
  21.                     liste_client[msg.id] = msg.nickname
  22.                     print("Nouveau client : "..msg.id.." "..liste_client[msg.id])
  23.                 elseif msg.action == "logout" then
  24.                     table.remove(liste_client, msg.id)
  25.                     print("logout du client : "..msg.id)
  26.                 end
  27.             end
  28.         end
  29.         modem.close(idChannelLogin)
  30.     end
  31. end
  32.  
  33. function administration()
  34.     -- envoi régulier d'infos aux clients
  35.     print("lancement admin OK.")
  36.     modem.open(idChannelAdmin)
  37.     while true do
  38.        
  39.         local event, modemSide, senderChannel, replyChannel, text, senderDistance = os.pullEvent("modem_message")
  40.         if text == "majliste" then
  41.             local msg = textutils.serialize(liste_client)
  42.             modem.transmit(idChannelAdminRep,idChannelAdmin,msg)
  43.             print("maj liste envoyee")
  44.         end
  45.        
  46.     end
  47.     modem.close(idChannelAdmin)
  48. end
  49.  
  50. function echange_msg()
  51.     -- reçoit et distribue les messages
  52.     print("lancement echange_msg OK.")
  53.     modem.open(idChannelMsgEnv)
  54.     while true do
  55.         --sleep(5)
  56.         local event, modemSide, senderChannel, replyChannel, text, senderDistance = os.pullEvent("modem_message")
  57.         if senderChannel == idChannelMsgEnv then
  58.             modem.transmit(idChannelMsgRec, idChannelMsgRec, text)
  59.         end
  60.     end
  61.     modem.close(idChannelMsgEnv)
  62. end
  63.  
  64. modem = peripheral.wrap("back")
  65.  
  66. term.clear()
  67. term.setCursorPos(1,1)
  68. print("ID du serveur : "..os.getComputerID())
  69.  
  70. parallel.waitForAny(attente_client, administration, echange_msg)
  71.  
  72. print("Fin du programme")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement