2010-12-14 68 views
1

所以在我试图使一个CMS系统,利用雅虎通讯SDK。这个想法是让自助机器人能够引导客户解决某些问题。会话通过两种方法运行。一个脚本存在以回应一般性回应。在客户收到的每条消息中,程序将查找certan关键字和问题,这些问题会触发XML文件的响应。如果它找到一个它继续与脚本。该计划的工作,但需要付出代价。它是一个巨大的资源。在这个程序中,我有一个类来处理所有的yahoo messenger函数,比如登录,注销,接收和发送消息。我也有一堂课,我称之为对话。这样可以保证消息来源的形式,发生的原因以及在脚本对话中的位置。在我的主要计划中,我根据有多少不同的网站帮助会计师使用我的初始X客户类。每次它收到一条消息时,它都会创建一个新的会话类,因为它存在或它检查现有会话并找到坐标位置。它显然也会对传入消息中的关键字进行所有检查。这是我用于接收消息的共享事件处理程序的代码。我的问题是,有没有办法让这个更有效率。资源和效率

Private Sub yahooclients_OnRec(ByVal sender As Object, ByVal buddy As String, ByVal message As String) 
    TotalRec = TotalRec + 1 

    Try 


     Dim c As YahooClient = CType(sender, YahooClient) 'Yahoo Client To Send Message From 
     showLog("From:" & buddy & " To:" & c.Account & " Message:" & message) 
     Dim msgSplit As String() 
     Dim retmsg As String 
     Dim smsg As String() 
     Dim n1 As XmlNode 
     Dim sran As New Random 'Random SPlit Message 
     Dim domran As New Random 'Random Domain ID 
     Dim Found1 As Boolean = False 
     Dim FoundIt As Integer = 0 
     Dim i As Integer = 0 'Keyword Counter 
     'Check Message For KeyWords By Splitting Each phrase by spaces 
     msgSplit = Split(message, " ") 
     For Each word In msgSplit 
      For Each value In KeywordInd 
       If value = word Then 
        n1 = m_nodelist.Item(i) 
        retmsg = n1.InnerText 
        GoTo ScrubMessage 
       End If 
       i = i + 1 
      Next 
      i = 0 
     Next 

     'Check For Conversations 
     If convos.Count = 0 Then 
      convos.Add(New Conversation(c.Account, buddy, 0)) 
      retmsg = Script(0) 
      GoTo ScrubMessage 
     Else 
      For A As Integer = 0 To (convos.Count - 1) 
       If InStr(convos(A).TUser, c.Account) > 0 And InStr(convos(A).FUser, buddy) > 0 Then 
        Found1 = True 
        Exit For 
       End If 
       FoundIt = FoundIt + 1 
      Next 
      If Found1 = True Then 
       convos(FoundIt).SPosition = convos(FoundIt).SPosition + 1 
       'Send Next Position In Script 
       If convos(FoundIt).SPosition > (Script.Length - 1) Then 
        If convos(FoundIt).SPosition = Script.Length Then 
         TotalScript = TotalScript + 1 
         ToolStripStatusLabel10.Text = TotalScript 
        End If 
        Exit Sub 
       End If 
       retmsg = Script(convos(FoundIt).SPosition) 
       GoTo ScrubMessage 
      Else 
       convos.Add(New Conversation(c.Account, buddy, 0)) 
       retmsg = Script(0) 
       GoTo ScrubMessage 
      End If 
     End If 

ScrubMessage: “去掉| SMSG =斯普利特(retmsg,“|”)

 'Pull A Random Response 
     If smsg.Length > 1 Then 
      retmsg = smsg(sran.Next(0, (smsg.Length) - 1)) 
     Else 
      retmsg = smsg(0) 
     End If 


     'Check For Domain Indicator 
     If InStr(retmsg, "%") > 0 Then 
      TotalLink = TotalLink + 1 
     End If 
     retmsg = Replace(retmsg, "%s", Domains(domran.Next(0, (Domains.Length - 1)))) 

     If CheckBox2.Checked = True Then 'send Message With Font and Color 
      retmsg = "<font face=" & """" & fname & """" & ">" & "[#FF80C0m" & retmsg & "</font>" 
     End If 


     showLog(("Sending Message: " & retmsg & " To: " & buddy & " From: " & c.Account)) 
     c.SendMessage(buddy, retmsg) 
     TotalSent = TotalSent + 1 
     ToolStripStatusLabel4.Text = TotalSent 'Updates Sent Counter 
     ToolStripStatusLabel6.Text = TotalRec 'Updates Rec Counter 
     ToolStripStatusLabel8.Text = TotalLink 'Updates Links counter 

    Catch ex As Exception 
     showLog(ex.ToString) 
    End Try 

End Sub 

程序变为真的很多账目没有resonsive都在问的问题等

回答

0

有一些看起来像一个良好的开端几件事情。

1)将ScrubMessage后面的代码拖入它自己的函数中,该函数将需要的信息作为参数。然后用呼叫替换所有的GoTos。这可能不会提高性能,但使代码更易于大多数人理解(并且约定表示原始gotos是邪恶的)。

Sub ScrubAndSendMessage(response As String, client As YahooClient) 
    'code after label here with needed local variables 
End Sub 

2)您正在搜索每个传入消息的每个词的列表。根据使用情况,KeywordInd和m_nodelist很可能像这样声明:

Dim KeywordInd As List(Of String) 
Dim m_nodelist As List(Of XmlNode) 'or whatever type has the inner text 

由于关键字列表似乎是一个关键到值的节点列表,我建议使用字典来简化代码,并可能提高速度。

Dim wordResponse As New Dictionary(Of String, String) 

,其中键是从KeywordInd字样和值是InnerTextm_nodelist,它可以让你说

For Each word In msgSplit 
    If wordResponse.ContainsKey(word) Then 
     ScrubMessage(wordResponse(word)) 
     Exit Sub 
    End If 
Next 

3)尽量使你不必谈话直接与YahooClient对象关联每次都要查找查找对话。当你这样做的时候,你也可以确保每个客户端都有一个Conversation,如果需要的话。

4)您直接与处理程序中的UI组件进行交互。这要求所有客户端的所有处理程序都在UI线程上运行。您可能会更好地从该处理程序中引发一些事件,并让表单处理事件并根据需要发布到UI线程。事件可能是ScriptPositionAdvancedResponseSent或类似的东西。然后,表单/控件可以处理这些事件,并根据您选择的UI框架所使用的模式发布到UI线程,您应该可以通过快速搜索来找到它。

+0

非常感谢提示! – 2011-01-13 21:24:50