Advertisement
Mojokojo69

chat server

Aug 29th, 2023 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. -- Server code
  2. local modem = peripheral.wrap("back") -- Assuming the modem is on the back of the computer
  3.  
  4. -- Unique identifier for this computer
  5. local myID = os.getComputerID()
  6.  
  7. -- Function to broadcast a message to all connected clients except the sender
  8. local function broadcast(channel, message, sender)
  9. modem.transmit(channel, channel, {message = message, sender = myID})
  10. end
  11.  
  12. -- Ask the user to configure the channel
  13. print("Enter the channel number to listen on:")
  14. local channel = tonumber(read())
  15.  
  16. -- Open the channel
  17. modem.open(channel)
  18. print("Listening on channel " .. channel)
  19.  
  20. -- Main loop
  21. while true do
  22. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  23.  
  24. if senderChannel == channel then
  25. if message.sender ~= myID or message.sender ~= message.sender then
  26. print("Received: " .. message.message)
  27. broadcast(channel, message.message, message.sender)
  28. else
  29. print("Ignoring message from self.")
  30. end
  31. end
  32. end
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement