[Screeninvader] [Janosh]built-in websocket support

Amir Hassan amir at viel-zu.org
Wed Mar 18 19:40:53 CET 2015


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

> 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:
>
and now the exact same program using shorthand mode and the new  
transaction function:

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

-- enables short global function names for the janosh api
Janosh:shorthand()

Jset("/val","2")
-- runs when /val is changed
function sub(key,value)
   -- update all clients with whole database. do it in a transaction
   Jtransaction(function()
     JwsBroadcast(janosh_get({"/."}));
   end)
end

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

-- init the db and /val
Jtruncate()
Jset("/val", "1")

-- open websocket
JwsOpen(9998)
-- install receive callback
JwsOnReceive(echo);
-- install subscription callback
Jsubscribe("/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
   Jtransaction(function()
     Jset("/val", "0")
     Jremove("/val")
     Jsleep(1000)
     Jset("/val","1")
     Jclose()
   end)
end
</code>



More information about the Screeninvader mailing list