I have a basic sonoff 1.5.5 that I would like to use to reboot a server if needed. I have set it up and can see it fine on the Android App. But after a short time (say 15 minutes) it is no longer available. I can see in the DHCP list the IP it is given and can ping it, but it appears 'stuck' in the double flash state.
Last night I change the DTIM beacon interval, which caused the wifi in the router to reset - the sonoff briefly came back..
Now, this cannot be any out bound filters, as it would fail in the start up case. Only thing I can think of is the websocket you are using is left in a half closed state or idle's out. Are you using ping/pong with the web socket? The 'tell' is that restarting the Sonoff gets the connection back and it can be pinged, so its a TCP/IP app level issue - hence how a long lived web socket connection is being handled.
Also this morning I found that the sonoff was on the double flash state and had managed to turn off the power to the server... I pressed the button to manually turn on the power on, but not 2 minutes later it was off again... I did try your loop timer, but have disabled that. I had to power cycle the sonoff and it came back.. So something at the app level is getting stuck.
I would suggest you look at the websocket code and see if in the event of connection failure you are rebuilding the websocket connection completely from scratch and not retry to reuse an existing websocket. A simple retry counter and fall back would do it. Also check if ping/pong is active.