Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local name = ""
- local channel = ""
- local channelPassword = ""
- local side = require("sides").left
- --*************Imports*********************
- local event = require("event")
- local net = require("internet")
- local os = require("os")
- local component = require("component")
- --***************************TCPClient***************************************
- local tcp = {}
- tcp.__index = tcp
- function tcp:connect(name)
- local t = {}
- setmetatable(t, tcp)
- t.con = net.open("clockville.eu", 23456)
- if t.con then
- t:sendString("login " .. name)
- return t
- else
- return nil
- end
- end
- function tcp:isConnected()
- if self.con then
- return true
- else
- return false
- end
- end
- function tcp:sendString(s)
- self.con:write(s .. "\n")
- self.con:flush()
- end
- function tcp:receiveString()
- local s = self.con:read()
- self.con:flush()
- return s
- end
- --****************************CommandExecutor********************************
- function tcp:execute(command, callback)
- local split_ = command:gmatch("%S+")
- local split = {}
- for word in split_ do table.insert(split, word) end
- local channel = split[2]
- local from = split[3]
- local label = ""
- local args = {}
- for i=4,#split do
- args[i-3] = split[i]
- label = label..split[i]
- if i<#split then
- label = label.." "
- end
- end
- callback(channel, from, label, args)
- end
- --***************************Program start**********************************
- client = tcp:connect(name)
- if client:isConnected() then
- client:sendString("createChannel "..channel.." "..channelPassword)
- client:sendString("joinChannel "..channel.." "..channelPassword)
- client:sendString("listenToChannel "..channel)
- while true do
- rcv = client:receiveString()
- tcp:execute(rcv, function(channel, from, label, args)
- if label=="on" then component.redstone.setOutput(side, 15) end
- if label=="off" then component.redstone.setOutput(side, 0) end
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement