由于工作需求,本人无奈从android开发,临时调去研究sip服务器,后找到了一款sip服务器软件,经过不懈的努力,终于完成公司的sip业务需求!
后来意外发现了JAINSIP,便来了兴趣,就研究了一下,小有所感,编写成博客,以便参考!
其实sip服务器,就相当于一个socket服务器,但是有点差别。
Sip.kotlin
import java.util.*
import javax.sip.*
import javax.sip.address.AddressFactory
import javax.sip.header.HeaderFactory
import javax.sip.message.MessageFactory
import javax.sip.message.Request
import kotlin.collections.HashMap
abstract class Sip : SipListener {
var sipstack: SipStack//监听端口对象
var hf: HeaderFactory//头部对象
var af: AddressFactory//地址对象
var mf: MessageFactory//消息体对象
var sipProvider: SipProvider//sip信息监听对象
var userInfo=HashMap<String,String>()//用户信息 key:账号value:密码
val address="192.168.3.5"//服务器地址
val prot=5060//服务器端口
init {
//添加账号信息
userInfo.put("1","123456")
userInfo.put("2","123456")
userInfo.put("3","123456")
userInfo.put("4","123456")
//配置一下日志信息
val prop = Properties()
prop.setProperty("javax.sip.STACK_NAME", "teststackname")
prop.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32")
prop.setProperty("gov.nist.javax.sip.DEBUG_LOG", "siptestdebug.txt")
prop.setProperty("gov.nist.javax.sip.SERVER_LOG", "siptestlog.txt")
val sf = SipFactory.getInstance()
sf.pathName = "gov.nist"
sipstack = sf.createSipStack(prop)
sf.createHeaderFactory()
hf = sf.createHeaderFactory()
af = sf.createAddressFactory()
mf = sf.createMessageFactory()
val listeningPoint = sipstack.createListeningPoint(address, prot, "udp")//启动sip的udp服务端
sipProvider = sipstack.createSipProvider(listeningPoint)
sipProvider.addSipListener(this)//设置sip的消息监听
println("sipf服务已启动")
}
/**
*@Description 注册函数
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 14:20
*/
abstract fun doRegister(requestEvent:RequestEvent)
/**
*@Description 处理invite消息
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 14:35
*/
abstract fun doInvite(requestEvent: RequestEvent)
/**
*@Description 处理通话过程中的信息
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/14
*@Time 14:33
*/
abstract fun doProcessResponse(responseEvent: ResponseEvent)
/**
* 当出现异步消息传输错误时,SIP堆栈调用此方法。
*/
override fun processIOException(p0: IOExceptionEvent?) {
System.err.println(p0?.transport)
// println("processIOException$p0")
}
/**
*@Description 此方法在对话(会话)结束时由SIP堆栈调用。
*@Param [dialogTerminatedEvent]
*@Return void
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 0:38
*/
override fun processDialogTerminated(p0: DialogTerminatedEvent?) {
println("processDialogTerminated$p0")
}
/**
* SIP服务端接收消息的方法
* Content 里面是GBK编码
* This method is called by the SIP stack when a new request arrives.
*/
override fun processRequest(requestEvent: RequestEvent) {
System.err.println("processRequest:${requestEvent.request.method.toUpperCase()}")
if (requestEvent.request==null) {
println("无效的processRequest请求")
return
}
when(requestEvent.request.method.toUpperCase()){
Request.INVITE->{
doInvite(requestEvent)
}
Request.REGISTER->{
doRegister(requestEvent)
}
Request.SUBSCRIBE->{
}
Request.ACK->{
}
Request.BYE->{
}
Request.CANCEL->{
}
else->{
println("未知的:" + requestEvent.request.method.toUpperCase())
}
}
}
/**
* sip客户端回复消息
*/
override fun processResponse(p0: ResponseEvent) {
doProcessResponse(p0)
}
/**
* 当消息没有应答时,SIP堆栈调用此方法。请注意,这与错误消息的处理方式不同。
*/
override fun processTimeout(p0: TimeoutEvent?) {
println("processTimeout$p0")
}
/**
*@Description 此方法在事务结束时由SIP堆栈调用。
*@Param [transactionTerminatedEvent]
*@Return void
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 0:38
*/
override fun processTransactionTerminated(p0: TransactionTerminatedEvent?) {
println("processTransactionTerminated$p0")
}
}
class SipManager:Sip() {
/**
*@Description 处理注册消息
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 14:35
*/
override fun doRegister(requestEvent: RequestEvent){
}
/**
*@Description 处理请求事件
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/9
*@Time 14:36
*/
override fun doInvite(requestEvent: RequestEvent) {
}
/**
*@Description 处理通话过程中的信息交互
*@Param
*@Return
*@Author Mr.Ren
*@Date 2019/11/14
*@Time 14:34
*/
override fun doProcessResponse(responseEvent: ResponseEvent) {
}
}
//启动sip服务器
companion object {
@JvmStatic
fun main(args: Array<String>) {
SipManager()
}
}
}
这样就启动好一个sip服务器了,下篇文章,我们将学会,如何验证用户端的连接,确保连接有效。