2010-12-14 83 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