2017-08-23 4 views
0

私はxamarin.Formsのpubnubを使ってチャットアプリケーションを試しています。ブレークポイントを維持することで理解できるチャンネルにログインできます。今では人のリストを取得しようとしています誰が自分のチャンネルにログインしていたのですか?ログインユーザーのリストを取得する方法-pubnub

<ContentPage.Content> 
    <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
     <Label Text="Welcome" TextColor="Blue" HorizontalOptions="Center" VerticalOptions="Center"> 
      <Label.FontSize> 
       <OnIdiom x:TypeArguments="x:Double"> 
        <OnIdiom.Phone> 
         <OnPlatform x:TypeArguments="x:Double" iOS="20" Android="20" WinPhone="20" /> 
        </OnIdiom.Phone> 
        <OnIdiom.Tablet> 
         <OnPlatform x:TypeArguments="x:Double" iOS="30" Android="30" WinPhone="30" /> 
        </OnIdiom.Tablet> 
       </OnIdiom> 
      </Label.FontSize> 
     </Label> 
     <ListView x:Name="LoginUserList" ItemsSource="{Binding LoginUserList}"ItemSelected="LoginUserList_OnItemSelected"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <StackLayout Orientation="Horizontal"> 
          <Label Text="{Binding UserName}" TextColor="SkyBlue" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"></Label> 
         </StackLayout> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
</ContentPage.Content> 

ここでは、ログインしているユーザーの一覧を表示する方法を示します。

public class ChatListViewModel : INotifyPropertyChanged 
{ 
    private List<UserDto> _loginUserList; 
    private string _offlineImageSource; 
    private bool _offlineVisibility; 
    private string _userName; 

    public ChatListViewModel() 
    { 
     InitializeData(); 
     GetAllUsersFromServer(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public List<UserDto> LoginUserList 
    { 
     get { return _loginUserList; } 
     set 
     { 
      _loginUserList = value; 
      OnPropertyChanged(); 
     } 
    } 

    public string OfflineImageSource 
    { 
     get { return _offlineImageSource; } 
     set 
     { 
      _offlineImageSource = value; 
      OnPropertyChanged(); 
     } 
    } 

    public string UserName 
    { 
     get { return _userName; } 
     set 
     { 
      _userName = value; 
      OnPropertyChanged(); 
     } 
    } 

    public bool OfflineVisibility 
    { 
     get { return _offlineVisibility; } 
     set 
     { 
      _offlineVisibility = value; 
      OnPropertyChanged(); 
     } 
    } 

    private async void GetAllUsersFromServer() 
    { 
     LoginUserList = await UserService.Instance.GetAllUsers(); 
    } 

      protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private void InitializeData() 
    { 
     OfflineImageSource = "BlackCircle.png"; 
    } 
} 

ここは、リスト表示のためのViewModelです。

+0

をあなたはLoginUserList ItemSourceにユーザーデータをバインドすることがありますか? –

+0

実際に私はwebApisからの登録ユーザのリストを表示するために、viewmodelのアイテムソースをバインドしています。私はpubnub apis there.Butを使用していませんでしたが、今はpubnubからバインドしたいのですが、 – sahithi

+0

あなたの質問にあなたのビューモデルコードを追加してください。 –

答えて

0

on "connect"コールバックを使用すると、PubNubチャネル上のアクティブなサブスクライバ/ユーザのリストを取得できます。新規または既存のユーザーが接続するときに呼び出されるon接続関数を提供します。これが発生すると、あなたはこの情報をあなたの最後に必要な場所に中継します。

私はあなたにいくつかのサンプルコードが表示されます:

PUBNUB.subscribe({ 
    channel : "hello_world",  // CONNECT TO THIS CHANNEL. 
    callback : function(message){} // RECEIVED A MESSAGE. 
    connect : function() {  // CONNECTION ESTABLISHED. 
     // A new user or existing user has 
     // connected. Send details to your server. 

     // --------------------------------------- 
     // This is a psudo-code example function: 
     // --------------------------------------- 
     psudo_post_url("http://www.mywebsite.com/pubnub-user-connected", { 
      "user_id" : 123456789, 
      "channel" : "hello_word" 
     }); 
    } 
}) 
+0

@Pavan V Parekhありがとう、私はこれを試してみます。 – sahithi

+1

このコードスニペットはPubNub v3 SDKからのものです。 v3はEOL(EOL)ですので、v4 SDKを参照してください。 v4はステータスイベントを受け取るリスナを使用し、その1つはPNConnectedCategoryです:https://www.pubnub.com/docs/web-javascript/pubnub-javascript-sdk#hello_world –

関連する問題