田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 4 48
 123456
78910111213
14151617181920
21222324252627
282930


站点统计

最新评论



Visual Basic 2005 飞信SDK开发短信收发程序 红色中国鼠标
未知 【转帖】Delphi.NET/Chrome 飞信SDK开发短信收发程序   [ 日期:2009-01-08 ]   [ 来自:【转帖】 ]  HTML
飞信SDK开发短信收发程序 

利用飞信的协议可以在线收发消息,或是向手机发送消息。由此,可以自己来完成一个IM工具。
本文即是对飞信SDK的使用方法,及如何开发作一个说明。

一、引用FetionSDK
飞信是采用C#开发的,所有的程序集均是.NET,因此我们也需要使用Delphi.NET/Chrome来进行相关的开发。在Chrome中,新建一个工程,并引入FetionSDK.dll,当然您也可以使用Delphi2007 for .NET,开发出来结果一样。
其实我曾尝试过把FetionSDK.dll变成一个COM+程序,但是不巧的是这个dll没有strong name,无法转换。
引用完SDK后,在主窗体的uses下添加NullStudio.Fetion_SDK。

二、准备工作
准备工作很简单,在public区分符下,建立一个名为sdk的FetionSDK对象。然后为它创建一个实例。

三、用户登录
使用以下代码来填入用户名和密码:
sdk.AccountManager.FillUserIdAndPassword(UserID,Password,true);
三个参数分别是用户手机号,密码,是否自动登录,当选定了自动登录为True时,可以使用
       sdk.AccountManager.LoginOrLogout();
来自动的判断是登录或是注销,否则的话,就要使用
sdk.AccountManager.Login(); 来登录。

四、状态改变
用户登录或注销,或是改变自己当前的状态时,会触发状态改变的事件。如下:
procedure sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
其中参数e中来自命名空间Imps.Client.Core。
然后我们绑定这个事件:
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange);
这样SDK就能接收到状态改变的事件了。另外,还能针对各个状态,执行不同的指令,如下:
case e.NewStatus of
Imps.Client.UserAccountStatus.Disconnected: ;
Imps.Client.UserAccountStatus.Initialized: ;
Imps.Client.UserAccountStatus.Loginning: ;
Imps.Client.UserAccountStatus.Logon:
Imps.Client.UserAccountStatus.Logoff: ;
Imps.Client.UserAccountStatus.Logouting: ;
Imps.Client.UserAccountStatus.None: ;
Imps.Client.UserAccountStatus.OfflineLogon: ;
Imps.Client.UserAccountStatus.StandBy: ;
Imps.Client.UserAccountStatus.WaitReconnect: ;
else
end;

五、获取好友列表
var
lst : List<Contact>;
i: Integer;
begin
lstFriendLst.Items.Clear();
lst := sdk.ContactControl.getAllContactList();
for i := 0 to lst.Count - 1 do
begin
lstFriendLst.Items.Add(
string.Format("{0} [Fetion: {1} Mobile: {2}]",
lst[i].DisplayName, lst[i].Uri.Id, 
IfThen(lst[i].PersonalInfo.MobileNo = string.Empty, "Not Published", lst[i].PersonalInfo.MobileNo)));
end;

六、发送消息
调用SDK的发送消息指令,传入的参数分别是对方手机号码和短信的内容。
sdk.ContactControl.SendIM.SendIM(edtPhoneNo.Text, edtSendMsg.Text);

七、接收消息
接收到消息时,会解发SDK的收到消息事件,如下:
procedure sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs);
实现此方法后,绑定这个事件。
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);

八、发送手机短信
与发送消息一样,只不过使用的是另一个方法。
sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself.Uri.Id,edtSendMsg.Text);
在这里需要注意SendIM与SendSMS的区别。
getMyself是SDK中的一个方法,用来获取当前用户的信息。

九、出错事件
当SDK因为某种原因出错后,会触发出错事件,如下:
procedure sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs);
实现后绑定:
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(sdk_SDK_Error);

十、编译,执行程序
现在可以编译并执行程序了。

十一、程序源码

namespace FetionDemo;

interface

uses
System.Windows.Forms,
System.Drawing,
Imps.Client.Core,
NullStudio.Fetion_SDK,
NullStudio.Fetion_SDK.Event,
System.Collections.Generic;

type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = class(System.Windows.Forms.Form)
{$REGION Windows Form Designer generated fields}
private
    btnSendSelf: System.Windows.Forms.Button;
    edtPhoneNo: System.Windows.Forms.TextBox;
    btnSend: System.Windows.Forms.Button;
    edtSendMsg: System.Windows.Forms.TextBox;
    sbMain: System.Windows.Forms.StatusStrip;
    lblPassword: System.Windows.Forms.Label;
    lblAccount: System.Windows.Forms.Label;
    edtMsg: System.Windows.Forms.TextBox;
    lblPhoneNo: System.Windows.Forms.Label;
    gbMsg: System.Windows.Forms.GroupBox;
    lstFriendLst: System.Windows.Forms.ListBox;
    gbFriendLst: System.Windows.Forms.GroupBox;
    edtPassword: System.Windows.Forms.TextBox;
    edtUserID: System.Windows.Forms.TextBox;
    lblError: System.Windows.Forms.ToolStripStatusLabel;
    btnLogoff: System.Windows.Forms.Button;
    btnLogin: System.Windows.Forms.Button;
    lblStatus: System.Windows.Forms.ToolStripStatusLabel;
    gbLogin: System.Windows.Forms.GroupBox;
    components: System.ComponentModel.Container := nil;
    method InitializeComponent;
{$ENDREGION}
private
      method btnSendSelf_Click(sender: System.Object; e: System.EventArgs);
      method btnSend_Click(sender: System.Object; e: System.EventArgs);
      method btnLogoff_Click(sender: System.Object; e: System.EventArgs);
      method btnLogin_Click(sender: System.Object; e: System.EventArgs);
      method MainForm_Load(sender: System.Object; e: System.EventArgs);
protected
    method Dispose(aDisposing: boolean); override;
    function IfThen(ABool: Boolean; AStr1, AStr2: String): String;
public
    sdk : FetionSDK;
    bLogon: Boolean;
    procedure sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
    procedure sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs);
    procedure sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs);
    constructor;
end;

implementation

{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
end;

method MainForm.Dispose(aDisposing: boolean);
begin
if aDisposing then begin
    if assigned(components) then
      components.Dispose();

    //
    // TODO: Add custom disposition code here
    //
end;
inherited Dispose(aDisposing);
end;
{$ENDREGION}

{$REGION Windows Form Designer generated code}
method MainForm.InitializeComponent;
begin
var resources: System.ComponentModel.ComponentResourceManager := new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
self.sbMain := new System.Windows.Forms.StatusStrip();
self.lblStatus := new System.Windows.Forms.ToolStripStatusLabel();
self.lblError := new System.Windows.Forms.ToolStripStatusLabel();
self.gbMsg := new System.Windows.Forms.GroupBox();
self.lblPhoneNo := new System.Windows.Forms.Label();
self.edtSendMsg := new System.Windows.Forms.TextBox();
self.btnSend := new System.Windows.Forms.Button();
self.edtPhoneNo := new System.Windows.Forms.TextBox();
self.edtMsg := new System.Windows.Forms.TextBox();
self.btnLogin := new System.Windows.Forms.Button();
self.edtUserID := new System.Windows.Forms.TextBox();
self.edtPassword := new System.Windows.Forms.TextBox();
self.lblAccount := new System.Windows.Forms.Label();
self.lblPassword := new System.Windows.Forms.Label();
self.gbLogin := new System.Windows.Forms.GroupBox();
self.btnLogoff := new System.Windows.Forms.Button();
self.gbFriendLst := new System.Windows.Forms.GroupBox();
self.lstFriendLst := new System.Windows.Forms.ListBox();
self.btnSendSelf := new System.Windows.Forms.Button();
self.sbMain.SuspendLayout();
self.gbMsg.SuspendLayout();
self.gbLogin.SuspendLayout();
self.gbFriendLst.SuspendLayout();
self.SuspendLayout();
// 
// sbMain
// 
self.sbMain.Items.AddRange(array of System.Windows.Forms.ToolStripItem([self.lblStatus,
      self.lblError]));
self.sbMain.Location := new System.Drawing.Point(0, 466);
self.sbMain.Name := ‘sbMain‘;
self.sbMain.Size := new System.Drawing.Size(358, 22);
self.sbMain.TabIndex := 9;
self.sbMain.Text := ‘statusStrip1‘;
// 
// lblStatus
// 
self.lblStatus.Name := ‘lblStatus‘;
self.lblStatus.Size := new System.Drawing.Size(41, 17);
self.lblStatus.Text := ‘Logoff‘;
// 
// lblError
// 
self.lblError.Name := ‘lblError‘;
self.lblError.Size := new System.Drawing.Size(53, 17);
self.lblError.Text := ‘No Error‘;
// 
// gbMsg
// 
self.gbMsg.Controls.Add(self.lblPhoneNo);
self.gbMsg.Controls.Add(self.edtSendMsg);
self.gbMsg.Controls.Add(self.btnSend);
self.gbMsg.Controls.Add(self.edtPhoneNo);
self.gbMsg.Controls.Add(self.edtMsg);
self.gbMsg.Location := new System.Drawing.Point(12, 198);
self.gbMsg.Name := ‘gbMsg‘;
self.gbMsg.Size := new System.Drawing.Size(337, 231);
self.gbMsg.TabIndex := 8;
self.gbMsg.TabStop := false;
self.gbMsg.Text := ‘Message‘;
// 
// lblPhoneNo
// 
self.lblPhoneNo.AutoSize := true;
self.lblPhoneNo.Location := new System.Drawing.Point(11, 174);
self.lblPhoneNo.Name := ‘lblPhoneNo‘;
self.lblPhoneNo.Size := new System.Drawing.Size(53, 12);
self.lblPhoneNo.TabIndex := 6;
self.lblPhoneNo.Text := ‘Phone No‘;
// 
// edtSendMsg
// 
self.edtSendMsg.Location := new System.Drawing.Point(7, 198);
self.edtSendMsg.Name := ‘edtSendMsg‘;
self.edtSendMsg.Size := new System.Drawing.Size(249, 21);
self.edtSendMsg.TabIndex := 5;
// 
// btnSend
// 
self.btnSend.Location := new System.Drawing.Point(262, 196);
self.btnSend.Name := ‘btnSend‘;
self.btnSend.Size := new System.Drawing.Size(69, 23);
self.btnSend.TabIndex := 4;
self.btnSend.Text := ‘Send‘;
self.btnSend.UseVisualStyleBackColor := true;
self.btnSend.Click += new System.EventHandler(@self.btnSend_Click);
// 
// edtPhoneNo
// 
self.edtPhoneNo.Location := new System.Drawing.Point(68, 171);
self.edtPhoneNo.Name := ‘edtPhoneNo‘;
self.edtPhoneNo.Size := new System.Drawing.Size(145, 21);
self.edtPhoneNo.TabIndex := 3;
// 
// edtMsg
// 
self.edtMsg.Location := new System.Drawing.Point(5, 20);
self.edtMsg.Multiline := true;
self.edtMsg.Name := ‘edtMsg‘;
self.edtMsg.ReadOnly := true;
self.edtMsg.ScrollBars := System.Windows.Forms.ScrollBars.Vertical;
self.edtMsg.Size := new System.Drawing.Size(326, 145);
self.edtMsg.TabIndex := 1;
// 
// btnLogin
// 
self.btnLogin.Location := new System.Drawing.Point(274, 12);
self.btnLogin.Name := ‘btnLogin‘;
self.btnLogin.Size := new System.Drawing.Size(75, 23);
self.btnLogin.TabIndex := 6;
self.btnLogin.Text := ‘Login‘;
self.btnLogin.UseVisualStyleBackColor := true;
self.btnLogin.Click += new System.EventHandler(@self.btnLogin_Click);
// 
// edtUserID
// 
self.edtUserID.Location := new System.Drawing.Point(69, 17);
self.edtUserID.Name := ‘edtUserID‘;
self.edtUserID.Size := new System.Drawing.Size(180, 21);
self.edtUserID.TabIndex := 3;
// 
// edtPassword
// 
self.edtPassword.Location := new System.Drawing.Point(69, 44);
self.edtPassword.Name := ‘edtPassword‘;
self.edtPassword.PasswordChar := ‘*‘;
self.edtPassword.Size := new System.Drawing.Size(180, 21);
self.edtPassword.TabIndex := 4;
// 
// lblAccount
// 
self.lblAccount.AutoSize := true;
self.lblAccount.Location := new System.Drawing.Point(6, 20);
self.lblAccount.Name := ‘lblAccount‘;
self.lblAccount.Size := new System.Drawing.Size(47, 12);
self.lblAccount.TabIndex := 1;
self.lblAccount.Text := ‘Accoumt‘;
// 
// lblPassword
// 
self.lblPassword.AutoSize := true;
self.lblPassword.Location := new System.Drawing.Point(6, 47);
self.lblPassword.Name := ‘lblPassword‘;
self.lblPassword.Size := new System.Drawing.Size(53, 12);
self.lblPassword.TabIndex := 2;
self.lblPassword.Text := ‘Password‘;
// 
// gbLogin
// 
self.gbLogin.Controls.Add(self.edtPassword);
self.gbLogin.Controls.Add(self.edtUserID);
self.gbLogin.Controls.Add(self.lblAccount);
self.gbLogin.Controls.Add(self.lblPassword);
self.gbLogin.Location := new System.Drawing.Point(11, 6);
self.gbLogin.Name := ‘gbLogin‘;
self.gbLogin.Size := new System.Drawing.Size(255, 80);
self.gbLogin.TabIndex := 7;
self.gbLogin.TabStop := false;
self.gbLogin.Text := ‘Login‘;
// 
// btnLogoff
// 
self.btnLogoff.Location := new System.Drawing.Point(274, 41);
self.btnLogoff.Name := ‘btnLogoff‘;
self.btnLogoff.Size := new System.Drawing.Size(75, 23);
self.btnLogoff.TabIndex := 10;
self.btnLogoff.Text := ‘Logoff‘;
self.btnLogoff.UseVisualStyleBackColor := true;
self.btnLogoff.Click += new System.EventHandler(@self.btnLogoff_Click);
// 
// gbFriendLst
// 
self.gbFriendLst.Controls.Add(self.lstFriendLst);
self.gbFriendLst.Location := new System.Drawing.Point(11, 92);
self.gbFriendLst.Name := ‘gbFriendLst‘;
self.gbFriendLst.Size := new System.Drawing.Size(338, 100);
self.gbFriendLst.TabIndex := 11;
self.gbFriendLst.TabStop := false;
self.gbFriendLst.Text := ‘Friend List‘;
// 
// lstFriendLst
// 
self.lstFriendLst.FormattingEnabled := true;
self.lstFriendLst.ItemHeight := 12;
self.lstFriendLst.Location := new System.Drawing.Point(8, 18);
self.lstFriendLst.Name := ‘lstFriendLst‘;
self.lstFriendLst.Size := new System.Drawing.Size(324, 76);
self.lstFriendLst.TabIndex := 0;
// 
// btnSendSelf
// 
self.btnSendSelf.Location := new System.Drawing.Point(243, 435);
self.btnSendSelf.Name := ‘btnSendSelf‘;
self.btnSendSelf.Size := new System.Drawing.Size(106, 23);
self.btnSendSelf.TabIndex := 12;
self.btnSendSelf.Text := ‘Send To Self‘;
self.btnSendSelf.UseVisualStyleBackColor := true;
self.btnSendSelf.Click += new System.EventHandler(@self.btnSendSelf_Click);
// 
// MainForm
// 
self.ClientSize := new System.Drawing.Size(358, 488);
self.Controls.Add(self.btnSendSelf);
self.Controls.Add(self.gbFriendLst);
self.Controls.Add(self.btnLogoff);
self.Controls.Add(self.sbMain);
self.Controls.Add(self.gbMsg);
self.Controls.Add(self.btnLogin);
self.Controls.Add(self.gbLogin);
self.Icon := (resources.GetObject(‘$this.Icon‘) as System.Drawing.Icon);
self.Name := ‘MainForm‘;
self.Text := ‘Fetion‘;
self.Load += new System.EventHandler(@self.MainForm_Load);
self.sbMain.ResumeLayout(false);
self.sbMain.PerformLayout();
self.gbMsg.ResumeLayout(false);
self.gbMsg.PerformLayout();
self.gbLogin.ResumeLayout(false);
self.gbLogin.PerformLayout();
self.gbFriendLst.ResumeLayout(false);
self.ResumeLayout(false);
self.PerformLayout();
end;
{$ENDREGION}

method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
begin
sdk := new FetionSDK;
bLogon := false;
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange);
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(sdk_SDK_Error);
end;


procedure MainForm.sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
var
    currStat: string;
    i: Integer;
    lst : List<Contact>;
begin
    currStat := e.NewStatus.ToString();
    lblStatus.Text := currStat;
   

    case e.NewStatus of
        Imps.Client.UserAccountStatus.Logon:
        begin
            bLogon := true;
            lstFriendLst.Items.Clear();
            lst := sdk.ContactControl.getAllContactList();
            for i := 0 to lst.Count - 1 do
            begin
                lstFriendLst.Items.Add(
                    string.Format("{0} [Fetion: {1} Mobile: {2}]",
                    lst[i].DisplayName, lst[i].Uri.Id, 
                    IfThen(lst[i].PersonalInfo.MobileNo = string.Empty, "Not Published", lst[i].PersonalInfo.MobileNo)));
            end;
        end;
    else
        begin
            bLogon := false;
            lstFriendLst.Items.Clear();
        end;
    {
        Imps.Client.UserAccountStatus.Disconnected: ;
        Imps.Client.UserAccountStatus.Initialized: ;
        Imps.Client.UserAccountStatus.Loginning: ;
        
        Imps.Client.UserAccountStatus.Logoff: ;
        Imps.Client.UserAccountStatus.Logouting: ;
        Imps.Client.UserAccountStatus.None: ;
        Imps.Client.UserAccountStatus.OfflineLogon: ;
        Imps.Client.UserAccountStatus.StandBy: ;
        Imps.Client.UserAccountStatus.WaitReconnect: ;
    } 
    end;
    
end;

method MainForm.btnLogin_Click(sender: System.Object; e: System.EventArgs);
begin
    if not bLogon then
    begin
        sdk.AccountManager.FillUserIdAndPassword(edtUserID.Text,edtPassword.Text,false);
        sdk.AccountManager.LoginOrLogout();
    end;
end;

procedure MainForm.sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs); 
begin
    lblError.Text := e.Message.Message;
end;

method MainForm.btnLogoff_Click(sender: System.Object; e: System.EventArgs);
begin
    if bLogon then
       sdk.AccountManager.LoginOrLogout();
end;

function MainForm.IfThen(ABool: Boolean; AStr1, AStr2: String): String; 
begin
    if ABool then
        result := AStr1
    else
        result := AStr2;
end;

method MainForm.btnSend_Click(sender: System.Object; e: System.EventArgs);
begin
    sdk.ContactControl.SendIM.SendIM(edtPhoneNo.Text, edtSendMsg.Text);
    edtMsg.Text := edtMsg.Text + ‘Self: ‘ + edtSendMsg.Text + ‘\r\n‘;    
end;

procedure MainForm.sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs); 
begin
    edtMsg.Text := edtMsg.Text + e.Contact.DisplayName + ‘: ‘+e.Message;
end;

method MainForm.btnSendSelf_Click(sender: System.Object; e: System.EventArgs);
begin
    sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself.Uri.Id,edtSendMsg.Text);
end;

end.





暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:2*3=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©