How to use leancloud-realtime - 10 common examples

To help you get started, we’ve selected a few leancloud-realtime examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github leancloud / leanmessage-demo / src / app / conversation / conversationMessage / conversation.message.controller.js View on Github external
$scope.sendText = () => {
    const {draft} = $scope;
    if (!draft) {
      return;
    }
    const message = new TextMessage(draft);
    // 匹配出所有的 @
    const results = draft.match(/(^|\s)@(\S*)/g);
    if (results) {
      const mentionMatchResults = new Set(results.map(match => match.trim().slice(1)));
      console.log(mentionMatchResults);
      // 找出 @all
      if (mentionMatchResults.has('all')) {
        message.mentionAll();
        mentionMatchResults.delete('all');
      }
      message.setMentionList(Array.from(mentionMatchResults));
    }
    $scope.draft = '';
    $scope.smileysShow = false;

    return $scope.send(message);
github leancloud / leanmessage-demo / src / app / conversation / conversation.controller.js View on Github external
$scope.$digest();
  };
  const invitedHandler = (payload, conversation) => {
    if (conversation.transient) {
      // 暂态对话
      if ($scope.transConvs.indexOf(conversation) === -1) {
        $scope.transConvs.push(conversation);
      }
    } else if ($scope.normalConvs.indexOf(conversation) === -1) {
      $scope.normalConvs.push(conversation);
    }
    $scope.$digest();
  };

  const client = $scope.imClient;
  client.on(Event.MESSAGE, messageHandler);
  client.on(Event.INVITED, invitedHandler);
  client.on(Event.UNREAD_MESSAGES_COUNT_UPDATE, () => {
    $scope.$emit('unreadCountUpdate', getTotalUnreadCount());
  });
  client.on(Event.DISCONNECT, () => {
    $scope.networkError = '连接已断开';
    $scope.networkErrorIcon = 'sync_problem';
    $scope.$digest();
  });
  client.on(Event.OFFLINE, () => {
    $scope.networkError = '网络不可用,请检查网络设置';
    $scope.networkErrorIcon = 'signal_wifi_off';
    $scope.networkShowRetry = false;
    $scope.$digest();
  });
  client.on(Event.ONLINE, () => {
github wujun4code / DoChat / chatkit-client / src / components / lc-conversation-list / lc-conversation-list.ts View on Github external
initList() {
    let realtime = new Realtime({ appId: lcGlobal.leancloud.appId, region: 'cn' });
    realtime.createIMClient(this.currentClientId).then(imClient => {
      //this.avIMClient = imClient;
      SharedService.client = imClient;
      SharedService.client.on('message', (message: Message, conversation: Conversation) => {
        let eventName: string = 'lc:received:' + conversation.id;
        // 检查是否开启了本地缓存聊天记录的选择,如果开启则记录在本地
        if (lcGlobal.realtime_config.localCache) {
          this.cacheProvider.getProvider(this.currentClientId).push(message, conversation);
        }
        console.log('Message received: ', message.id, message.type, JSON.stringify(message), eventName);
        this.updateLastMessage(message);
        this.events.publish(eventName, message);
      });

      return SharedService.client.getQuery().withLastMessagesRefreshed(true).find();
    }).then(cons => {
github leancloud / leanmessage-demo / src / app / conversation / conversation.controller.js View on Github external
};
  const invitedHandler = (payload, conversation) => {
    if (conversation.transient) {
      // 暂态对话
      if ($scope.transConvs.indexOf(conversation) === -1) {
        $scope.transConvs.push(conversation);
      }
    } else if ($scope.normalConvs.indexOf(conversation) === -1) {
      $scope.normalConvs.push(conversation);
    }
    $scope.$digest();
  };

  const client = $scope.imClient;
  client.on(Event.MESSAGE, messageHandler);
  client.on(Event.INVITED, invitedHandler);
  client.on(Event.UNREAD_MESSAGES_COUNT_UPDATE, () => {
    $scope.$emit('unreadCountUpdate', getTotalUnreadCount());
  });
  client.on(Event.DISCONNECT, () => {
    $scope.networkError = '连接已断开';
    $scope.networkErrorIcon = 'sync_problem';
    $scope.$digest();
  });
  client.on(Event.OFFLINE, () => {
    $scope.networkError = '网络不可用,请检查网络设置';
    $scope.networkErrorIcon = 'signal_wifi_off';
    $scope.networkShowRetry = false;
    $scope.$digest();
  });
  client.on(Event.ONLINE, () => {
    $scope.networkError = '网络已恢复';
github leancloud / leanmessage-demo / src / app / conversation / conversation.controller.js View on Github external
const invitedHandler = (payload, conversation) => {
    if (conversation.transient) {
      // 暂态对话
      if ($scope.transConvs.indexOf(conversation) === -1) {
        $scope.transConvs.push(conversation);
      }
    } else if ($scope.normalConvs.indexOf(conversation) === -1) {
      $scope.normalConvs.push(conversation);
    }
    $scope.$digest();
  };

  const client = $scope.imClient;
  client.on(Event.MESSAGE, messageHandler);
  client.on(Event.INVITED, invitedHandler);
  client.on(Event.UNREAD_MESSAGES_COUNT_UPDATE, () => {
    $scope.$emit('unreadCountUpdate', getTotalUnreadCount());
  });
  client.on(Event.DISCONNECT, () => {
    $scope.networkError = '连接已断开';
    $scope.networkErrorIcon = 'sync_problem';
    $scope.$digest();
  });
  client.on(Event.OFFLINE, () => {
    $scope.networkError = '网络不可用,请检查网络设置';
    $scope.networkErrorIcon = 'signal_wifi_off';
    $scope.networkShowRetry = false;
    $scope.$digest();
  });
  client.on(Event.ONLINE, () => {
    $scope.networkError = '网络已恢复';
    $scope.$digest();
github leancloud / leanmessage-demo / src / index.js View on Github external
.factory('LeanRT', () => {
    const LeanRT = {};
    const realtime = new Realtime({
      appId,
      appKey,
      // server: 'rtm51',
      plugins: [TypedMessagesPlugin, GroupchatReceiptsPlugin, TypingIndicatorPlugin],
      region: 'cn' // 美国节点为 "us"
    });
    realtime.register([StickerMessage]);
    LeanRT.realtime = realtime;
    LeanRT.imClient = null;
    LeanRT.currentConversation = null;
    return LeanRT;
  })
  .service('userService', userService)
github leancloud / leanmessage-demo / src / app / conversation / conversationMessage / conversation.message.controller.js View on Github external
// 消息列表滚动
      $scope.messages.push(msg);
      scrollToBottom();
    };

    const receiptUpdateHandler = () => {
      $scope.$digest();
    };

    const handleVisibilityChange = () => {
      if (!document.hidden && conversation.unreadMessagesCount) {
        conversation.read();
      }
    };

    conversation.on(Event.MEMBERS_JOINED, membersJoinedHandler);
    conversation.on(Event.MEMBERS_LEFT, membersLeftHandler);
    conversation.on(Event.KICKED, kickedHandler);
    conversation.on(Event.INFO_UPDATED, infoUpdatedHandler);
    conversation.on(Event.MEMBER_INFO_UPDATED, memberInfoUpdateHandler);
    conversation.on(Event.MESSAGE, readMarker);
    conversation.on(Event.MESSAGE, messageUpdater);
    conversation.on(Event.LAST_DELIVERED_AT_UPDATE, receiptUpdateHandler);
    conversation.on(Event.LAST_READ_AT_UPDATE, receiptUpdateHandler);
    conversation.on('lastreadtimestampsupdate', receiptUpdateHandler);
    conversation.on(Event.MESSAGE_RECALL, replaceRecalledMessage);
    document.addEventListener("visibilitychange", handleVisibilityChange);

    $scope.$on("$destroy", () => {
      conversation.off(Event.MEMBERS_JOINED, membersJoinedHandler);
      conversation.off(Event.MEMBERS_LEFT, membersLeftHandler);
      conversation.off(Event.KICKED, kickedHandler);
github leancloud / leanmessage-demo / server / index.js View on Github external
function sendMessage(content, peerId, convId) {
  console.log('sending message [' + content + '] to peer [' + peerId + ']');
  var message = new TextMessage(content);
  var conversation = AV.Object.createWithoutData('_Conversation', convId);
  return conversation.send('MathBot', message, { toClients: [peerId] }, { useMasterKey: true }).then(function() {
    console.log('sended: ' + JSON.stringify(message));
  }, function(error) {
    console.error('send message error: ' + error.message);      
  });
}
github wujun4code / DoChat / chatkit-client / src / components / lc-conversation-list / lc-conversation-list.ts View on Github external
SharedService.client.getConversation(convIdStr, true).then(conv => {
      let txtMessage = new TextMessage(sendEventData.text);
      console.log(JSON.stringify(this.items));
      console.log('send(sendEventData)');
      return conv.send(txtMessage);
    }).then(message => {
      this.updateLastMessage(message);
github leancloud / leanmessage-demo / server / index.js View on Github external
AV.Cloud.define('chime', function() {
  if (!CHIME_CONV_ID) return console.warn('CHIME_CONV_ID not set, skip chiming');
  console.log('chime');
  var hours = Math.round((Date.now() / 3600000  + 8) % 24);
  var message = new TextMessage('北京时间 ' + hours + ' 点整');
  var conversation = AV.Object.createWithoutData('_Conversation', CHIME_CONV_ID);
  return conversation.broadcast('LeanObservatory', message, { validTill: Date.now() }, { useMasterKey: true }).then(function() {
    console.log('chimed');
  }, function(error) {
    console.error('broadcast message error: ' + error.message);      
  });
});