[Screeninvader] [Janosh]built-in websocket support

Amir Hassan amir at viel-zu.org
Wed Mar 18 18:26:08 CET 2015


On Wed, 18 Mar 2015 14:33:43 +0100, Amir Hassan <amir at viel-zu.org> wrote:

> Janosh now supports websockets :D.
>
> example:
>
a more complete example using transactions:

<code>
#!/usr/local/bin/janosh -f

-- runs when /val is changed
function sub(key,value)
   -- update all clients with whole database. do it in a transaction
   Janosh:open()
   Janosh:wsBroadcast(janosh_get({"/."}));
   Janosh:close()
end

-- runs when a websocket message is received
function echo(handle, message)
  print("echo");
  -- echo message back
  Janosh:wsSend(handle, message)
end

-- init the db and /val
Janosh:truncate()
Janosh:set("/val", "1")

-- open websocket
Janosh:wsOpen(9998)
-- install receive callback
Janosh:wsOnReceive(echo);
-- install subscription callback
Janosh:subscribe("/val", sub)

print("loop")
while true do
   -- set, remove and set /val. do it in a transaction so that everybody  
else sees /val as 1
   Janosh:open()
   Janosh:set("/val", "0")
   Janosh:remove("/val")
   Janosh:sleep(1000)
   Janosh:set("/val","1")
   Janosh:close()
end
</code>



More information about the Screeninvader mailing list