对于简单的服务器端的扩充以及修改注册表等,那些比较菜的方法这里就不说了,还是说点有技术含量的吧,你要在OCS上面开发,首先得对OCS的架构有一个大致的了解,如果不了解呢,就直接参考网上的一些资料和信息了。了解大致的框架,然后开始看SDK里面的开发包的介绍,原来好像写过一篇文章,仔细找找。
开发过程当中基本按照前面的构架流程来做,而且可以大部分参考SDK里面的DEMO程序来做,但是有几个需要注意的地方,一个是证书的获取,默认的OCS服务器会有一个证书来验证用户的身份,原来本想直接用默认的身份验证来代替,但是个人实验没有成功,所以这里必须有获取证书的方法。而且还需要注意的就是证书的获取这里默认是获取本地自己的证书的,因为这里个人开发的程序是直接部署在OCS的服务器上面的,如果是部署在域里面的其他服务器上面,那么里面就要做相应的修改了,下面贴函数(该死的天涯没有专业的code标,代码缩进完全变形,只有凑合看了):
///
/// 获得本地证书
///
/// 输出信息
/// 是否成功
public bool GetCertificateDictionary(out String m_message)
{
m_message = String.Empty;
bool m_flag = false;
//Create new X509 store from the local certificate store.
X509Store Store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
//Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly | OpenFlags.MaxAllowed );
Store.Open(OpenFlags.MaxAllowed);
m_DictionaryOfCertificates.Clear();
int i = 0;
CertificatesNameList = new string[Store.Certificates.Count];
foreach (X509Certificate2 ctf in Store.Certificates)
{
String CertificateId = ctf.SerialNumber;
String issue=ctf.Issuer;
//ComboboxCertificate.Items.Add(CertificateId);
CertificatesNameList.SetValue(CertificateId, i);
i++;
m_DictionaryOfCertificates.Add(CertificateId, ctf);
m_flag = true;
}
Store.Close();
if (m_flag == false)
{
m_message = "Please ensure the certificate you want to use is loaded into the personal folder of the local computer store.";
}
return m_flag;
}
catch (System.Security.SecurityException e)
{
m_message = "You don't have permissions to enumerate the certificates from the local computer store";
return m_flag;
}
}
有了证书,后面的事情就好办多了,无非就是初始化连接manage,初始化endpoint然后加载信息到session,然后发送就ok了,broadcastI。事例里面是直接将当前的需要的用户加入到了广播的session里面,然后统一开了一个后台的线程开始不行发信息。这里需要注意的几个地方:首先要保证你的ocs的服务器的tls和tcp通信都可正常连接,并且端口都是设定ok的,而且受信任的证书也要搞定,原来ocs2005的话需要原来有一个专门的证书服务器来颁发证书,ocs2007里面就已经直接集成了。
下面开始贴无脑代码,大部分仿照IM的例子里面东西来写的。
private void RegisterEndpoint()
{
// Create the connection manager
RecordProgress("Creating the connection manager");
if (Settings.TransportType == SipTransportType.Tls)
{
try
{
_connectionManager = (RealTimeServerConnectionManager)new RealTimeServerTlsConnectionManager(Settings.CertificateIssuerName, Settings.CertificateSerialNumber);
RecordProgress("Created the Tls connection manager");
}
catch (TlsFailureException)
{
Error("Cannot read the certificate");
return;
}
_endPoint = new SipEndpoint("sip:administrator@domcool.net",
SipAuthenticationProtocols.Ntlm,
SipTransportType.Tls,
"ocs.domcool.net",
5061,
true,
_connectionManager,
null);
_endPoint.CredentialCache.Add(SipEndpoint.DefaultRtcRealm, fromCredential);
RecordProgress("Created a Tls endpoint for {0}", Settings.Uri);
}
else
{
_connectionManager = (RealTimeServerConnectionManager)new RealTimeServerTcpConnectionManager();
RecordProgress("Created the Tcp connection manager");
_endPoint = new SipEndpoint(Settings.Uri,
SipAuthenticationProtocols.Ntlm,
SipTransportType.Tcp,
Settings.OCSServerName,
5060,
true,
_connectionManager,
null);
_endPoint.CredentialCache.Add(SipEndpoint.DefaultRtcRealm, CredentialCache.DefaultNetworkCredentials);
RecordProgress("Created a Tcp endpoint for {0}", Settings.Uri);
}
_endPoint.BeginRegister(new AsyncCallback(RegisterCallback), _endPoint);
}
private void RegisterCallback(IAsyncResult asyncResult) {
SipEndpoint endPoint = asyncResult.AsyncState as SipEndpoint;
SipResponseData response;
try {
response = endPoint.EndRegister(asyncResult);
RecordProgress("Register ok");
if (Started != null)
Started(this, EventArgs.Empty);
} catch { }
}
以html类型发送消息提供了二种方式,一种是以同步方式,一种是以异步方式,下面是同步方式。
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
endpoint.SendMessage(MessageType.Message,
target,
contentType,
msgBody);
}
catch (RealTimeException e)
{
// Handle exception.
}
异步方式:
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
endpoint.BeginSendMessage(MessageType.Message,
target,
contentType,
msgBody,
CompleteSendMessage,
endpoint);
}
catch (RealTimeException e)
{
// Handle exception here.
}
// Async callback function to end the BeginSendMessage operation.
void CompleteSendMessage(AsyncCallback result)
{
RealTimeEndpoint ep = ar.AsyncState as RealTimeEndpoint;
SipResponseData data;
try
{
data = ep.EndSendMessage(ar);
}
catch (FailureResponseException exp)
{
// Handle a failure response
Console.WriteLine("Failure response for {0}: {1}",
ep.Uri,
exp.ToString());
}
catch (OperationTimeoutException exp)
{
// Operation timed out
Console.WriteLine("Operation timed out for {0}: {1}",
ep.Uri,
exp.ToString());
}
catch (RealTimeException exp)
{
// Handle exception.
Console.WriteLine("Invite Failure for {0}: {1}",
ep.Uri,
exp.ToString());
}
}
关键的大致就是这样一些代码,归结一下整个消息发送的过程:就是创建连接manage,创建endpoint,在连接池里面注册你的endpoint,然后创建session,附加你的消息到session里面,然后通过session把消息发送出去。对于简单的文本的消息处理大致就是这样,ocs还有一大主要的利用是在语音和视频会议上面,由于哪一部分的没有搞过,这里就没有研究,不过相信会有接口提供相应的支持。详细信息请参考SDK!
开发过程当中基本按照前面的构架流程来做,而且可以大部分参考SDK里面的DEMO程序来做,但是有几个需要注意的地方,一个是证书的获取,默认的OCS服务器会有一个证书来验证用户的身份,原来本想直接用默认的身份验证来代替,但是个人实验没有成功,所以这里必须有获取证书的方法。而且还需要注意的就是证书的获取这里默认是获取本地自己的证书的,因为这里个人开发的程序是直接部署在OCS的服务器上面的,如果是部署在域里面的其他服务器上面,那么里面就要做相应的修改了,下面贴函数(该死的天涯没有专业的code标,代码缩进完全变形,只有凑合看了):
///
/// 获得本地证书
///
/// 输出信息
/// 是否成功
public bool GetCertificateDictionary(out String m_message)
{
m_message = String.Empty;
bool m_flag = false;
//Create new X509 store from the local certificate store.
X509Store Store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
//Store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly | OpenFlags.MaxAllowed );
Store.Open(OpenFlags.MaxAllowed);
m_DictionaryOfCertificates.Clear();
int i = 0;
CertificatesNameList = new string[Store.Certificates.Count];
foreach (X509Certificate2 ctf in Store.Certificates)
{
String CertificateId = ctf.SerialNumber;
String issue=ctf.Issuer;
//ComboboxCertificate.Items.Add(CertificateId);
CertificatesNameList.SetValue(CertificateId, i);
i++;
m_DictionaryOfCertificates.Add(CertificateId, ctf);
m_flag = true;
}
Store.Close();
if (m_flag == false)
{
m_message = "Please ensure the certificate you want to use is loaded into the personal folder of the local computer store.";
}
return m_flag;
}
catch (System.Security.SecurityException e)
{
m_message = "You don't have permissions to enumerate the certificates from the local computer store";
return m_flag;
}
}
有了证书,后面的事情就好办多了,无非就是初始化连接manage,初始化endpoint然后加载信息到session,然后发送就ok了,broadcastI。事例里面是直接将当前的需要的用户加入到了广播的session里面,然后统一开了一个后台的线程开始不行发信息。这里需要注意的几个地方:首先要保证你的ocs的服务器的tls和tcp通信都可正常连接,并且端口都是设定ok的,而且受信任的证书也要搞定,原来ocs2005的话需要原来有一个专门的证书服务器来颁发证书,ocs2007里面就已经直接集成了。
下面开始贴无脑代码,大部分仿照IM的例子里面东西来写的。
private void RegisterEndpoint()
{
// Create the connection manager
RecordProgress("Creating the connection manager");
if (Settings.TransportType == SipTransportType.Tls)
{
try
{
_connectionManager = (RealTimeServerConnectionManager)new RealTimeServerTlsConnectionManager(Settings.CertificateIssuerName, Settings.CertificateSerialNumber);
RecordProgress("Created the Tls connection manager");
}
catch (TlsFailureException)
{
Error("Cannot read the certificate");
return;
}
_endPoint = new SipEndpoint("sip:administrator@domcool.net",
SipAuthenticationProtocols.Ntlm,
SipTransportType.Tls,
"ocs.domcool.net",
5061,
true,
_connectionManager,
null);
_endPoint.CredentialCache.Add(SipEndpoint.DefaultRtcRealm, fromCredential);
RecordProgress("Created a Tls endpoint for {0}", Settings.Uri);
}
else
{
_connectionManager = (RealTimeServerConnectionManager)new RealTimeServerTcpConnectionManager();
RecordProgress("Created the Tcp connection manager");
_endPoint = new SipEndpoint(Settings.Uri,
SipAuthenticationProtocols.Ntlm,
SipTransportType.Tcp,
Settings.OCSServerName,
5060,
true,
_connectionManager,
null);
_endPoint.CredentialCache.Add(SipEndpoint.DefaultRtcRealm, CredentialCache.DefaultNetworkCredentials);
RecordProgress("Created a Tcp endpoint for {0}", Settings.Uri);
}
_endPoint.BeginRegister(new AsyncCallback(RegisterCallback), _endPoint);
}
private void RegisterCallback(IAsyncResult asyncResult) {
SipEndpoint endPoint = asyncResult.AsyncState as SipEndpoint;
SipResponseData response;
try {
response = endPoint.EndRegister(asyncResult);
RecordProgress("Register ok");
if (Started != null)
Started(this, EventArgs.Empty);
} catch { }
}
以html类型发送消息提供了二种方式,一种是以同步方式,一种是以异步方式,下面是同步方式。
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
endpoint.SendMessage(MessageType.Message,
target,
contentType,
msgBody);
}
catch (RealTimeException e)
{
// Handle exception.
}
异步方式:
RealTimeEndpoint endpoint = ...; // Assumed to be created elsewhere
RealTimeAddress target = new RealTimeAddress("sip:jayanama@contoso.com");
string msg = "Hi!";
ContentType contentType = new ContentType("text/plain; charset=UTF-8");
byte[] msgBody = Encoding.UTF8.GetBytes(msg);
try
{
endpoint.BeginSendMessage(MessageType.Message,
target,
contentType,
msgBody,
CompleteSendMessage,
endpoint);
}
catch (RealTimeException e)
{
// Handle exception here.
}
// Async callback function to end the BeginSendMessage operation.
void CompleteSendMessage(AsyncCallback result)
{
RealTimeEndpoint ep = ar.AsyncState as RealTimeEndpoint;
SipResponseData data;
try
{
data = ep.EndSendMessage(ar);
}
catch (FailureResponseException exp)
{
// Handle a failure response
Console.WriteLine("Failure response for {0}: {1}",
ep.Uri,
exp.ToString());
}
catch (OperationTimeoutException exp)
{
// Operation timed out
Console.WriteLine("Operation timed out for {0}: {1}",
ep.Uri,
exp.ToString());
}
catch (RealTimeException exp)
{
// Handle exception.
Console.WriteLine("Invite Failure for {0}: {1}",
ep.Uri,
exp.ToString());
}
}
关键的大致就是这样一些代码,归结一下整个消息发送的过程:就是创建连接manage,创建endpoint,在连接池里面注册你的endpoint,然后创建session,附加你的消息到session里面,然后通过session把消息发送出去。对于简单的文本的消息处理大致就是这样,ocs还有一大主要的利用是在语音和视频会议上面,由于哪一部分的没有搞过,这里就没有研究,不过相信会有接口提供相应的支持。详细信息请参考SDK!