2010-12-14 6 views
1

私はyahoo messenger SDKを利用するCMSシステムを作ろうとしています。自分の助けを借りて、特定の問題を解決してクライアントを導くことがアイデアです。会話は2つの方法で実行されます。一般的な応答で応答するためのスクリプトが1つあります。クライアントが受け取る各メッセージで、プログラムはXMLファイルからの応答を引き起こすcertanのキーワードと質問を探します。もしそれが見つかると、それはスクリプトで続行されます。プログラムは機能しますが、コストはかかります。その巨大な資源豚。プログラムでは、ログイン、ログアウト、受信、メッセージ送信などのすべてのyahooメッセンジャー機能を扱うクラスがあります。私は会話と呼ぶクラスも持っています。これはメッセージがどこに来たのか、どこに誰が行くのか、どの場所がスクリプトされた会話の中にあったのかを把握しています。私の主なプログラムでは、私が使用しているアパートの数に応じてクライアントクラスの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、「|」)プログラムは、実際にアカウントの多くとはresonsiveなりません

 '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 

は質問をしているなど

答えて

0

開始するには良い場所のように見えるがいくつかあります。

1)ScrubMessageの後のコードを、必要な情報をパラメータとする独自の関数に引き出します。その後、すべての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 

キーワードリストは、値のノードリストへの鍵のようですので、私はコードを簡素化するために辞書を使用することをお勧めし、潜在的でしょう速度を向上させる。キーはKeywordIndから単語や値は

Dim wordResponse As New Dictionary(Of String, String) 

あるあなたが言うことができます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

ヒントをいただきありがとうございます! –