我已经通过cocoapods从https://github.com/chebur/pjsip添加了pjsip lib。
不,我正在尝试使用this pjsip示例连接到sip服务器。
这是我的代码:
func startPjsip() {
var status: pj_status_t
// Create pjsua first
status = pjsua_create()
if status != Int32(PJ_SUCCESS.rawValue) {
pjsuaError("Error in pjsua_create()", status: status)
}
// Init Pjsua
var config = pjsua_config()
pjsua_config_default(&config)
// on reg state
config.cb.on_reg_state = { (accountId: pjsua_acc_id) in
p("on_reg_state", accountId)
}
// Init the logging config structure
var logConfig = pjsua_logging_config()
pjsua_logging_config_default(&logConfig)
logConfig.console_level = 4
// Init the pjsua
status = pjsua_init(&config, &logConfig, nil)
if status != Int32(PJ_SUCCESS.rawValue) {
pjsuaError("Error in pjsua_init()", status: status)
}
// Adding UDP transport
// Init transport config structure
var udpTransportConfig = pjsua_transport_config()
pjsua_transport_config_default(&udpTransportConfig)
udpTransportConfig.port = 5060
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &udpTransportConfig, nil)
if status != Int32(PJ_SUCCESS.rawValue) {
pjsuaError("Error in creating UDP transport", status: status)
}
// Initialization is done, now start pjsua
status = pjsua_start()
if status != Int32(PJ_SUCCESS.rawValue) {
pjsuaError("Error starting pjsua", status: status)
}
// Register the account on local sip server
var accountConfig = pjsua_acc_config()
pjsua_acc_config_default(&accountConfig)
// username
let username = "10001"
let domain = "voip01.interpreters.travel"
let userId = "sip:\(username)@\(domain)"
accountConfig.id = pj_str_t(ptr: stringToPointer(string: userId), slen: pj_ssize_t(userId.characters.count))
// uri
var uri = "sip:\(domain)"
accountConfig.reg_uri = pj_str_t(ptr: stringToPointer(string: uri), slen: pj_ssize_t(uri.characters.count))
accountConfig.cred_count = 1
accountConfig.cred_info.0.realm = pj_str_t(ptr: stringToPointer(string: "*"), slen: pj_ssize_t("*".characters.count))
let scheme = "digest"
accountConfig.cred_info.0.scheme = pj_str_t(ptr: stringToPointer(string: scheme), slen: pj_ssize_t(scheme.characters.count))
accountConfig.cred_info.0.data_type = Int32(PJSIP_CRED_DATA_PLAIN_PASSWD.rawValue)
let password = "10001"
accountConfig.cred_info.0.data = pj_str_t(ptr: stringToPointer(string: password), slen: pj_ssize_t(password.characters.count))
accountConfig.cred_info.0.username = pj_str_t(ptr: stringToPointer(string: username), slen: pj_ssize_t(username.characters.count))
var accountId = pjsua_acc_id()
status = pjsua_acc_add(&accountConfig, pj_bool_t(PJ_TRUE.rawValue), &accountId)
p(accountId)
if status != Int32(PJ_SUCCESS.rawValue) {
pjsuaError("Error adding account", status: status)
}
}
func pjsuaError(_ message: String, status: pj_status_t) {
let url = NSURL(fileURLWithPath: #file)
let fileName: String = url.lastPathComponent == nil ? #file : url.lastPathComponent!
pjsua_perror(fileName, message, status)
pjsua_destroy()
}
func stringToPointer(string: String) -> UnsafeMutablePointer<Int8> {
let cs = (string as NSString).utf8String
return UnsafeMutablePointer<Int8>(mutating: cs!)
}
当我运行应用程序时,我有请求发送到sip服务器,它返回401 Unauthorized (log is here),根据此doc.,这似乎没问题 之后,pjsip发送另一个请求并sip服务器返回403 Forbidden,here is the log。
我已经问过我的服务器人员有什么问题,他说他使用与JsSIP lib相同的凭据用于浏览器客户端,一切正常。 Here is the log他给了我。
我查看了他的日志,看到这些行的不同之处: 我的 - 通过:SIP / 2.0 / UDP 他的 - 通过:SIP / 2.0 / WSS
所以我似乎使用了错误的传输协议。但我不知道如何设置pjsip来使用wss协议。
如果您有同样的问题,有人可以帮助我。或者我需要阅读/尝试实现我的目标。
更新
我添加了Starscream,并让它连接到wss:// url。这是代码:
var socket: WebSocket!
override func viewDidLoad() {
super.viewDidLoad()
socket = WebSocket(url: URL(string: "wss://voip01.interpreters.travel:4443")!)
socket.delegate = self
socket.pongDelegate = self
socket.headers["Sec-WebSocket-Protocol"] = "sip"
socket.connect()
}
func websocketDidConnect(socket: WebSocket) {
print("websocketDidConnect")
print(socket)
}
它说“websocketDidConnect”。但现在我需要通过此SIP REGISTER
传输协议发送wss
。我找不到任何有关如何设置pjsip以使用wss
的信息。
我尝试使用pjsua_transport_create
或PJSIP_TRANSPORT_TCP
更改TLS
,但没有成功。
请有人帮助我找到有关pjsip + websocket支持的信息。