linux写聊天程序,轻易实现基于linux或win运行的聊天服务端程序
對(duì)于不了解網(wǎng)絡(luò)編程的開(kāi)發(fā)人員來(lái)說(shuō),編寫(xiě)一個(gè)良好的服務(wù)端通訊程序是一件比較麻煩的事情.然而通過(guò)EC這個(gè)免費(fèi)組件你可以非常簡(jiǎn)單地構(gòu)建一個(gè)基于linux或win部署運(yùn)行的網(wǎng)絡(luò)服務(wù)程序.這種便利性完全得益于mono這些年來(lái)的不停發(fā)展.下面介紹通過(guò)EC這個(gè)組件如何通過(guò)短短十來(lái)分鐘的時(shí)候內(nèi)就能實(shí)現(xiàn)一個(gè)聊天室通訊服務(wù)程序.
在實(shí)現(xiàn)一個(gè)網(wǎng)絡(luò)通訊程序的時(shí)候需要定義一個(gè)通訊協(xié)議,但EC已經(jīng)集成了基礎(chǔ)的協(xié)議功能,只需要根據(jù)交互的數(shù)據(jù)定義消息類型即可(EC提供兩種序列化對(duì)象描述分別是protobuf和msgpack).
消息定義
針對(duì)簡(jiǎn)單的聊到室只需要定義登進(jìn),登出和發(fā)言這幾個(gè)消息如下:
[MessageID(0x0001)]
[ProtoContract]
public class Login
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string From { get; set; }
}
[MessageID(0x0003)]
[ProtoContract]
public class Signout
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string From { get; set; }
}
[MessageID(0x0002)]
[ProtoContract]
public class Say
{
[ProtoMember(1)]
public string Content { get; set; }
[ProtoMember(3)]
public string From { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
}
服務(wù)端
消息定義完成那用EC來(lái)制定一個(gè)聊天轉(zhuǎn)發(fā)的服務(wù)端來(lái)說(shuō)則是件非常簡(jiǎn)單的事情,只需要十來(lái)行代碼就可以構(gòu)建聊天和服務(wù)啟動(dòng)等相關(guān)功能.
[EC.Controller]
public class Program
{
static void Main(string[] args)
{
EC.ECServer.Open();
System.Threading.Thread.Sleep(-1);
}
public void OnLogin(EC.ISession session, Chat.Login e)
{
session.Channel.Name = e.Name;
e.From = session.Channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
{
if (other != session.Channel)
session.Application.Server.Send(e, other);
}
}
public void OnSay(EC.ISession session, Chat.Say e)
{
e.Name = session.Channel.Name;
e.From = session.Channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in session.Application.Server.GetOnlines())
{
if (other != session.Channel)
session.Application.Server.Send(e, other);
}
}
}
以上一個(gè)簡(jiǎn)單的聊取室的登進(jìn)和聊天的功能,不過(guò)還有一個(gè)需要我們?nèi)ヌ幚淼木褪钱?dāng)用戶斷開(kāi)后如果反映給其他用戶.在EC中監(jiān)控連接斷開(kāi)的過(guò)程需要通過(guò)一個(gè)AppModel來(lái)監(jiān)控,發(fā)布有連接斷開(kāi)了則向其他連接發(fā)送登出信息,代碼如下:
public class AppModel : EC.IAppModel
{
public void Init(EC.IApplication application)
{
application.Disconnected += (o, e) =>
{
Beetle.Express.IChannel channel = e.Session.Channel;
Chat.Signout msg = new Signout();
msg.Name = channel.Name;
msg.From = channel.EndPoint.ToString();
foreach (Beetle.Express.IChannel other in application.Server.GetOnlines())
{
if (other != channel)
application.Server.Send(msg, other);
}
};
}
public string Name
{
get { return "AppModel"; }
}
public string Command(string cmd)
{
throw new NotImplementedException();
}
}
EC提供一個(gè)IAppModel的自定義功能,通過(guò)AppModel可以監(jiān)控用戶會(huì)話,和處理全局消息的能力;在以后的文章再詳細(xì)介紹.
客戶端
EC同樣提供便利的Client功能對(duì)象,你只需要定義簡(jiǎn)單的代碼就可以向?qū)?yīng)的服務(wù)端發(fā)送和接收相應(yīng)的消息來(lái)處理.
EC.ProtoClient mClient = new EC.ProtoClient("127.0.0.1");
mClient.Receive = (o, p) => {
if (p.Message is Say)
{
Invoke(new Action(OnSay), p.Message);
}
else if (p.Message is Login)
{
Invoke(new Action(OnLogin), p.Message);
}
else if (p.Message is Signout)
{
Invoke(new Action(OnSignout), p.Message);
}
};
mClient.Send(new Say{ Content=t"你好"});
借助于Xamarin我們還可以同樣的方式把功能移植到不同平臺(tái)下運(yùn)行如android,ios等
private IServiceChannel mClient = new ServiceChannel("10.0.2.2",10034);
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ServiceChannel.Register (typeof(MainActivity).Assembly);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
EditText name = FindViewById (Resource.Id.txtname);
EditText say = FindViewById (Resource.Id.txtsay);
TextView content = FindViewById (Resource.Id.txtContent);
mClient.Receive = (o, p) => {
content.Post(delegate {
content.Append(p.Message.ToString());
});
};
FindViewById (Resource.Id.btnlogin).Click += delegate {
Login login = new Login();
login.Name = name.Text;
mClient.Send(login);
};
FindViewById (Resource.Id.btnsay).Click += delegate {
Say s = new Say{ Content=say.Text};
mClient.Send(s);
};
// Get our button from the layout resource,
// and attach an event to it
}
這樣一個(gè)多平臺(tái)的基礎(chǔ)聊天功能就完成了
總結(jié)
以上是生活随笔為你收集整理的linux写聊天程序,轻易实现基于linux或win运行的聊天服务端程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle 9i sql_id,Ora
- 下一篇: linux crontab日志,cron