My first attempt: to start socket.io listening ONLY after retrieving the
initial values from DB (I presume your led data can be retrieved via
ledxx.get(),
If this code works, then you could refactor a bit, to a clear
initialization, now all the code is inside a "big" callback. But, baby
steps ;-)
Server.js
if(start==0)
{
db.get('4662745328718336f506f9d2d3010c0c', function(err, val) {
console.log('Switches =', val);
led10.set(val.sw1);
led11.set(val.sw2);
led12.set(val.sw3);
// I moved your code here, in the db.get callback
io.sockets.on('connection', function (socket) {
socket.emit('fromserver', {sw1:led10.get(),sw2:led11.get(),swt3:led12.get()
});
socket.on('fromclient', function (data) {
if (data.l10==true)
led10.setHigh();
if (data.l10==false)
led10.setLow();
if (data.l11==true)
led11.setHigh();
if (data.l11==false)
led11.setLow();
if (data.l12==true)
led12.setHigh();
if (data.l12==false)
led12.setLow();
});
});
});
start=1;
}
On Mon, Feb 17, 2014 at 8:05 AM, Reiko Nötzold <
[email protected]> wrote:
> thanks is running and can you help me by socket.io?
> Button/Switches change in Browser the LED is set and is running.
> My next Goal to write the states in CouchDB and set the Buttons On/Off
> with the Values in CouchDB after first start.
>
> Client.js
> var socket = io.connect();
>
>
> $('input[type="checkbox"],[type="radio"]').not('#create-switch').bootstrapSwitch();
>
> $('#LED10').bootstrapSwitch('state', false); //from CouchDB
> $('#LED11').bootstrapSwitch('state', false); //from CouchDB
> $('#LED12').bootstrapSwitch('state', false); //from CouchDB
>
> socket.on('fromserver', function (data) {
> $('.label-toggle-switchl10').on('switchChange', function(e, data) {
> socket.emit('fromclient', { l10:data.value });
> });
> $('.label-toggle-switchl11').on('switchChange', function(e, data) {
> socket.emit('fromclient', { l11:data.value });
> });
> $('.label-toggle-switchl12').on('switchChange', function(e, data) {
> socket.emit('fromclient', { l12:data.value });
> });
> //console.log(data);
> });
>
>
>
> Server.js
> if(start==0)
> {
> db.get('4662745328718336f506f9d2d3010c0c', function(err, val) {
> console.log('Switches =', val);
> led10.set(val.sw1);
> led11.set(val.sw2);
> led12.set(val.sw3);
> });
> start=1;
> }
>
> io.sockets.on('connection', function (socket) {
> socket.emit('fromserver', {sw1:?,sw2:?,swt3:?});
> socket.on('fromclient', function (data) {
> if (data.l10==true)
> led10.setHigh();
> if (data.l10==false)
> led10.setLow();
> if (data.l11==true)
> led11.setHigh();
> if (data.l11==false)
> led11.setLow();
> if (data.l12==true)
> led12.setHigh();
> if (data.l12==false)
> led12.setLow();
> });
> });
>
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" 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/groups/opt_out.
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" 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/groups/opt_out.