问题描述
我希望开发一个WCF的语音聊天应用程序。
I'm looking to develop a WCF voice chat application.
我想使用WCF服务来构建应用程序
I would like to use WCF service to build the application
| Invite |
| --------------------------------> |
| OK |
| <-------------------------------- |
| |
| --------------------------------> |
| Audio flow |
| <-------------------------------- |
| Bye |
| --------------------------------> |
A B
在其他的方式同一个应用程序应该有监听服务,所以它接收呼叫和发送呼叫到其他机器上的其他应用程序一样。 (无需要额外的服务主机应用程序接收呼叫)。
in other way the same application should have the listening service, so it receives calls and sends calls to the other same application on the other machine. (Without the need for an extra service host application to receives calls).
我知道也许我应该使用像套接字或一个特定的协议等传输声音,但我会查看以后。
I know maybe i should use a specific protocols like sockets or other for transferring voice, but i will check about that later.
那么什么样的WCF服务,我应该使用,以及什么类型的结合将是最好的用于这一目的。
So what kind of WCF service should i use, and what type of binding would be the best for this purpose.
推荐答案
作为对您的问题评论说,UDP或其他流媒体协议通常是首选。但与WCF提供更多的抽象,会更容易做这样的事情。说了这么多我实现了一个声音在我们以前的软件中的一个聊天功能(该软件用于局域网中的多个客户端)与WCF和 NetTcpBinding的
,和它的工作好(我在一个局域网5个客户测试了),你不能真的觉得WCF阻碍架构。它会与WCF 4.5的发布,这将是支持UDP传输有趣。
As the comment on your question says, UDP or other streaming protocols are usually preferred. But with WCF providing more abstraction, would be easier to do something like this. Having said that I have implemented a voice chatting feature on one of our previous software (the software was used by multiple clients in a LAN) with WCF and netTcpBinding
, and it worked well (I tested it with 5 clients in a LAN), you couldn't really feel that WCF hindered the architecture. It will get interesting with the release of WCF 4.5 which will be supporting UDP transport.
有关这样的事情,你应该得到双工通信的要点在WCF。我约了用这样的方式,我对我的服务 SendVoice()
经营合同做,每次客户端发送的asudio流服务时, SendVoice()
方法将通过用户(连接的客户端)的列表中迭代,并调用 SendVoiceCallback()
每个客户端上。你可以像这样开始:
For something like this, you should really get the gist of duplex communication in WCF. I went about with doing in such a way that i had a SendVoice()
operation contract on my service, everytime a client sent their asudio stream to the service, the SendVoice()
method would iterate through the list of subscribers (connected clients) and invoke the SendVoiceCallback()
on each client. You could start with something like this:
public void SendVoice(byte[] audio)
{
//Keep a list of connected clients in a dictionary called subscribers
//lock your subscribers list so that it's not modified when you're in the middle of sending the stream
lock (subscribers)
{
//send the received voice stream to each client
foreach (var _subscriber in subscribers)
{
if (OperationContext.Current.GetCallbackChannel<IVoiceChatCallback>() == subscriber.Key)
{
//if the person who sent the video is the current subscriber then don't send the video to THAT subscriber
continue;
}
try
{
//Send the received stream to the client asynchronously
_subscriber.Key.BeginOnVoiceSendCallback(audio, onNotifyCompletedVoiceSend, _subscriber.Key);
}
catch (Exception)
{
//fault handling
}
}
}
}
您的客户会那么上述定期调用该方法,我在执行我设置为每250毫秒,它完美运作良好(显然出现了一个小滞后)。
Your clients would then call the method above periodically, in my implementation I set this to every 250 milliseconds and it worked perfectly well (obviously there was a small lag).
在上面 IVoiceChatCallback
中的代码是回调合同。回调使服务调用客户端上的一些操作,让一个isntance客户端会像一台服务器。
异步服务电话将意味着你的 SendVoice()
将发布音频到每一个客户端异步,而不是等待前一个流发送之前到达客户端新的数据流。
In the code above IVoiceChatCallback
is the callback contract. Callbacks enable the service to invoke some operation on the clients, so for an isntance the client will behave like a server.Asynchronous service calls will mean that your SendVoice()
will publish the audio to every client asynchronously, rather than waiting for the previous stream to reach the client before sending the new stream.
上面的代码是你开始只是一个想法。你应该添加一些错误处理代码,以服务来检查时,客户端断开连接,然后从用户词典中删除它们。同样,你应该成为舒适与WCF异步操作调用以及回调。此外,如果你正在寻找在局域网中使用这一点,那么 NetTcpBinding的
是要走的路,你应该配置您的服务以优化吞吐量和并发性。在互联网上,你会希望使用HTTP绑定,支持全双工。
The code above is just an idea for you to get started. You should add some fault handling code to the service to check when clients are disconnected and remove them from your subscribers dictionary. Again You should become comfortable with asynchronous operation calls in WCF as well as callbacks. Also if you're looking to use this in LAN, then netTcpBinding
is the way to go, and you should configure your service as to optimise its throughput and concurrency. Over the internet, you'd want to use a Http Binding that supports duplex.
这篇关于如何启动一个WCF语音聊天应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!