以下是使用FetionSDK.dll的一个示例,PowerTalkBox是一个聊天控件,可以制作IM客服在线咨询系统,也可以做聊天室,把他们两个结合起来做一个例子,应该是应用范围比较广,希望对大家有用.
关键部分的代码:
/// <summary>
/// 登陆飞信
/// </summary>
private bool FetionLogin(string userName,string pwd)
{
sdk.AccountManager.FillUserIdAndPassword(userName, pwd, false);//第三个参数为是否自动登陆
bool isOk=sdk.AccountManager.Login();//登陆
return isOk;
}
/// <summary>
/// 发送消息
/// </summary>
private bool SendFetionMsg(string userName, string msg)
{
bool isOk = false;
try
{
int state = sdk.ContactControl.SendIM.SendIM(userName, msg);
}
catch { isOk = false; }
return isOk;
}
/// <summary>
/// 发送手机消息
/// </summary>
private bool SendFetionMobileMsg(string userName, string msg)
{
bool isOk = false;
try
{
int state = sdk.ContactControl.SendSMS.SendSMS(userName, msg);
}
catch { isOk = false; }
return isOk;
}
/// <summary>
/// 加载好友列表
/// </summary>
/// <param name="psSdk">飞信sdk对象</param>
/// <returns>返回飞信好友信息集合</returns>
private List<FetionFriend> GetFetionFriendList(FetionSDK psSdk)
{
List<FetionFriend> fetionFriends = new List<FetionFriend>();
try
{
List<Imps.Client.Core.Contact> list = psSdk.ContactControl.getAllContactList();
for (int i = 0; i < list.Count; i++)
{
try
{
FetionFriend fetionFriend = new FetionFriend();//自定义飞信用户实体信息类
fetionFriend.UserId = int.Parse(list[i].Uri.Id);
fetionFriend.Nickname = list[i].DisplayName;
fetionFriend.Mobile = list[i].PersonalInfo.MobileNo;
fetionFriends.Add(fetionFriend);
}
catch { }
}
}
catch { fetionFriends = new List<FetionFriend>(); }
return fetionFriends;
}
/// <summary>
/// 飞信sdk出现错误
/// </summary>
private void Sdk_SDK_Error(object sender, NullStudio.Fetion_SDK.Event.SDK_ErrorEventArgs fe)
{
this.txtBox.Text += "信息:"+fe.Message.Message+"\r\n";
}
private string userName = "";
private string userPwd = "";
/// <summary>
/// 当前飞信号状态
/// </summary>
private void Sdk_SDK_UserSatusChange(object sender, Imps.Client.Core.UserSatusChangedEventArgs e)
{
switch (e.NewStatus.ToString())
{
case "Disconnected"://断开了网络
FetionLogin(userName, userPwd);//重新登陆
break;
case "Initialized"://已初始化
break;
case "Loginning"://登陆中
break;
case "Logon"://已登陆
sdk.AccountManager.CurrentUser.Presence.AsyncChangeMainPresence(Imps.Common.MainPresence.Online, Imps.Common.MainPresence.Online.ToString());//设置PC在线
break;
case "Logoff"://离开网络
break;
case "Logouting"://注销中
break;
case "None"://无状态
break;
case "OfflineLogon"://离线
break;
case "StandBy"://待机
break;
case "WaitReconnect"://等待
break;
}
this.txtBox.Text += "状态"+e.NewStatus.ToString() + "\r\n";
}
/// <summary>
/// 接收到的消息
/// </summary>
private void sdk_SDK_ReceiveMessage(object sender, NullStudio.Fetion_SDK.Event.SDK_ReceiveMessageEventArgs fe)
{
string Content = fe.Message.Substring(fe.Message.IndexOf("\r\n")+2);
string[] ContentS = Content.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
int lc=Content.IndexOf(':');
string UserTo=ContentS[0].ToString();
string Msg=Content.Substring(lc+1);
if (Msg[0] == '@')//如果是转移字符串
{
string CChar = Msg.Substring(1);
int topi = 0;
if (int.TryParse(CChar, out topi))//如果为转意数字的话
{
string ListStr = textBox6.Text;
string[] ChangeChar = ListStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string istr in ChangeChar)
{
string[] OneChar = istr.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
string One = OneChar[0];
if (CChar == One)
{
Msg = OneChar[1];
}
}
}
}
PowerTalkService.FetionInterFace PTFIF = new PowerTalkClient.PowerTalkService.FetionInterFace();
PTFIF.ChatAdd(textBox3.Text, "游客"+UserTo,Msg);
this.txtBox.Text += "" + string.Format(fe.Message, fe.Contact.PersonalInfo.Nickname +"("+ fe.Contact.Uri.Sid.ToString()+")") + "\r\n";
}
private FetionSDK sdk = new FetionSDK();//实例化飞信sdk
private void Fetion_Load(object sender, EventArgs e)
{
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);//消息接收事件
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(Sdk_SDK_UserSatusChange);//用户状态事件
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(Sdk_SDK_Error);//sdk错误事件
textBox6.Text = "1:您好!\r\n2:有什么可以帮助您的?\r\n3:是的,我们的产品是开源但不免费的。";
}
视频演示下载地址:https://files.cnblogs.com/hanghangaidoudou/AVI.rar
代码下载地址:https://files.cnblogs.com/hanghangaidoudou/FetionAndPowerTalk.rar