message: add additional error checking on socket connection
diff: @@ -132,11 +132,15 @@ function SocketBot(configuration) {
  if (bot.connected || !bot.ws) {
  if (bot.ws) {
  try {
  + if (bot.ws && bot.ws.readyState === WebSocket.OPEN) {
  bot.ws.send(JSON.stringify(message), function(err) {
  if (cb) {
  return cb(err, message);
  }
  });
  + } else {
  + console.error('Cannot send message to closed socket');
  + }
  } catch (err) {
  return cb(err);
  }
  @@ -164,6 +168,7 @@ function SocketBot(configuration) {
  bot.startTyping = function() {
  if (bot.connected) {
  try {
  + if (bot.ws && bot.ws.readyState === WebSocket.OPEN) {
  bot.ws.send(JSON.stringify({
  type: 'typing'
  }), function(err) {
  @@ -171,6 +176,9 @@ function SocketBot(configuration) {
  console.error('startTyping failed: ' + err.message);
  }
  });
  + } else {
  + console.error('Socket closed! Cannot send message');
  + }
  } catch (err) {
  console.error('startTyping failed: ', err);
  }
  