Fix wrong binding of JS this

inside the closure this binds to the WebSocket object rather than to the figure object.