No carga contactos o datos en listbox asp.net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Saludos, tengo un problema, estoy construyendo un sitio de prueba que se conecta por ejemplo al chat del facebook y me muestra los contactos conectados, bien ya hice la conexion xmpp y logra conectarse y tambien me trae los usuarios conectados, esto lo se
xq lo compruebo con los puntos de interrupcion.
el problema esta, es cuando agrego los contactos al listbox o listview mediante listcontact.item.add o enlazarlos como una base de datos via datasource no me muestra nada en el listbox, e incluso trato de q lo muestre aunque sea 1 contacto en un textbox
y no me lo muestra, ya pongo el codigo de la web a ver que estoy haciendo mal.
el problema esta en xmppClient_OnPresence, ya q desde alli es que trato de cargar el listbox, he intentado muchas formas pero no me lo carga. Saludos y espero q puedan ayudarme a solucionar el problema.
<pre class="prettyprint <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default4.aspx.vb" Inherits="Default4" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

<html xmlns="http://www.w3.org/1999/xhtml
<head runat="server
<title></title>
</head>
<body>
<form id="form1" runat="server

<asp:ListBox ID="listEvents" runat="server" AutoPostBack="true" Width="109px </asp:ListBox>
<asp:ListBox ID="listContacts" runat="server" Height="299px" Width="246px" AutoPostBack="True </asp:ListBox>
<asp:TextBox ID="rtfDebug1" runat="server" TextMode="MultiLine" Height="243px" Width="250px </asp:TextBox>
<br />
<asp:TextBox ID="txtUsername" runat="server" Text="usuario </asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" Text="clave </asp:TextBox>
<asp:TextBox ID="txtserver" runat="server" Text="chat.facebook.com </asp:TextBox>
<br />
<asp:TextBox ID="txtMessage" runat="server </asp:TextBox>
<asp:Button ID="cmdConnect" runat="server" onclick="cmdConnect_Click" Text="Login" />
<asp:Button ID="cmdDisconnect" runat="server" onclick="cmdDisconnect_Click" Text="Logout" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:TextBox ID="txtJabberIdReceiver" runat="server" Text="txtJabberIdReceiver </asp:TextBox>
<asp:TextBox ID="txtHost" runat="server" Text="txtHost </asp:TextBox>


</form>
</body>
</html>[/code]
<br/>
<pre class="prettyprint Imports System.Collections
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Windows.Forms
Imports System.Security.Cryptography.X509Certificates
Imports System.Xml.Linq
Imports Matrix
Imports Matrix.Xml
Imports Matrix.Xmpp
Imports Matrix.Xmpp.Client
Imports Matrix.Xmpp.Register
Imports Matrix.Xmpp.XData
Imports Presence = Matrix.Xmpp.Client.Presence
Imports RosterItem = Matrix.Xmpp.Roster.RosterItem
Imports Subscription = Matrix.Xmpp.Roster.Subscription
Imports EventArgs = Matrix.EventArgs

Partial Class Default4
Inherits System.Web.UI.Page
Public Shared Function SendMessage(window As IntPtr, message As Integer, wparam As Integer, lparam As Integer) As IntPtr
End Function

Public Shared Function GetRosterImageIndex(pres As Presence) As Integer
If pres.Type = PresenceType.unavailable Then
Return 0
End If

Select Case pres.Show
Case Show.chat
Return 1
Case Show.away
Return 2
Case Show.xa
Return 2
Case Show.dnd
Return 3
Case Else
Return 1
End Select
End Function
Const WM_VSCROLL As Integer = &H115
Const SB_BOTTOM As Integer = 7

Public listado As New System.Windows.Forms.ListView
Private WithEvents rtfDebug As System.Windows.Forms.RichTextBox
Private WithEvents xmppClient As Matrix.Xmpp.Client.XmppClient
Private WithEvents presenceManager As Matrix.Xmpp.Client.PresenceManager
Private WithEvents Ros As Matrix.Xmpp.Roster.RosterEventArgs
Private ReadOnly _dictContactGroups As New Dictionary(Of String, ListViewGroup)()
Private ReadOnly _dictContats As New Dictionary(Of Jid, RosterItem)()

Private Shared Sub SetLicense()
const string LIC = @"YOUR LICENSE";
Matrix.License.LicenseManager.SetLicense(LIC);
End Sub
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager
Me.cmdConnect = New System.Web.UI.WebControls.Button()
Me.cmdDisconnect = New System.Web.UI.WebControls.Button()
Me.listContacts = New System.Web.UI.WebControls.ListBox()
Me.listContacts = New System.Web.UI.WebControls.ListView()
Me.listEvents = New System.Web.UI.WebControls.ListBox()
Me.txtHost = New System.Web.UI.WebControls.TextBox()
Me.txtserver = New System.Web.UI.WebControls.TextBox()
Me.txtPassword = New System.Web.UI.WebControls.TextBox()


xmppClient


Me.xmppClient = New Matrix.Xmpp.Client.XmppClient
Me.xmppClient.Compression = False
Me.xmppClient.Hostname = Nothing
Me.xmppClient.ProxyHostname = Nothing
Me.xmppClient.ProxyPass = Nothing
Me.xmppClient.ProxyPort = 1080
Me.xmppClient.ProxyType = Matrix.Net.Proxy.ProxyType.None
Me.xmppClient.ProxyUser = Nothing
Me.xmppClient.ResolveSrvRecords = True
Me.xmppClient.Status = ""
Me.xmppClient.Transport = Matrix.Net.Transport.Socket
Me.xmppClient.Uri = Nothing
End Sub
Public Sub New()
InitializeComponent()

SetLicense()
End Sub

Private Sub DisplayEvent(ev As String)
listEvents.Items.Add(ev)
listEvents.SelectedIndex = listEvents.Items.Count - 1
End Sub

Protected Sub cmdConnect_Click(sender As Object, e As System.EventArgs) Handles cmdConnect.Click
xmppClient.SetXmppDomain(txtserver.Text)
xmppClient.SetUsername(txtUsername.Text)
xmppClient.Password = txtPassword.Text
xmppClient.Status = "ready for chat"
xmppClient.Show = Xmpp.Show.chat

If Not String.IsNullOrEmpty(txtHost.Text) Then
xmppClient.Hostname = txtHost.Text
End If
xmppClient.Open()
End Sub

Protected Sub cmdDisconnect_Click(sender As Object, e As System.EventArgs) Handles cmdDisconnect.Click
xmppClient.Close()
End Sub

Private Sub xmppClient_OnPrebind(sender As Object, e As Matrix.Net.PrebindEventArgs) Handles xmppClient.OnPrebind
DisplayEvent("OnPrebind Rid:" + e.Rid + " Sid:" + e.Sid)
End Sub

Private Sub xmppClient_OnError(sender As Object, e As ExceptionEventArgs) Handles xmppClient.OnError
DisplayEvent("OnError")
End Sub

Private Sub xmppClient_OnLogin(sender As Object, e As Matrix.EventArgs) Handles xmppClient.OnLogin
DisplayEvent("OnLogin")
End Sub

Private Sub xmppClient_OnBind(sender As Object, e As JidEventArgs) Handles xmppClient.OnBind
DisplayEvent("OnBind")
End Sub

Private Sub xmppClient_OnClose(sender As Object, e As Matrix.EventArgs) Handles xmppClient.OnClose
DisplayEvent("OnClose")
listContacts.Items.Clear()
End Sub

Private Sub xmppClient_OnRosterStart(sender As Object, e As Matrix.EventArgs) Handles xmppClient.OnRosterStart

End Sub

Private Sub xmppClient_OnRosterEnd(sender As Object, e As Matrix.EventArgs) Handles xmppClient.OnRosterEnd
DisplayEvent("OnRosterEnd")
End Sub

Private Sub xmppClient_OnRosterItem(sender As Object, e As Matrix.Xmpp.Roster.RosterEventArgs)
Dim version = e.Version
DisplayEvent(String.Format("OnRosterItem" & vbTab & "{0}" & vbTab & "{1}", e.RosterItem.Jid, e.RosterItem.Name))

If e.RosterItem.Subscription <> Subscription.remove Then
set a default group name
Dim groupname As String = "Contacts"

id the contact has groups get the 1st group. In this example we dont support multiple or nested groups
for contacts, but XMPP has support for this.
If e.RosterItem.HasGroups Then
groupname = e.RosterItem.GetGroups()(0)
End If

If Not _dictContactGroups.ContainsKey(groupname) Then
Dim newGroup = New ListViewGroup(groupname)
_dictContactGroups.Add(groupname, newGroup)
listContacts.Groups.Add(newGroup)
End If

Dim listGroup = _dictContactGroups(groupname)

contact already exists, this is a contact update
If _dictContats.ContainsKey(e.RosterItem.Jid) Then
listContacts.Items.RemoveAt(e.RosterItem.Jid)
End If

var newItem = new ListViewItem(e.RosterItem.Jid, listGroup) {Name = e.RosterItem.Jid};
Dim newItem = New RosterListViewItem(If(e.RosterItem.Name, e.RosterItem.Jid.ToString), 0, listGroup) With {.Name = e.RosterItem.Jid.Bare}

newItem.SubItems.AddRange(New String() {"", ""})
listado.Items.Add(newItem)
End If
End Sub

Private Sub xmppClient_OnPresence(sender As Object, e As PresenceEventArgs)
DisplayEvent(String.Format("OnPresence" & vbTab & "{0}", e.Presence.From))


If e.Presence.Type = PresenceType.subscribe Then

ElseIf e.Presence.Type = PresenceType.subscribed Then

ElseIf e.Presence.Type = PresenceType.unsubscribe Then

ElseIf e.Presence.Type = PresenceType.unsubscribed Then
Else
Dim item = TryCast(listado.Items(e.Presence.From.Bare), RosterListViewItem)
Dim dStudent As New Dictionary(Of Integer, String)()

For i = 0 To item.SubItems.Count - 1
dStudent.Add(i, item.Text)
txtJabberIdReceiver.Text = item.Text
Console.WriteLine(item.Text)
Next
listContacts.DataSource = dStudent
listContacts.DataTextField = "Value"
listContacts.DataValueField = "Key"
listContacts.DataBind()

If item IsNot Nothing Then
item.ImageIndex = GetRosterImageIndex(e.Presence)
Dim resource As String = e.Presence.From.Resource
If e.Presence.Type <> PresenceType.unavailable Then
If Not item.Resources.Contains(resource) Then
item.Resources.Add(resource)
End If
Else
If item.Resources.Contains(resource) Then
item.Resources.Remove(resource)
End If
End If
End If
End If
End Sub
End Class
[/code]
<br/>
<hr class="sig ejs

View the full article
 
Back
Top