2012-07-19 4 views
0

이 내 코드에서 이벤트는 별표 관리자 인터페이스에 연결을 얻을 :나는 캔트 별표 관리자 인터페이스

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text.RegularExpressions; 
using System.Text; 
using System.Windows.Forms; 
using System.Net.Sockets; 
using System.Net; 
using System.IO; 


namespace WindowsFormsApplication1 
{ 

public partial class Form1 : Form 
{ 

    private Socket clientSocket; 
    private byte[] data = new byte[1024]; 
    private int size = 1024; 
    //------------------------------------------------------------------------------------------ 
    public Form1() 
    { 
     InitializeComponent();    
    } 

    //------------------------------------------------------------------------------------------ 
    [STAThread] 
    private void BtnConnect_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      AddItem("Connecting..."); 
      clientSocket = new Socket(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp); 
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.155"), 5038); 
      clientSocket.BeginConnect(iep, new AsyncCallback(Connected), clientSocket); 
     } 
     catch (Exception exp) 
     { 

      AddItem(exp.Message); 
     } 
    } 

    //------------------------------------------------------------------------------------------ 
    private void BtnDisconnect_Click(object sender, EventArgs e) 
    { 
     clientSocket.Close(); 
    } 

    //------------------------------------------------------------------------------------------ 
    void Connected(IAsyncResult iar) 
    { 
     clientSocket = (Socket)iar.AsyncState; 
     try 
     { 
      clientSocket.EndConnect(iar); 
      AddItem("Connected to: " + clientSocket.RemoteEndPoint.ToString());     
      clientSocket.BeginReceive(data, 0, size, SocketFlags.None, 
          new AsyncCallback(OnDataReceive), clientSocket); 
     } 
     catch (Exception exp) 
     { 
      AddItem("Error connecting: " + exp.Message); 
     } 
    } 
    //------------------------------------------------------------------------------------------ 

    private void OnDataReceive(IAsyncResult result) 
    { 
     Socket remote = (Socket)result.AsyncState; 
     int recv = remote.EndReceive(result); 
     string stringData = Encoding.ASCII.GetString(data, 0, recv); 
     AddItem(stringData); 
    } 

    //------------------------------------------------------------------------------------------ 
    private delegate void stringDelegate(string s); 
    private void AddItem(string s) 
    { 
     if (ListBoxEvents.InvokeRequired) 
     { 
      stringDelegate sd = new stringDelegate(AddItem); 
      this.Invoke(sd, new object[] { s }); 
     } 
     else 
     { 
      ListBoxEvents.Items.Add(s); 
     } 
    } 

    //------------------------------------------------------------------------------------------ 
    private void BtnLogin_Click(object sender, EventArgs e) 
    { 
     clientSocket.Send(Encoding.ASCII.GetBytes("Action: Login\r\nUsername: admin\r\nSecret: lastsecret\r\nActionID: 1\r\n\r\n")); 
    } 

    //------------------------------------------------------------------------------------------ 


} 
} 

문제는 내가 서버에 연결할 때이 "별표 통화 관리자/1.1"메시지가 나타날 것입니다. 서버에 연결 한 후 서버에 로그인했는데 메시지가 표시되지 않습니다. 별표에서 이벤트를 가져오고 싶습니다. 네트워크 소켓 사용에 문제가 있습니까? 특수 명령을 사용하여 별표에 이벤트 수신을 알리는 것이 좋습니다.

답변

2

[편집]

예. 따라서 AMI 구성에는 문제가 없습니다.

연결이 완료되면 BeginReceive가 시작되지만 데이터를 한 번받은 후에는 새 BeginReceive가 시작되지 않습니다.

OnDataReceive에서 BeginReceive를 다시 호출하여 소켓에서 다시 읽으려고해야합니다.

clientSocket.BeginReceive(data, 0, size, SocketFlags.None, 
          new AsyncCallback(OnDataReceive), clientSocket); 

아래에서 원래의 답을 유지하고 있습니다. 그 정보는 여전히 확인해야 할 사항입니다. 내 "FYI"를 다시 강조 할 것입니다. 교육적인 목적으로이 작업을 수행하지 않는 한, 실제로 TCP에 익숙하지 않은 경우 기존의 AMI 라이브러리를 사용해야합니다.

[원본]

보내는 사용자 이름과 암호는 무엇인가요? 이벤트를 보내기 위해 계정이 올바르게 구성 되었습니까?

로그인하여 Asterisk와의 연결을 인증 한 후에는 자동으로 이벤트를 받기 시작해야합니다. 그러나 특정 유형의 이벤트를 수신하려면 적절한 읽기 권한 클래스 권한이 필요합니다.

; Authorization for various classes 
; 
; Read authorization permits you to receive asynchronous events, in general. 
; Write authorization permits you to send commands and get back responses. The 
; following classes exist: 
; 
; all  - All event classes below (including any we may have missed). 
; system - General information about the system and ability to run system 
;    management commands, such as Shutdown, Restart, and Reload. 
; call  - Information about channels and ability to set information in a 
;    running channel. 
; log  - Logging information. Read-only. (Defined but not yet used.) 
; verbose - Verbose information. Read-only. (Defined but not yet used.) 
; agent  - Information about queues and agents and ability to add queue 
;    members to a queue. 
; user  - Permission to send and receive UserEvent. 
; config - Ability to read and write configuration files. 
; command - Permission to run CLI commands. Write-only. 
; dtmf  - Receive DTMF events. Read-only. 
; reporting - Ability to get information about the system. 
; cdr  - Output of cdr_manager, if loaded. Read-only. 
; dialplan - Receive NewExten and VarSet events. Read-only. 
; originate - Permission to originate new calls. Write-only. 
; agi  - Output AGI commands executed. Input AGI command to execute. 
; cc  - Call Completion events. Read-only. 
; aoc  - Permission to send Advice Of Charge messages and receive Advice 
;   - Of Charge events. 
; test  - Ability to read TestEvent notifications sent to the Asterisk Test 
;    Suite. Note that this is only enabled when the TEST_FRAMEWORK 
;    compiler flag is defined. 

그래서, 내가 더 ACL을 정의하지와 암호 "바"와 사용자 "foo는"로 인증을 원했고, 나는 모든 이벤트를 수신하고 싶어 말 : 샘플 manager.conf에서. 또한 '시스템'과 '호출'클래스 명령 만 실행할 수 있기를 원합니다. manager.conf에서 내 사용자를 다음과 같이 설정해야합니다.

[foo] 
secret = bar 
read = all 
write = system,call 

참고로 여기에서 휠을 다시 발명했을 수 있습니다. 운동으로 자신의 AMI 라이브러리를 만드는 경우가 아니면 Asterisk .NET을 사용하는 것이 좋습니다.

+0

감사합니다. Matt. 사실 TCP에 익숙하지 않습니다. NET 소켓 프로그래밍에 익숙하지 않습니다. – Karadous