Asterisk 我无法从星号管理器界面获取事件

Asterisk 我无法从星号管理器界面获取事件,asterisk,asteriskami,Asterisk,Asteriskami,这是连接到Asterisk Manager界面的我的代码: 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

这是连接到Asterisk Manager界面的我的代码:

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"));
    }

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


}
}
问题是,当我连接到服务器时,我收到“Asterisk Call Manager/1.1”消息。连接到服务器后,我登录到服务器,没有收到任何消息。我想从asterisk获取事件。使用网络套接字时是否存在问题?我是否应该使用特殊命令告诉星号我想要接收事件。

[编辑]

嗯。所以这不是AMI配置的问题

当连接完成时,您会启动一个BeginReceive,但一旦您收到一次数据,就不会启动一个新的BeginReceive

您需要在OnDataReceive中再次调用BeginReceive,以便它再次尝试从套接字读取

clientSocket.BeginReceive(data, 0, size, SocketFlags.None,
                          new AsyncCallback(OnDataReceive), clientSocket);
我在下面保留我的原始答案,因为这些信息仍然是你应该检查的。我将再次重申我的“仅供参考”——除非你是出于教育目的而这样做的,否则你真的应该使用现有的AMI库——特别是如果你不熟悉TCP的话

[原件]

您发送的用户名和密码是什么?您确定已正确配置帐户以发送事件吗

一旦您通过登录通过Asterisk验证了您的连接,您应该开始自动接收事件。但是,请记住,您需要适当的读取授权类权限才能接收特定类型的事件。从sample manager.conf:

; 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.
所以,假设我想用密码“bar”作为用户“foo”进行身份验证,没有定义ACL,我想接收所有事件。我也只希望能够执行'system'和'call'类命令。我需要在manager.conf中将我的用户设置为:

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

仅供参考-您可能正在重新发明车轮。除非你创建你自己的AMI库作为练习,否则你可能想考虑使用.< /P>谢谢Matt。事实上,我对TCP并不陌生。我不熟悉.NET套接字编程。