我正在使用IceLink库进行对等通信。我们需要为此部署两个服务器IceLink和WebSync。
http://docs.frozenmountain.com/icelink2/index.html#class=icelink-getting-started-creating-a-conference-10_ios-macosx
但是我想使用XMPP而不是WebSync。
现在,以下代码用于WebSync,我只想替换它,以便在使用此WebSync时可以改用XMPP。
[client addOnStreamFailureWithValueBlock:^(FMWebSyncStreamFailureArgs *e)
{
[conference unlinkAll];
}];
// Add a couple event handlers to the conference to send
// generated offers/answers and candidates to a peer.
// The peer ID is something we define later. In this case,
// it represents the remote WebSync client ID. WebSync's
// "notify" method is used to send data to a specific client.
[conference addOnLinkOfferAnswerWithValueBlock:^(FMIceLinkLinkOfferAnswerArgs *e)
{
[client notifyWithNotifyArgs:[FMWebSyncNotifyArgs notifyArgsWithClientId:[FMGuid guidWithG:e.peerId]
dataJson:[e.offerAnswer toJson]
tag:@"offeranswer"]];
}];
[conference addOnLinkCandidateWithValueBlock:^(FMIceLinkLinkCandidateArgs *e)
{
[client notifyWithNotifyArgs:[FMWebSyncNotifyArgs notifyArgsWithClientId:[FMGuid guidWithG:e.peerId]
dataJson:[e.candidate toJson]
tag:@"candidate"]];
}];
// Add an event handler to the WebSync client to receive
// incoming offers/answers and candidates from a peer.
// Call the "receiveOfferAnswer" or "receiveCandidate"
// method to pass the information to the conference.
[client addOnNotifyWithValueBlock:^(FMWebSyncNotifyReceiveArgs *e)
{
NSString *peerId = [e.notifyingClient.clientId toString];
NSObject *peerState = e.notifyingClient.boundRecords;
if ([e.tag isEqualToString:@"offeranswer"])
{
[conference receiveOfferAnswerWithOfferAnswer:[FMIceLinkOfferAnswer fromJsonWithOfferAnswerJson:e.dataJson]
peerId:peerId
peerState:peerState];
}
else if ([e.tag isEqualToString:@"candidate"])
{
[conference receiveCandidateWithCandidate:[FMIceLinkCandidate fromJsonWithCandidateJson:e.dataJson]
peerId:peerId];
}
}];
// Subscribe to a WebSync channel. When another client joins the same
// channel, create a P2P link. When a client leaves, destroy it.
FMWebSyncSubscribeArgs *subscribeArgs = [FMWebSyncSubscribeArgs subscribeArgsWithChannel:@"/mychat"];
[subscribeArgs setOnSuccessBlock:^(FMWebSyncSubscribeSuccessArgs *e)
{
[self writeLine:@"-- Subscribed to %@.", e.channel];
}];
[subscribeArgs setOnFailureBlock:^(FMWebSyncSubscribeFailureArgs *e)
{
[self writeLine:@"-- Could not subscribe to %@. %@", e.channel, e.exception.message];
}];
[subscribeArgs setOnReceiveBlock:^(FMWebSyncSubscribeReceiveArgs *e) { }];
[subscribeArgs setOnClientSubscribeWithOnClientSubscribeBlock:^(FMWebSyncSubscribersClientSubscribeArgs *e)
{
NSString *peerId = [e.subscribedClient.clientId toString];
NSObject *peerState = e.subscribedClient.boundRecords;
[conference linkWithPeerId:peerId peerState:peerState];
}];
[subscribeArgs setOnClientUnsubscribeWithOnClientUnsubscribeBlock:^(FMWebSyncSubscribersClientUnsubscribeArgs *e)
{
NSString *peerId = [e.unsubscribedClient.clientId toString];
[conference unlinkWithPeerId:peerId];
}];
[client subscribeWithSubscribeArgs:subscribeArgs];
最佳答案
毫无疑问。无论如何,作为对未来读者的一般回答,您可以使用客户端之间的任何通信方式。您甚至可以通过电子邮件发送和复制/粘贴通过信令通道发送的信息(实际上,我为最早的WebRTC做的事情就是这样做,以避免拥有信令服务器的复杂性)。
您只需使用您的信令通道来交换以下信息(我使用的是C#命名,但其他语言应具有相似的名称,但要遵循语言约定):
一个对等方的要约(通过调用OnLinkOfferAnswer
一旦通过回调Conference.Link
生成)将发送给另一对等方。要呼叫Conference.Link
,您需要知道您要连接的对象的对等ID,因此可能需要您的信令服务器将数据发送给我们(就我而言,我实际上已连接到中间服务器进行记录,因此可以使用硬编码的“服务器”对等ID)。
该对等方的答案(在您向OnLinkOfferAnswer
注册要约后,通过回调Conference.ReceiveOfferAnswer
生成)将发送到第一个对等方(并以相同的方式注册)。
两个对等方都将使用OnLinkCandidate
回调(多次)生成ICE候选对象,该回调必须发送到另一个对等方并在Conference.ReceiveCandidate
中注册。
请注意,所有这些操作都是异步发生的,因此可以在接收答案之前生成ICE候选对象。很好,因为IceLink将在内部对其进行管理。
您必须跟踪这意味着哪个对等方(要约/答案和候选者特定于对等方)。生成某种唯一的对等ID完全取决于您的信令库(此处为XMPP),以便您可以标识用户(如果您的信令库没有,则可以自己完成)。
交换的数据是JSON,但是您不必在典型用法中进行修改。 OfferAnswer
和Candidate
对象具有FromJson
和ToJson
方法来处理转换。