Qsys server script send data !!!

Discussion space for anything related to Q-Sys.
omega_elite
Posts: 13
Joined: June 13th, 2017, 9:14 am

Qsys server script send data !!!

Postby omega_elite » February 14th, 2018, 9:30 am

Hi

Please could someone please tell me how I send data with the script listed below....Many Thanks

server = TcpSocketServer.New()

function SocketHandler(NewSocketInstance, event)
print( "TCP Socket Event: "..event )
if event == TcpSocket.Events.Data then
print( NewSocketInstance, NewSocketInstance:Read(NewSocketInstance.BufferLength) )
end
end

server.EventHandler = function(SocketInstance)
SocketInstance.ReadTimeout = 10
print( "Got connect", SocketInstance )
SocketInstance.EventHandler = SocketHandler
end

server:Listen(1720) -- This listen port is opened on all network interfaces

dgriffin
Posts: 14
Joined: June 16th, 2016, 7:55 am

Re: Qsys server script send data !!!

Postby dgriffin » February 16th, 2018, 11:04 am

You can use NewSocketInstance:Write()

Code: Select all

server = TcpSocketServer.New()

function SocketHandler(NewSocketInstance, event)
   print( "TCP Socket Event: "..event )
   if event == TcpSocket.Events.Data then
      print( NewSocketInstance, NewSocketInstance:Read(NewSocketInstance.BufferLength) )
      NewSocketInstance:Write( "Hello World" )
   end
end

server.EventHandler = function(SocketInstance)
   SocketInstance.ReadTimeout = 10
   print( "Got connect", SocketInstance )
   SocketInstance.EventHandler = SocketHandler
end

server:Listen(1720) -- This listen port is opened on all network interfaces

omega_elite
Posts: 13
Joined: June 13th, 2017, 9:14 am

Re: Qsys server script send data !!!

Postby omega_elite » February 19th, 2018, 9:50 am

Thanks for reply, I am now trying to send the value of input 1 to the connected client but am getting an error "11: attempt to concatenate a nil value (local 'event')" and not sure what I am doing wrong !!!! ????


server = TcpSocketServer.New()

Controls.Inputs[1].EventHandler = function()
sendtosock = Controls.Inputs[1].Value
SocketHandler(sendtosock) --Send to Client
end

function SocketHandler(NewSocketInstance, event)
print( "TCP Socket Event: "..event )
if event == TcpSocket.Events.Data then
print( NewSocketInstance, NewSocketInstance:Read(NewSocketInstance.BufferLength) )
NewSocketInstance:Write( "Hello World" )
else
NewSocketInstance:Write( sendtosock )
end
end

server.EventHandler = function(SocketInstance)
SocketInstance.ReadTimeout = 10
print( "Got connect", SocketInstance )
SocketInstance.EventHandler = SocketHandler
end

server:Listen(49563)

dgriffin
Posts: 14
Joined: June 16th, 2016, 7:55 am

Re: Qsys server script send data !!!

Postby dgriffin » February 22nd, 2018, 5:35 am

With the socket server you can only reply to messages, not initiate them.

If you need to initiate messages, if possible use a TCP socket client: q-syshelp.qschome.com

If you have to use a TCP socket server you will have to store the value you want to send and wait till the client sends the server a message, then you can respond with your value.

addisonb
Posts: 6
Joined: June 6th, 2017, 5:52 am

Re: Qsys server script send data !!!

Postby addisonb » March 24th, 2018, 4:13 am

That's not entirely true,
I wrote a script a long time back that when you get a new connection you save the NewSocketInstance into a global array of all the connected instances then when you want to initiate data (rather than reply) you can iterate over the entire array of clients or send data to specific clients with

connectedClients[2]:Write("Some String")

You need a way of removing the socketInstances from the array when the client disconnects or your array will just keep growing and growing.

dgriffin
Posts: 14
Joined: June 16th, 2016, 7:55 am

Re: Qsys server script send data !!!

Postby dgriffin » March 27th, 2018, 9:21 am

Thanks for the info addisonb! This is quite useful.


Return to “Q-Sys Discussion”

Who is online

Users browsing this forum: No registered users and 0 guests

cron