Hi,

I am trying to implement a client which tries to reconnect to server if, 
for whatever reason, is not available sometimes. My application is about 
piping the stdout of arecord to a TCP socket on a server. The application 
works fine, but is not bullet-proof if the server is not ready or is 
restarted. I want the client to reconnect to the server, without the need 
of a process monitor on the client side.

I found a similar nodejs example: https://gist.github.com/sio2boss/6334089

local spawn = require('childprocess').spawn
local net = require('net')

local args = {
  "-M",
  "-D", "hw:0,1",
  "-t", "raw",
  "-c", "2",
  "-r", "48000",
  "-f", "S16_LE",
}

local sock
sock = net.Socket:new()

local function doConnect()
  print("Connecting...")

  sock:connect(1234, '192.168.1.157')
end

local function onConnect()
  print("Connected")
  local arecord

  arecord = spawn("arecord", args)

  arecord:on("error", function(err)
    print("Spawn error:", err)
  end)
  arecord:on("exit", function(exit_status)
    print("Exit with " .. exit_status)
  end)

  arecord.stdout:pipe(sock)
  arecord.stderr:pipe(sock)
end

local function onError(err)
  print("Socket error:", err)
  if err == 'ECONNREFUSED' then
    sock:setTimeout(1000, doConnect)
  elseif err == 'EPIPE' then        
    sock:setTimeout(1000, doConnect)   
  end                               
end  
   
sock:on("connect", onConnect)
sock:on("error", onError)    
                         
doConnect()


The client tries one time to reconnect, but not more:

# luvit client.lua 
Connecting...
Socket error: ECONNREFUSED
Connecting...
# 

Any idea what is wrong with my code?

Best regards,
Jörg Krause

-- 
You received this message because you are subscribed to the Google Groups 
"luvit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to