同步发送IM的简单示例 [英] Simple example to send IM synchonously
问题描述
我正在寻找一个向Communicator 2007客户端发送IM的简单示例。我不打算使用threads.or接收响应。这只是偶尔发送警报(或公告),最好是在html中,尽可能少的代码行。这将是最初的原型,所以我不想花很多时间学习我可能不会使用的API,所以我希望可能有一个非常简单的例子我可以玩。我已经看过BroadcastIM和其他样本,但它们是复杂的或限制我需要的。有什么建议或例子吗?
谢谢!
这个怎么样?
1
public class RTCTest:IOfferAnswer
2
{
3
< font style ="color:blue"> string uri = " sip:sender @ domain。 com" ;
4
string server = " server.domain.com" ;
5
< font style ="color:blue"> string username = " username" ;
6
string password = " password" ;
7
< font style ="color:blue"> string domain = " domain" ;
8
9
string targeturi = " sip:someone @ domain .com" ;
10
11
private RealTimeClientConnectionManager _connectionManager;
12
private SipEndpoint _endpoint;
13
14
NetworkCredential _credentials;
15
SignalingSession _session;
16
17
public void start()
18
< td style ="background-color:#f7f7f7"> {
19
_credentials = new NetworkCredential(用户名,密码,域名);
20
string message = "我还活着" ;
21
_connectionManager =(RealTimeClientConnectionManager) new RealTimeClientConnectionManager();
22
_endpoint = new SipEndpoint(uri,SipAuthenticationProtocols.Ntlm,SipTransportType.Tcp, server,5060, true ,_ connectionManager, null );
23
_endpoint.CredentialCache.Add (SipEndpoint.DefaultRtcRealm,_credentials);
24
_endpoint.DisplayName = " Sample" ;
25
_endpoint.ApplicationUserAgent = " Sample" ;
26
_endpoint.Register();
27
28
_session = new SignalingSession(_endpoint, new RealTimeAddress(targetURI中));
29
_session.OfferAnswerNegotiation = this ;
30
31
_session.Participate( );
32
_session.SendMessage(MessageType.Message, new ContentType( " text / plain" ),Encoding.UTF8.GetBytes(message));
33
34
}
35
36
#region IOfferAnswer Members
37
38
public ContentDescription GetAnswer( object sender,ContentDescription offer)
39
{
40
throw new NotImplementedException();
41
}
42
43
public ContentDescription GetOffer( object 发件人)
44
{
45
< td> SignalingSession Session = sender as SignalingSession;
46
的IPAddress Ip地址;
47
48
//此方法是每一个出站的INVITE被发送的时间叫回来。
49
if (Session.Connection!= null )
50
{
51
IpAddress = Session.Connection.LocalEndpoint.Address;
52
}
53
54
else
55
{
56
IpAddress = IPAddress.Any;
57
}
58
59
Sdp< SdpGlobalDescription,SdpMediaDescription> SessionDescription = new Sdp< SdpGlobalDescription,SdpMediaDescription>();
60
61
//设置SDP的原点行
62
// s,t和v行自动构造
63
SessionDescription.GlobalDescription.Origin.Version = 0;
64
SessionDescription.GlobalDescription.Origin.SessionId = " 0" ;
65
SessionDescription.GlobalDescription.Origin .UserName = " - " ;
66
SessionDescription.GlobalDescription.Origin.Connection.Set(IpAddress.ToString());
67
68
//设置连接线
69
SessionDescription.GlobalDescription.Connection.TrySet(IpAddress.ToString());
70
71
SdpMediaDescription mditem = < font style ="color:blue"> new SdpMediaDescription( " message" );
72
73
mditem.Port = 5060;
74
mditem.TransportProtocol = " sip" ;
75
mditem.Formats = " null" ;
76
77
SdpAttribute aitem = new SdpAttribute( " accept-类型" , " text / plain" );
78
79
mditem.Attributes。加入(aitem);
80
81
//将媒体描述附加到全局描述
82
SessionDescription.MediaDescriptions.Add(mditem);
83
84
ContentType ct = < font style ="color:blue"> new ContentType( " application / sdp" );
85
86
return new ContentDescription(CT,SessionDescription.GetBytes());
87
}
88
89
public void HandleOfferInInviteResponse( object sender,OfferInInviteResponseEventArgs e)
90
{
91
throw new NotImplementedException() ;
92
}
93
94
public void HandleOfferInReInvite( object sender,OfferInReInviteEventArgs e)
95
{
96
< font style ="color:blue"> throw new NotImplementedException();
97
}
98
99
public void SetAnswer( object sender,ContentDescription answer)
100
{
101
}
102
103
#endregion
104
105
public < font style ="color:blue"> void end()
106
{
107
_session.Terminate();
108
_endpoint.Unregister();
109
_endpoint.Terminate();
110
_connectionManager.Dispose();
111
112
}
113
}
114
115
class 程序
116
{
117
static void Main( string [] args)
118
{
119
RTCTest t = new RTCTest();
120
Console.WriteLine( " start session" );
121
t.start() ;
122
Console.WriteLine( "按enter键结束会话" );
123
Console.ReadLine() ;
124
t.end();
125
}
126
}
I am looking for a really simple example of sending an IM to a Communicator 2007 client. I do not plan on using threads.or receiving a response. This is just going to occaisionally send an alert (or announcement), preferably in html, in as few lines of code as possible. This will be prototype initially so I don't want to invest a whole lot of time learning an API that I might not use so I was hoping there might be a really simple example I can toy with. I have seen the BroadcastIM and other samples but they are to complex or restricted for what I need. Any suggestions or examples?
Thanks!
解决方案How about this?
1 public class RTCTest:IOfferAnswer 2 { 3 string uri = "sip:sender@domain.com"; 4 string server = "server.domain.com"; 5 string username = "username"; 6 string password = "password"; 7 string domain = "domain"; 8 9 string targeturi = "sip:someone@domain.com"; 10 11 private RealTimeClientConnectionManager _connectionManager; 12 private SipEndpoint _endpoint; 13 14 NetworkCredential _credentials; 15 SignalingSession _session; 16 17 public void start() 18 { 19 _credentials = new NetworkCredential(username,password,domain); 20 string message = "I'm alive"; 21 _connectionManager = (RealTimeClientConnectionManager)new RealTimeClientConnectionManager(); 22 _endpoint = new SipEndpoint(uri,SipAuthenticationProtocols.Ntlm,SipTransportType.Tcp,server,5060,true,_connectionManager,null); 23 _endpoint.CredentialCache.Add(SipEndpoint.DefaultRtcRealm,_credentials); 24 _endpoint.DisplayName = "Sample"; 25 _endpoint.ApplicationUserAgent = "Sample"; 26 _endpoint.Register(); 27 28 _session = new SignalingSession(_endpoint, new RealTimeAddress(targeturi)); 29 _session.OfferAnswerNegotiation = this; 30 31 _session.Participate(); 32 _session.SendMessage(MessageType.Message, new ContentType("text/plain"), Encoding.UTF8.GetBytes(message)); 33 34 } 35 36 #region IOfferAnswer Members 37 38 public ContentDescription GetAnswer(object sender, ContentDescription offer) 39 { 40 throw new NotImplementedException(); 41 } 42 43 public ContentDescription GetOffer(object sender) 44 { 45 SignalingSession Session = sender as SignalingSession; 46 IPAddress IpAddress; 47 48 // This method is called back every time an outbound INVITE is sent. 49 if (Session.Connection != null) 50 { 51 IpAddress = Session.Connection.LocalEndpoint.Address; 52 } 53 54 else 55 { 56 IpAddress = IPAddress.Any; 57 } 58 59 Sdp<SdpGlobalDescription, SdpMediaDescription> SessionDescription = new Sdp<SdpGlobalDescription, SdpMediaDescription>(); 60 61 //Set the origin line of the SDP 62 //s, t, and v lines are automatically constructed 63 SessionDescription.GlobalDescription.Origin.Version = 0; 64 SessionDescription.GlobalDescription.Origin.SessionId = "0"; 65 SessionDescription.GlobalDescription.Origin.UserName = "-"; 66 SessionDescription.GlobalDescription.Origin.Connection.Set(IpAddress.ToString()); 67 68 //Set the connection line 69 SessionDescription.GlobalDescription.Connection.TrySet(IpAddress.ToString()); 70 71 SdpMediaDescription mditem = new SdpMediaDescription("message"); 72 73 mditem.Port = 5060; 74 mditem.TransportProtocol = "sip"; 75 mditem.Formats = "null"; 76 77 SdpAttribute aitem = new SdpAttribute("accept-types", "text/plain"); 78 79 mditem.Attributes.Add(aitem); 80 81 //Append the Media description to the Global description 82 SessionDescription.MediaDescriptions.Add(mditem); 83 84 ContentType ct = new ContentType("application/sdp"); 85 86 return new ContentDescription(ct, SessionDescription.GetBytes()); 87 } 88 89 public void HandleOfferInInviteResponse(object sender, OfferInInviteResponseEventArgs e) 90 { 91 throw new NotImplementedException(); 92 } 93 94 public void HandleOfferInReInvite(object sender, OfferInReInviteEventArgs e) 95 { 96 throw new NotImplementedException(); 97 } 98 99 public void SetAnswer(object sender, ContentDescription answer) 100 { 101 } 102 103 #endregion 104 105 public void end() 106 { 107 _session.Terminate(); 108 _endpoint.Unregister(); 109 _endpoint.Terminate(); 110 _connectionManager.Dispose(); 111 112 } 113 } 114 115 class Program 116 { 117 static void Main(string[] args) 118 { 119 RTCTest t = new RTCTest(); 120 Console.WriteLine("start session"); 121 t.start(); 122 Console.WriteLine("press enter to end session"); 123 Console.ReadLine(); 124 t.end(); 125 } 126 }
这篇关于同步发送IM的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文