我正在尝试在C#SIP客户端项目中使用来自Codeplex的Sip魔术来向我的服务器发送和接收请求和响应。当我使用SIPUDPChannel.Send('remoteEP','msg')时,可以看到SUBSCRIBE数据包已发送到我的服务器,并且我的服务器以状态响应进行应答。
但是,如何使用SIP Sorcery API捕获响应/请求事件呢?
为了回答您在评论中发布的第二个问题,即第二个SUBSCRIBE请求为何在对话中(或更正确地说是在交易中);它不是。
但是,由于您正在重新使用从响应到第一个SUBSCRIBE请求的响应中获得的SIP标头,因此最终会以某种方式设置某些标头,使其看起来像是现有事务的一部分。
正确的方法是重新使用原始SIP请求中的标头,并递增CSeq并重新生成CallID。下面的代码类似(请注意,我没有编译它来检查语法错误)。
static SIPTransport Transport;
static SIPMessage msg;
static string inputMsg;
static SIPUDPChannel channel;
static SIPRequest subscribeRequest;
static void Main(string[] args)
{
InitializeSIP();
Console.Read();
}
static void InitializeSIP()
{
//ResolveIPEndPoint
SIPEndPoint sipep = new SIPEndPoint(new IPEndPoint(IPAddress.Parse("192.168.102.12"), 5060));
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.102.12"), 5060);
//Set Transport
Transport = new SIPTransport(SIPDNSManager.ResolveSIPService, new SIPTransactionEngine());
//IPEndPoint
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.100.10"), 8080);
//Create Channel object
channel = new SIPUDPChannel(localEndPoint);
Transport.AddSIPChannel(channel);
//Wire transport with incoming requests
Transport.SIPTransportRequestReceived += new SIPTransportRequestDelegate(Transport_SIPTransportRequestReceived);
//Wire transport with incoming responses
Transport.SIPTransportResponseReceived += new SIPTransportResponseDelegate(Transport_SIPTransportResponseReceived);
inputMsg = "SUBSCRIBE sip:[email protected] SIP/2.0" + SIPConstants.CRLF +
"Via: SIP/2.0/UDP 192.168.100.10:8080;rport;branch=z9hG4bKFBB7EAC06934405182D13950BD51F001" + SIPConstants.CRLF +
"From: <sip:[email protected]>;tag=196468136" + SIPConstants.CRLF +
"To: <sip:[email protected]>" + SIPConstants.CRLF +
"Contact: <sip:subscribeUser@>" + SIPConstants.CRLF +
"Call-ID: 1337505490-453410046-705466123" + SIPConstants.CRLF +
"CSeq: 1 SUBSCRIBE" + SIPConstants.CRLF +
"Max-Forwards: 70" + SIPConstants.CRLF +
"Event: Presence" + SIPConstants.CRLF +
"Content-Length: 0";
subscribeRequest = SIPRequest.ParseSIPRequest(inputMsg);
channel.Send(remoteEP, subscribeRequest.ToString());
}
static void Transport_SIPTransportResponseReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponse sipResponse)
{
Console.WriteLine("Response method: " + sipResponse.Header.CSeqMethod);
if(sipResponse.StatusCode == 401 && sipResponse.Header.CSeqMethod == SIPMethodsEnum.SUBSCRIBE)
{
//Resubscribe with Digist
//SIP Header
SIPHeader header = subscribeRequest.Header;
header.CSeq++;
header.CallID = "some_new_callID";
header.AuthenticationHeader = sipResponse.Header.AuthenticationHeader;
header.Expires = 120;
//New Request
SIPRequest request = new SIPRequest(SIPMethodsEnum.SUBSCRIBE, new SIPURI(SIPSchemesEnum.sip, remoteEndPoint));
request.LocalSIPEndPoint = localSIPEndPoint;
request.RemoteSIPEndPoint = remoteEndPoint;
request.Header = header;
//Send request
channel.Send(remoteEndPoint.GetIPEndPoint(), request.ToString());
}
else
Console.WriteLine(string.Format("Error {0} {1}.", sipResponse.StatusCode, sipResponse.Status));
}
static void Transport_SIPTransportRequestReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest)
{
Console.WriteLine("Request body: " + sipRequest.Body);
}
更新:
您将凭据添加到SIP请求中,如下面的代码所示。注意,认证头基于每个请求而不是每个传输或每个信道。在sipsorcery代码中,SIPTransport可以具有多个SIPChannel,而SIPChannel可以与多个SIP端点进行通信,因此在传输或通道上设置凭据没有意义。
SIPAuthorisationDigest authDigest = sipResponse.Header.AuthenticationHeader.SIPDigest;
authDigest.SetCredentials(username, password, uri, SIPMethodsEnum.INVITE.ToString());
authRequest.Header.AuthenticationHeader = new SIPAuthenticationHeader(authDigest);
authRequest.Header.AuthenticationHeader.SIPDigest.Response = authDigest.Digest;
更新2:
发送对SIP请求的简单响应的最简单方法如下。
static void Transport_SIPTransportRequestReceived(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest)
{
SIPResponse response = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null);
_sipTransport.SendResponse(response);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句