Disconnection And Reset

When device is disconnected from the internet or calling close() will cause disconnection. Then onclose will be called.

Operations can't be delivered after disconnection, device or cloud will automatically reset the state.

When device disconnected from the internet.

If the device is powered on normally, but disconnected from the network, the device will automatically "reset" and all the IO and other states will return to the same state as after powering on. In such a case, to stop the reset and keep the IO and other states as they were at the end (high ios will remain high), the following settings can be made after the connection, and the device will maintain the IO and other states.

It can be used to maintain what is displayed on a connected display without resetting the display.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function() {
  obniz.keepWorkingAtOffline(true);
}

IO, etc. will just continue the last state. If it is io.output(true), it will continue outputting, but if it is

obniz.onconnect = async function() {
  obniz.keepWorkingAtOffline(true);
  var val = true;
  while(true) {
    obniz.io0.output(val);
    val = !val;
    await obniz.wait(1);
  }
}

In such a case, it is not clear whether io0 will stop with false or true.

When obniz.js is disconnected from the cloud.

If the device itself is communicating with the cloud normally, but you close the browser running the program or stop the nodejs program, the obniz cloud will tell the device to reset. However, if you have multiple connections and there are still connections from other programs, the reset will not be sent. This setting can be optionally turned off. This setting can be optionally turned off, so that even if you exit the program, the device IO, etc. will remain in its last state.

By using this setting, we can connect to the device from obniz.js only when necessary, and keep the device in its last state even if the obniz.js program is stopped after changing the display or turning the motor.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE", { reset_obniz_on_ws_disconnection: false });
obniz.onconnect = async function() {

}

Alternatively, you can change the settings after the connection as shown below.

// Javascript Full Example

var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function() {
  obniz.resetOnDisconnect(false);
}

Event-driven by obniz connection state

You can receive the firing of events depending on the connection state of the obniz.

For example, the connect event, which is fired when obniz is connected, can be received by the on() and the corresponding processing can be run.
Similarly, the close event can be received by the on() when the connection to obniz is closed.

// Javascript Example

obniz.on('connect', async() => {
  console.log("a connect event occurred!");
  await obniz.wait(3000);
  obniz.close();
});

obniz.on('close', () => {
  console.log("a close event occurred!");
});

The once() can also be used as a listener. The on() will receive and run any number of events, while the once() will only be run the first time.

// Javascript Example

let isConnectPhase = true;

setInterval(async() => {
  if(isConnectPhase){
    obniz.connect();
  }else{
    obniz.close();
  }
  isConnectPhase = !isConnectPhase
}, 3000);

obniz.once('connect', () => {
  console.log("a connect event occurred for the first time!");
});

obniz.on('connect', () => {
  console.log("a connect event occurred!");
});

関連の質問