利用飞信的协议可以在线收发消息,或是向手机发送消息。由此,可以自己来完成一个IM工具。

本文即是对飞信SDK的使用方法,及如何开发作一个说明。

FetionSDK下载:http://www.fzzsh.cn/software/FetionSDK.rar

贴一下核心代码:

private FetionSDK sdk = new FetionSDK();//实例化飞信sdk

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错误事件

/// <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";
}

/// <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)
{
    this.txtBox.Text += "" + string.Format(fe.Message, fe.Contact.PersonalInfo.Nickname +"("+ fe.Contact.Uri.Sid.ToString()+")") + "/r/n";
}
 

有几点需要注意的就是:

1、登陆后显示短信在线状态?

       需要用sdk.AccountManager.CurrentUser.Presence.AsyncChangeMainPresence(Imps.Common.MainPresence.Online, Imps.Common.MainPresence.Online.ToString());激活一次,显示PC在线,更改用户状态。

2、为什么会经常掉线?

      解决办法:间隔一段时间更改一次用户状态,让服务器知道我还在线呢。


本文转载:CSDN博客