23.03.2018, 14:25
Hi,
Is there an example from this? Because that's what I am trying. thx Roger
Is there an example from this? Because that's what I am trying. thx Roger
Code:
if not ready then
socket = require("user.websocket")
usocket = require("socket")
copas = require("copas")
ready=true
apps = {}
--helper functions to round and increment values
function increment(n)
n = n + 1
return n
end
function parse(data)
alert('parsing: %s', data)
local response = string.split(data,' = ')
end
function sendCommand(command)
skt:send(command)
sleep(1)
end
function init()
end
function fromKNX(command)
local telegram = string.split(command,',')
log(command)
sendCommand(command)
end
local server = usocket.udp()
server:setsockname("127.0.0.1",53450)
function handler(skts)
skts = copas.wrap(skts)
alert("UDP connection handler")
while true do
local s, err
alert("UDP receiving..")
s, erro = skts:receive(2048)
if not s then
alert("Receive error: %s", erro)
break
end
alert("Received data, bytes: %s",s)
fromKNX(s)
end
end
copas.addserver(server, handler, 1)
end
if not skt then
url= 'ws://demos.kaazing.com/echo'
skt,err = socket.client('copas',5)
skt:connect(url,80)
skt:send('hello')
-- when theres no error connect ok, initialize
if(not err) then
-- add receive thread
copas.addthread(function()
while true do
local resp,err = copas.receive(skt)
-- if theres no connection start a new connection
if not resp then
alert("[tcp-client] Receive error: %s", err)
copas.removeserver(skt)
skt = nil
break
end
local fd,prtd = pcall(parse,resp)
if(fd==false)then
alert("Error with parsemsg %s ",prtd)
end
end
end)
if skt then
alert('[tcp-client] connection ok')
init()
-- error while connecting,
else
if warningfailed then alert('[tcp-client] connection failed (conn): %s', err) end
return
end
else
alert('[tcp-client] error connecting %s',err)
return
end
end
copas.loop()