Skype – 如何开始?
我花了很多时间。 整个Skype论坛似乎已经破裂或死亡,或者他们没有技术人员来处理这些领域。
入门代码示例无效或不可用(完全exception)。 例如: http : //forum.skype.com/index.php?showtopic = 3557
因此,我请求有人可以请给我一个简单的C#工作代码示例如何一步一步地开始。
提前致谢。
- 添加对skype COM库的引用
- 添加以下类并开始探索skype。
代码(包括使用SKYPE4COMLib;它搞砸了stackoverflow语法高亮显示)
namespace Example { class SkypeExample { private SkypeClass _skype; public SkypeExample() { _skype = new SkypeClass(); _skype.MessageStatus += OnMessage; _skype._ISkypeEvents_Event_AttachmentStatus += OnAttach; _skype.Attach(7, false); } private void OnAttach(TAttachmentStatus status) { // this app was successfully attached to skype } private void OnMessage(ChatMessage pmessage, TChatMessageStatus status) { // dont do anything if the message is not received (ie we are sending a emssage) if (status != TChatMessageStatus.cmsReceived) return; // simple echo service. _skype.get_Chat(pmessage.ChatName).SendMessage(pmessage.Body); } public bool MakeFriend(string handle) { for (int i = 1; i <= _skype.Friends.Count; i++) { if (_skype.Friends[i].Handle == handle) return true; } UserCollection collection = _skype.SearchForUsers(handle); if (collection.Count >= 1) collection[1].BuddyStatus = TBuddyStatus.budPendingAuthorization; return false; } } }
除了jgauffin所说的,这里是我以前使用的(单例)包装类(C#3):
using System; using System.Collections.Generic; using System.Linq; using SKYPE4COMLib; namespace SkypeR { public class SkypeApplication { private static readonly SkypeApplication TheInstance = new SkypeApplication(); private readonly Skype _skype = new Skype(); private SkypeApplication() { _skype.OnlineStatus += SkypeOnlineStatus; _skype.UserStatus += SkypeUserStatus; _skype.UserMood += SkypeUserMood; } private void SkypeUserMood(User puser, string moodtext) { FireUserMood(puser.GetUserInfo(), puser.OnlineStatus.GetStatusValue(), moodtext); } public string MyMood { get { return _skype.CurrentUserProfile.MoodText; } set { _skype.CurrentUserProfile.MoodText = value; } } public static SkypeApplication Instance { get { return TheInstance; } } public bool Running { get { return _skype.Client.IsRunning; } } public string Information { get { return String.Format("v{0}", _skype.Version); } } public Status MyStatus { get { return _skype.CurrentUserStatus.GetStatusValue(); } set { _skype.CurrentUserStatus = value.GetTUserStatusValue(); } } public IEnumerable ContactList { get { UserCollection friends = _skype.Friends; return from User user in friends select new Contact { UserInfo = user.GetUserInfo(), Status = user.OnlineStatus.GetStatusValue() }; } } private void SkypeUserStatus(TUserStatus status) { FireUserStatus(status.GetStatusValue()); } private void FireUserStatus(Status status) { if (OnStatusChange != null) { OnStatusChange(this, new StatusChangedArgs {Status = status, Time = DateTime.Now}); } } private void SkypeOnlineStatus(User puser, TOnlineStatus status) { FireUserStatus(puser.GetUserInfo(), status.GetStatusValue(), puser.MoodText); } public void Start() { if (!_skype.Client.IsRunning) { _skype.Client.Start(true, true); } } public void Shutdown() { if (_skype.Client.IsRunning) { _skype.Client.Shutdown(); } } public event EventHandler OnUserStatusChange; public event EventHandler OnStatusChange; public event EventHandler OnUserMoodChange; private void FireUserStatus(UserInfo user, Status newStatus, string userMood) { if (OnUserStatusChange != null) { OnUserStatusChange(this, new UserSomethingChangedArgs { Contact = new Contact {UserInfo = user, Status = newStatus, Mood = userMood}, Time = DateTime.Now }); } } public void FireUserMood(UserInfo user, Status userStatus, string newMood) { if (OnUserMoodChange != null) { OnUserMoodChange(this, new UserSomethingChangedArgs { Contact = new Contact { UserInfo = user, Status= userStatus, Mood = newMood }, Time = DateTime.Now }); } } } public class StatusChangedArgs : EventArgs { public Status Status { get; set; } public DateTime Time { get; set; } } public class UserSomethingChangedArgs : EventArgs { public Contact Contact { get; set; } public DateTime Time { get; set; } } public enum Sex { Female, Male } public enum Status { Online, Away, Offline, Unknown, DoNotDisturb, NotAvailable, SkypeMe, SkypeOut } public struct UserInfo { public string Name { get; set; } public Sex Sex { get; set; } public static bool operator ==(UserInfo i1, UserInfo i2) { return (i1.Name.Equals(i2.Name) && i1.Sex.Equals(i2.Sex)); } public static bool operator !=(UserInfo i1, UserInfo i2) { return (!i1.Sex.Equals(i2.Sex) || !i1.Name.Equals(i2.Name)); } public bool Equals(UserInfo other) { return Equals(other.Name, Name) && Equals(other.Sex, Sex); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; return obj.GetType() == typeof (UserInfo) && Equals((UserInfo) obj); } public override int GetHashCode() { unchecked { return ((Name != null ? Name.GetHashCode() : 0)*397) ^ Sex.GetHashCode(); } } } public class Contact { public UserInfo UserInfo { get; set; } public Status Status { get; set; } public string Mood { get; set; } } public static class Extensions { public static string GetSomeName(this User user) { if (!string.IsNullOrEmpty(user.FullName)) return user.FullName; if (!string.IsNullOrEmpty(user.DisplayName)) return user.DisplayName; if (!string.IsNullOrEmpty(user.Handle)) return user.Handle; if (!string.IsNullOrEmpty(user.Aliases)) return user.Aliases; return "unbekannt"; } public static Sex GetUserInfoSexValue(this TUserSex sex) { return sex == TUserSex.usexMale ? Sex.Male : Sex.Female; } public static string ToLocalString(this Status status) { switch (status) { case Status.Online: return "online"; case Status.Away: return "abwesend"; case Status.Offline: return "offline"; case Status.Unknown: return "unbekannt"; case Status.DoNotDisturb: return "nicht stören"; case Status.NotAvailable: return "nicht verfügbar"; case Status.SkypeMe: return "Skype Me!"; case Status.SkypeOut: return "Skype out"; default: return String.Empty; } } public static Status GetStatusValue(this TOnlineStatus status) { switch (status) { case TOnlineStatus.olsOnline: return Status.Online; case TOnlineStatus.olsAway: return Status.Away; case TOnlineStatus.olsOffline: return Status.Offline; case TOnlineStatus.olsDoNotDisturb: return Status.DoNotDisturb; case TOnlineStatus.olsNotAvailable: return Status.NotAvailable; case TOnlineStatus.olsSkypeMe: return Status.SkypeMe; case TOnlineStatus.olsSkypeOut: return Status.SkypeOut; } return Status.Unknown; } public static TUserStatus GetTUserStatusValue(this Status status) { switch (status) { case Status.Online: return TUserStatus.cusOnline; case Status.Away: return TUserStatus.cusAway; case Status.Offline: return TUserStatus.cusOffline; case Status.Unknown: return TUserStatus.cusUnknown; case Status.DoNotDisturb: return TUserStatus.cusDoNotDisturb; case Status.NotAvailable: return TUserStatus.cusNotAvailable; case Status.SkypeMe: return TUserStatus.cusSkypeMe; } return TUserStatus.cusUnknown; } public static Status GetStatusValue(this TUserStatus status) { switch (status) { case TUserStatus.cusOnline: return Status.Online; case TUserStatus.cusAway: return Status.Away; case TUserStatus.cusOffline: return Status.Offline; case TUserStatus.cusDoNotDisturb: return Status.DoNotDisturb; case TUserStatus.cusNotAvailable: return Status.NotAvailable; case TUserStatus.cusSkypeMe: return Status.SkypeMe; } return Status.Unknown; } public static int GetImageIndex(this Status status) { switch (status) { case Status.Online: return 0; case Status.Away: return 1; case Status.Offline: return 4; case Status.Unknown: return 4; case Status.DoNotDisturb: return 2; case Status.NotAvailable: return 1; case Status.SkypeMe: return 0; case Status.SkypeOut: return 3; default: return 4; } } public static UserInfo GetUserInfo(this User user) { // TODO: Maybe take from some database! return new UserInfo {Name = user.GetSomeName(), Sex = user.Sex.GetUserInfoSexValue()}; } } }
如果您愿意,可以延长它,如果您愿意,甚至可以将其发回。
一般用法:
启动Skype
SkypeApplication.Instance.Start();
注册某些活动
SkypeApplication.Instance.OnUserStatusChange += UserStatusChange; SkypeApplication.Instance.OnUserMoodChange += UserMoodChange; SkypeApplication.Instance.OnStatusChange += StatusChange;
在哪里,例如:
private void UserMoodChange(object sender, UserSomethingChangedArgs args) { var listViewItem = new ListViewItem(new[] { args.Time.ToString(), args.Contact.UserInfo.Name, args.Contact.Status.ToLocalString(), args.Contact.Mood }, args.Contact.Status.GetImageIndex()); listView1.Items.Add(listViewItem); listViewItem.EnsureVisible(); NotifyStatus(args); RefreshContactList(); LogIt(args); }
正如您所看到的,有一些辅助扩展方法可以获取图像索引,状态消息文本(需要本地化)等等。
设置我的状态
SkypeApplication.Instance.MyStatus = Status.Away;
与jgauffin相反,我写的包装器不会修改任何东西,只是只读状态除了状态。
最后,一个小的部署问题:您必须将可执行文件编译为x86才能使SKYPE4COMLib正常工作。 IIRC这适用于所有COM互操作组件。
如果您有任何疑问,请随时询问。
自微软收购Skype以来,Skype API已被弃用。 你不能在新的群聊中使用它。
试试这个: http : //forum.skype.com/index.php?showtopic = 142821
上述就是C#学习教程:Skype – 如何开始?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1021352.html