Add onopen and onclose callback functions