To manage a SIP dialog as a server, use sipgo.NewDialogServerCache. This allows you to read incoming requests (like Invite, Ack, or Bye) and associate them with an existing dialog session.
ua, _ := sipgo.NewUA()
srv, _ := sipgo.NewServer(ua)
client, _ := sipgo.NewClient(ua)
uasContact := sip.ContactHeader{
Address: sip.Uri{User: "test", Host: "127.0.0.200", Port: 5099},
}
dialogSrv := sipgo.NewDialogServerCache(client, uasContact)
// Handle incoming INVITE
srv.OnInvite(func(req *sip.Request, tx sip.ServerTransaction) {
dlg, err := dialogSrv.ReadInvite(req, tx)
if err != nil {
return
}
defer dlg.Close()
dlg.Respond(sip.StatusTrying, "Trying", nil)
dlg.Respond(sip.StatusOK, "OK", nil)
<-dlg.Context().Done()
})
// Handle ACK and BYE for the dialog
srv.OnAck(func(req *sip.Request, tx sip.ServerTransaction) {
dialogSrv.ReadAck(req, tx)
})
srv.OnBye(func(req *sip.Request, tx sip.ServerTransaction) {
dialogSrv.ReadBye(req, tx)
})