A-A+

UCMA开发-SignalingSession

2016年04月29日 技术 暂无评论 阅读 3,686 次

索引请点击这里

本文的目的在于讲解如何创建、使用SignalingSession,为之后构建自动应答机器人做准备。

1. Constructors

名称 说明
SignalingSession(RealTimeEndpoint, RealTimeAddress) 使用端点(EndPoint)和目标初始化一个信令会话(SignalingSession)

2. Methods

名称 说明
BeginAccept(AsyncCallback, Object) 接受会话
BeginEstablish(AsyncCallback, Object) 建立会话
BeginParticipate(AsyncCallback, Object) 加入一个会话,这个方法需要调用(用户传入和传出消息)已建立的会话
BeginSendMessage(MessageType, ContentType, Byte [],AsyncCallback, Object) 发送一条消息,会话应该在连接状态
BeginSendMessage(MessageType, ContentType, Byte [],IEnumerable<SignalingHeader>, AsyncCallback, Object)
BeginTerminate(AsyncCallback, Object) 异步终止会话,本次会话将不再可用

3. Properties

名称 说明
OfferAnswerNegotiation 获取或设置由调用者实现并提供的应答协商接口
State 获取或设置会话的状态

4. Events

名称 说明
MessageReceived 收到消息时触发

5. Example

  1. //直接建立SignalingSession,并发送消息
  2. RealTimeAddress target = new RealTimeAddress(strRemoteUri);
  3. SignalingSession session = new SignalingSession(sipEndPoint, target);
  4. session.OfferAnswerNegotiation = this;
  5. try
  6. {
  7.     session.EndEstablish(session.BeginEstablish(nullnull));
  8.     break;
  9. }
  10. catch
  11. {
  12.     Thread.Sleep(10);
  13. }
  14. ContentType contentType = new ContentType("text/plain; charset=UTF-8");
  15. byte[] msgBody = Encoding.UTF8.GetBytes(strMessage);
  16. try
  17. {
  18.     session.SendMessage(
  19.         MessageType.Message,
  20.         contentType,
  21.         msgBody);
  22. }
  23. catch (Exception ex)
  24. {
  25.     throw ex;
  26. }
  27. session.EndTerminate(session.BeginTerminate(nullnull));
  28. //通过SipEndPoint的SessionReceived事件来建立
  29. void SipEndpoint_SessionReceived(object sender, SessionReceivedEventArgs e)
  30. {
  31.     e.Session.OfferAnswerNegotiation = this;
  32.     //开始参与该会话Session
  33.     try
  34.     {
  35.         e.Session.BeginParticipate(
  36.             new AsyncCallback(ParticipateCallback), e.Session);
  37.     }
  38.     catch
  39.     {
  40.     }
  41. }
  42. //参与处理回发事件
  43. void ParticipateCallback(IAsyncResult ar)
  44. {
  45.     SignalingSession session = ar.AsyncState as SignalingSession;
  46.     SipMessageData response = null;
  47.     try
  48.     {
  49.         response = session.EndParticipate(ar);
  50.         session.SendMessage(
  51.             MessageType.Message,
  52.             contentType,
  53.             msgBody);
  54.     }
  55.     catch
  56.     {
  57.     }
  58. }

关于“Session.OfferAnswerNegotiation = this;”的解释:

this其实代表了继承IOfferAnswer接口的类,在示例中,因为代码段所属的类继承并实现了IOfferAnswer接口的,所以可以直接将自己赋值给OfferAnswerNegotiation属性,IOfferAnswer的实现见下面的示例代码:

  1. #region IOfferAnswer 接口实现
  2. //Occurs when we receive and INVITE with no offer
  3. public ContentDescription GetAnswer(object sender, ContentDescription offer)
  4. {
  5.     return GetContentDescription((SignalingSession)sender);
  6. }
  7. //Occurs when we receive an invite with an offer
  8. public ContentDescription GetOffer(object sender)
  9. {
  10.     return GetContentDescription((SignalingSession)sender);
  11. }
  12. //Occurs in Reinvite cases
  13. public void HandleOfferInInviteResponse(object sender, OfferInInviteResponseEventArgs e)
  14. {
  15.     return;
  16. }
  17. //Occurs in Reinvite cases
  18. public void HandleOfferInReInvite(object sender, OfferInReInviteEventArgs e)
  19. {
  20.     return;
  21. }
  22. //Occurs when we initiate the invite
  23. public void SetAnswer(object sender, ContentDescription answer)
  24. {
  25.     SignalingSession session = sender as SignalingSession;
  26.     byte[] Answer = answer.GetBody();
  27.     if (Answer != null)
  28.     {
  29.         Sdp<SdpGlobalDescription, SdpMediaDescription> sessionDescription =
  30.             new Sdp<SdpGlobalDescription, SdpMediaDescription>();
  31.         if (!sessionDescription.TryParse(Answer))
  32.         {
  33.             session.BeginTerminate(null, session);
  34.             return;
  35.         }
  36.         else
  37.         {
  38.             IList<SdpMediaDescription> ActiveMediaTypes =
  39.                 sessionDescription.MediaDescriptions;
  40.             if ((ActiveMediaTypes.Count == 1) &&
  41.                 (ActiveMediaTypes[0].MediaName.Equals("message",
  42.                     StringComparison.Ordinal)) &&
  43.                 (ActiveMediaTypes[0].Port > 0) &&
  44.                 (ActiveMediaTypes[0].TransportProtocol.Equals("sip",
  45.                     StringComparison.OrdinalIgnoreCase)))
  46.             {
  47.             }
  48.             else
  49.             {
  50.                 session.BeginTerminate(null, session);
  51.             }
  52.         }
  53.     }
  54. }
  55. //Retrieves the content description for offers and answers
  56. private ContentDescription GetContentDescription(SignalingSession session)
  57. {
  58.     IPAddress ipAddress;
  59.     // This method is called back every time an outbound INVITE is sent.
  60.     if (session.Connection != null)
  61.     {
  62.         ipAddress = session.Connection.LocalEndpoint.Address;
  63.     }
  64.     else
  65.     {
  66.         ipAddress = IPAddress.Any;
  67.     }
  68.     Sdp<SdpGlobalDescription, SdpMediaDescription> sessionDescription =
  69.         new Sdp<SdpGlobalDescription, SdpMediaDescription>();
  70.     //Set the origin line of the SDP
  71.     //s, t, and v lines are automatically constructed
  72.     sessionDescription.GlobalDescription.Origin.Version = 0;
  73.     sessionDescription.GlobalDescription.Origin.SessionId = "0";
  74.     sessionDescription.GlobalDescription.Origin.UserName = "-";
  75.     sessionDescription.GlobalDescription.Origin.Connection.Set(ipAddress.ToString());
  76.     //Set the connection line
  77.     sessionDescription.GlobalDescription.Connection.TrySet(ipAddress.ToString());
  78.     SdpMediaDescription mditem = new SdpMediaDescription("message");
  79.     mditem.Port = 5061;
  80.     mditem.TransportProtocol = "sip";
  81.     mditem.Formats = "null";
  82.     SdpAttribute aitem = new SdpAttribute("accept-types""text/plain");
  83.     mditem.Attributes.Add(aitem);
  84.     //Append the Media description to the Global description
  85.     sessionDescription.MediaDescriptions.Add(mditem);
  86.     ContentType ct = new ContentType("application/sdp");
  87.     return new ContentDescription(ct, sessionDescription.GetBytes());
  88. }
  89. #endregion

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: