@@ -39,7 +39,7 @@ local pending_close = {}
3939
4040cosock .spawn (function ()
4141 while true do
42- for _ , unique_key in ipairs (pending_close ) do -- close any sockets pending close before selecting/receiving on them
42+ for _ , unique_key in ipairs (pending_close or {} ) do -- close any sockets pending close before selecting/receiving on them
4343 local wss = websockets [unique_key ]
4444 if wss ~= nil then
4545 log .trace (string.format (" Closing websocket for player %s" , unique_key ))
@@ -60,7 +60,7 @@ cosock.spawn(function()
6060 pending_close = {}
6161
6262 local socks = { control_rx }
63- for _ , wss in pairs (websockets ) do
63+ for _ , wss in pairs (websockets or {} ) do
6464 table.insert (socks , wss )
6565 end
6666 local receivers , _ , err = socket .select (socks , nil , 10 )
@@ -71,7 +71,7 @@ cosock.spawn(function()
7171 log .error (" Error in Websocket Router event loop: " .. err )
7272 end
7373 else
74- for _ , recv in ipairs (receivers ) do
74+ for _ , recv in ipairs (receivers or {} ) do
7575 if recv .link and recv .link .queue and # recv .link .queue == 0 then -- workaround a bug in receiving
7676 log .warn (" attempting to receive on empty channel" )
7777 goto continue
@@ -93,7 +93,7 @@ cosock.spawn(function()
9393 elseif err == " closed" and recv .id then -- closed websocket
9494 log .trace (string.format (" Websocket %s closed" , tostring (recv .id )))
9595 local still_open_sockets = {}
96- for unique_key , wss in pairs (websockets ) do
96+ for unique_key , wss in pairs (websockets or {} ) do
9797 if wss .id ~= recv .id then
9898 still_open_sockets [unique_key ] = wss
9999 end
@@ -207,7 +207,7 @@ local function _make_websocket(url_table, api_key)
207207
208208 local headers = SonosApi .make_headers (api_key )
209209 local config = LustreConfig .default ():protocol (" v1.api.smartspeaker.audio" )
210- for k , v in pairs (headers ) do
210+ for k , v in pairs (headers or {} ) do
211211 config = config :header (k , v )
212212 end
213213
313313function SonosWebSocketRouter .cleanup_unused_sockets (driver )
314314 log .trace (" Begin cleanup of unused websockets" )
315315 local should_keep = {}
316- for unique_key , _ in pairs (websockets ) do
316+ for unique_key , _ in pairs (websockets or {} ) do
317317 local household_id , player_id = unique_key :match (" (.*)/(.*)" )
318318 local is_joined = driver .sonos :get_device_id_for_player (household_id , player_id ) ~= nil
319319 log .debug (string.format (" Is Player %s joined? %s" , player_id , is_joined ))
@@ -322,7 +322,7 @@ function SonosWebSocketRouter.cleanup_unused_sockets(driver)
322322
323323 local known_devices = driver :get_devices ()
324324
325- for _ , device in ipairs (known_devices ) do
325+ for _ , device in ipairs (known_devices or {} ) do
326326 local household_id , coordinator_id = driver .sonos :get_coordinator_for_device (device )
327327 local coordinator_unique_key , bad_key_part =
328328 utils .sonos_unique_key (household_id , coordinator_id )
@@ -340,7 +340,7 @@ function SonosWebSocketRouter.cleanup_unused_sockets(driver)
340340 end
341341 end
342342
343- for unique_key , keep in pairs (should_keep ) do
343+ for unique_key , keep in pairs (should_keep or {} ) do
344344 if not keep then
345345 SonosWebSocketRouter .close_socket_for_player (unique_key )
346346 end
0 commit comments