SipSorcery允许使用不同的端口进行注册和邀请,这可能会引起一些问题,如丢失邀请或挂断电话。一个简单的解决方法是配置SipSorcery在注册和邀请时使用相同的端口。以下是一个示例代码,将SipSorcery配置为使用相同的端口。
using SIPSorcery.SIP.App;
using SIPSorcery.SIP.Core;
using SIPSorcery.Sys;
//在SipSorcery Configuration中添加以下代码
DIContainer.Register<SIPTransport>(() => {
var sipTransport = SIPTransportConfig.ParseSIPTransportConfig(null);
sipTransport.AddSIPChannel(new SIPUDPChannel(new IPEndPoint(IPAddress.Any, 5060)));
return sipTransport;
});
DIContainer.Register<ISIPServerUserAgent>(() => new SIPServerUserAgent(SIPTransport.GetSIPTransport().SIPChannels, new SIPCallDescriptor()));
// 在Program.cs中添加以下代码
static void Main()
{
Console.WriteLine("Starting SIPSorcery");
AddConsoleLoggers();
// 标准初始化代码
var sipTransportTask = SIPTransport.GetSIPTransportTask(new SIPTransportConfig(false, null));
sipTransportTask.Start();
// 运行Demo代码
new SIPClient().MakeCall("sip:username@sipsorcery.com", "password", "sip:1234567890@sipsorcery.com", null);
Console.WriteLine("Press any key to stop SIPSorcery");
Console.ReadLine();
// 清理工作并退出
sipTransportTask.Stop();
SetExitStatus(0);
}
通过以上配置,SipSorcery将只使用5060端口,这可以避免端口引起的问题并简化配置。这个解决方法还可以根据需要进行修改和扩展。